Improve cancelled order display and add notes to order detail
- Add "Catatan" field display in consulting order detail page - Add dedicated "Cancelled Order" section with rebooking option - Update status alert to show proper message for cancelled orders - Refactor edge function to focus on calendar cleanup only - Set payment_status to 'failed' when auto-cancelling expired orders 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,115 +16,65 @@ serve(async (req: Request): Promise<Response> => {
|
||||
const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
||||
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
||||
|
||||
console.log("[CANCEL-EXPIRED] Starting check for expired consulting orders");
|
||||
console.log("[CLEANUP-CALENDAR] Starting calendar cleanup for cancelled sessions");
|
||||
|
||||
// Find expired pending consulting orders
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// Get orders with consulting_sessions that are pending payment and QR is expired
|
||||
const { data: expiredOrders, error: queryError } = await supabase
|
||||
.from("orders")
|
||||
.select(`
|
||||
id,
|
||||
payment_status,
|
||||
qr_expires_at,
|
||||
consulting_sessions (
|
||||
id,
|
||||
topic_category,
|
||||
notes,
|
||||
session_date,
|
||||
start_time,
|
||||
end_time
|
||||
)
|
||||
`)
|
||||
.eq("payment_status", "pending")
|
||||
.lt("qr_expires_at", now)
|
||||
.not("consulting_sessions", "is", null);
|
||||
// Find cancelled consulting sessions with calendar events that haven't been cleaned up
|
||||
const { data: cancelledSessions, error: queryError } = await supabase
|
||||
.from("consulting_sessions")
|
||||
.select("id, calendar_event_id")
|
||||
.eq("status", "cancelled")
|
||||
.not("calendar_event_id", "is", null);
|
||||
|
||||
if (queryError) {
|
||||
console.error("[CANCEL-EXPIRED] Query error:", queryError);
|
||||
console.error("[CLEANUP-CALENDAR] Query error:", queryError);
|
||||
throw queryError;
|
||||
}
|
||||
|
||||
if (!expiredOrders || expiredOrders.length === 0) {
|
||||
console.log("[CANCEL-EXPIRED] No expired orders found");
|
||||
if (!cancelledSessions || cancelledSessions.length === 0) {
|
||||
console.log("[CLEANUP-CALENDAR] No cancelled sessions with calendar events found");
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "No expired orders to process",
|
||||
message: "No calendar events to clean up",
|
||||
processed: 0
|
||||
}),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`[CANCEL-EXPIRED] Found ${expiredOrders.length} expired orders`);
|
||||
console.log(`[CLEANUP-CALENDAR] Found ${cancelledSessions.length} cancelled sessions with calendar events`);
|
||||
|
||||
let processedCount = 0;
|
||||
|
||||
// Process each expired order
|
||||
for (const order of expiredOrders) {
|
||||
console.log(`[CANCEL-EXPIRED] Processing order: ${order.id}`);
|
||||
|
||||
// Update order status to cancelled
|
||||
const { error: updateError } = await supabase
|
||||
.from("orders")
|
||||
.update({ status: "cancelled" })
|
||||
.eq("id", order.id);
|
||||
|
||||
if (updateError) {
|
||||
console.error(`[CANCEL-EXPIRED] Failed to update order ${order.id}:`, updateError);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Cancel all consulting sessions for this order
|
||||
if (order.consulting_sessions && order.consulting_sessions.length > 0) {
|
||||
for (const session of order.consulting_sessions) {
|
||||
// Delete calendar event if exists
|
||||
if (session.calendar_event_id) {
|
||||
try {
|
||||
await supabase.functions.invoke('delete-calendar-event', {
|
||||
body: { session_id: session.id }
|
||||
});
|
||||
console.log(`[CANCEL-EXPIRED] Deleted calendar event for session: ${session.id}`);
|
||||
} catch (err) {
|
||||
console.log(`[CANCEL-EXPIRED] Failed to delete calendar event: ${err}`);
|
||||
// Continue anyway
|
||||
}
|
||||
}
|
||||
|
||||
// Update session status to cancelled
|
||||
await supabase
|
||||
.from("consulting_sessions")
|
||||
.update({ status: "cancelled" })
|
||||
.eq("id", session.id);
|
||||
|
||||
// Delete or release time slots
|
||||
await supabase
|
||||
.from("consulting_time_slots")
|
||||
.delete()
|
||||
.eq("session_id", session.id);
|
||||
|
||||
console.log(`[CANCEL-EXPIRED] Cancelled session: ${session.id}`);
|
||||
// Delete calendar events for cancelled sessions
|
||||
for (const session of cancelledSessions) {
|
||||
if (session.calendar_event_id) {
|
||||
try {
|
||||
await supabase.functions.invoke('delete-calendar-event', {
|
||||
body: { session_id: session.id }
|
||||
});
|
||||
console.log(`[CLEANUP-CALENDAR] Deleted calendar event for session: ${session.id}`);
|
||||
processedCount++;
|
||||
} catch (err) {
|
||||
console.log(`[CLEANUP-CALENDAR] Failed to delete calendar event: ${err}`);
|
||||
// Continue with other events even if one fails
|
||||
}
|
||||
}
|
||||
|
||||
processedCount++;
|
||||
}
|
||||
|
||||
console.log(`[CANCEL-EXPIRED] Successfully processed ${processedCount} orders`);
|
||||
console.log(`[CLEANUP-CALENDAR] Successfully cleaned up ${processedCount} calendar events`);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: `Successfully cancelled ${processedCount} expired consulting orders`,
|
||||
message: `Successfully cleaned up ${processedCount} calendar events`,
|
||||
processed: processedCount
|
||||
}),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error("[CANCEL-EXPIRED] Error:", error);
|
||||
console.error("[CLEANUP-CALENDAR] Error:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user