From 9f6bb98edbf5399ee221300df2e5a398c3f3e370 Mon Sep 17 00:00:00 2001 From: iamkiddy Date: Wed, 3 Dec 2025 18:50:45 +0000 Subject: [PATCH] Enhance appointment scheduling and rejection logic by refining payload structure. Default create_jitsi_meeting to true if not specified and remove undefined fields from the payload. Update scheduleAppointmentSchema to support optional date_str and time_slot, ensuring either scheduled_datetime or both must be provided for validation. --- lib/actions/appointments.ts | 24 ++++++++++++++++++++++-- lib/schema/appointments.ts | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/actions/appointments.ts b/lib/actions/appointments.ts index 1324622..7266b4f 100644 --- a/lib/actions/appointments.ts +++ b/lib/actions/appointments.ts @@ -392,13 +392,27 @@ export async function scheduleAppointment(id: string, input: ScheduleAppointment throw new Error("Authentication required."); } + // Build payload with defaults + const payload: any = { + ...input, + // Default create_jitsi_meeting to true if not specified + create_jitsi_meeting: input.create_jitsi_meeting !== undefined ? input.create_jitsi_meeting : true, + }; + + // Remove undefined fields + Object.keys(payload).forEach(key => { + if (payload[key] === undefined) { + delete payload[key]; + } + }); + const response = await fetch(`${API_ENDPOINTS.meetings.listAppointments}${id}/schedule/`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${tokens.access}`, }, - body: JSON.stringify(input), + body: JSON.stringify(payload), }); const data = await parseResponse(response); @@ -415,13 +429,19 @@ export async function rejectAppointment(id: string, input: RejectAppointmentInpu throw new Error("Authentication required."); } + // Build payload - only include rejection_reason if provided + const payload: any = {}; + if (input.rejection_reason) { + payload.rejection_reason = input.rejection_reason; + } + const response = await fetch(`${API_ENDPOINTS.meetings.listAppointments}${id}/reject/`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${tokens.access}`, }, - body: JSON.stringify(input), + body: JSON.stringify(payload), }); const data = await parseResponse(response); diff --git a/lib/schema/appointments.ts b/lib/schema/appointments.ts index 69b7e9d..990ee09 100644 --- a/lib/schema/appointments.ts +++ b/lib/schema/appointments.ts @@ -36,10 +36,26 @@ export const createAppointmentSchema = z.object({ export type CreateAppointmentInput = z.infer; // Schedule Appointment Schema (Admin only) +// Supports two scheduling methods: +// 1. Direct datetime: scheduled_datetime + scheduled_duration +// 2. Date and slot: date_str + time_slot + scheduled_duration export const scheduleAppointmentSchema = z.object({ - scheduled_datetime: z.string().datetime("Invalid datetime format"), + scheduled_datetime: z.string().datetime("Invalid datetime format").optional(), scheduled_duration: z.number().int().positive().optional(), -}); + date_str: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format").optional(), + time_slot: z.enum(["morning", "afternoon", "evening"]).optional(), + create_jitsi_meeting: z.boolean().optional(), + jitsi_custom_config: z.string().optional(), +}).refine( + (data) => { + // Either scheduled_datetime OR (date_str + time_slot) must be provided + return data.scheduled_datetime || (data.date_str && data.time_slot); + }, + { + message: "Either scheduled_datetime or both date_str and time_slot must be provided", + path: ["scheduled_datetime"], + } +); export type ScheduleAppointmentInput = z.infer;