import { NextResponse } from 'next/server'; import db from '@/lib/db'; export async function GET() { try { // Ambil semua appointment beserta relasi yang diperlukan const appointments = await db.appointment.findMany({ include: { pasien: { include: { user: true, }, }, koas: { include: { user: true, }, }, schedule: { include: { post: { include: { treatment: true, Review: true, }, }, timeslot: true, }, }, timeslot: true, }, orderBy: { createdAt: 'desc', }, }); // Buat map untuk menghitung jumlah peserta "Confirmed" per timeslot per tanggal const participantsMap = new Map(); for (const a of appointments) { if (a.status === 'Confirmed' && a.scheduleId && a.timeslotId && a.date) { const key = `${a.scheduleId}_${a.timeslotId}_${a.date}`; participantsMap.set(key, (participantsMap.get(key) || 0) + 1); } } // Update setiap appointment dengan currentParticipants pada timeslot terkait const updatedAppointments = appointments.map((appointment) => { const { schedule, timeslotId, date } = appointment; let relatedTimeslot = null; let currentParticipants = 0; if (schedule && schedule.timeslot && Array.isArray(schedule.timeslot)) { relatedTimeslot = schedule.timeslot.find( (slot) => slot.id === timeslotId ); } if (appointment.scheduleId && timeslotId && date) { const key = `${appointment.scheduleId}_${timeslotId}_${date}`; currentParticipants = participantsMap.get(key) || 0; } return { ...appointment, schedule: { ...schedule, timeslot: relatedTimeslot ? { ...relatedTimeslot, currentParticipants, } : null, }, }; }); return NextResponse.json( { appointments: updatedAppointments, }, { status: 200 } ); } catch (error) { console.error('Error getting appointments:', error); return NextResponse.json( { status: 'Error', message: 'Internal Server Error' }, { status: 500 } ); } } export async function POST(req: Request) { const body = await req.json(); const { scheduleId, pasienId, koasId, timeslotId, date } = body; if (!scheduleId || !pasienId || !koasId || !timeslotId || !date) { return NextResponse.json( { error: 'Missing required fields' }, { status: 400 } ); } try { // Cek apakah schedule, pasien, dan koas valid const schedule = await db.schedule.findUnique({ where: { id: scheduleId }, include: { post: true, timeslot: true, }, }); const pasien = await db.pasienProfile.findUnique({ where: { id: pasienId }, }); const koas = await db.koasProfile.findUnique({ where: { id: koasId }, }); if (!schedule || !pasien || !koas) { return NextResponse.json( { error: 'Invalid schedule, pasien or koas' }, { status: 404 } ); } if ( new Date(date) < new Date(schedule.dateStart) || new Date(date) > new Date(schedule.dateEnd) ) { return NextResponse.json( { error: 'Date is not within the schedule range' }, { status: 400 } ); } // Cek apakah timeslotId yang dipilih ada dalam schedule const selectedTimeslot = schedule.timeslot.find( (slot) => slot.id === timeslotId ); if (!selectedTimeslot) { return NextResponse.json( { error: 'Timeslot not found for the given schedule' }, { status: 404 } ); } if (!selectedTimeslot.isAvailable) { return NextResponse.json( { error: 'Timeslot is fully booked or unavailable.' }, { status: 400 } ); } // Membuat appointment baru const appointment = await db.appointment.create({ data: { pasien: { connect: { id: pasienId }, }, koas: { connect: { id: koasId }, }, schedule: { connect: { id: scheduleId }, }, timeslot: { connect: { id: timeslotId }, }, date: date, status: 'Pending', }, }); const appointments = { ...appointment, date: appointment.date.split('T')[0], }; return NextResponse.json( { appointments, }, { status: 201 } ); } catch (error) { console.error('Error creating appointment:', error); return NextResponse.json( { error: 'Internal Server Error' }, { status: 500 } ); } } export async function DELETE() { try { await db.appointment.deleteMany({}); return NextResponse.json( { status: 'Success', message: 'All appointments deleted successfully', }, { status: 200 } ); } catch (error) { console.error('Error deleting all appointments', error); return NextResponse.json( { error: 'Internal Server Error' }, { status: 500 } ); } }