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.

This commit is contained in:
iamkiddy 2025-12-03 18:50:45 +00:00
parent ce8383da89
commit 9f6bb98edb
2 changed files with 40 additions and 4 deletions

View File

@ -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);

View File

@ -36,10 +36,26 @@ export const createAppointmentSchema = z.object({
export type CreateAppointmentInput = z.infer<typeof createAppointmentSchema>;
// 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<typeof scheduleAppointmentSchema>;