diff --git a/src/pages/member/OrderDetail.tsx b/src/pages/member/OrderDetail.tsx index 91dea74..db00384 100644 --- a/src/pages/member/OrderDetail.tsx +++ b/src/pages/member/OrderDetail.tsx @@ -517,6 +517,57 @@ export default function OrderDetail() { )} + {/* Cancelled Order Handling */} + {order.status === "cancelled" && ( +
+ + + + {isConsultingOrder + ? "Order ini telah dibatalkan. Slot konsultasi telah dilepaskan." + : "Order ini telah dibatalkan."} + + + + {isConsultingOrder ? ( + // Consulting order - show booking button with pre-filled data +
+ +

+ Kategori dan catatan akan terisi otomatis dari order sebelumnya +

+
+ ) : ( + // Product order - show back to products button + + )} +
+ )} + {/* Fallback button for pending payments without QR */} {order.payment_status === "pending" && !order.qr_string && order.payment_url && (
@@ -572,6 +623,13 @@ export default function OrderDetail() {
)} + {consultingSlots[0]?.notes && ( +
+

Catatan

+

{consultingSlots[0].notes}

+
+ )} + {consultingSlots[0]?.meet_link && (
@@ -613,6 +671,13 @@ export default function OrderDetail() { Pembayaran berhasil! Silakan bergabung sesuai jadwal. + ) : order.status === "cancelled" ? ( + + + + Order telah dibatalkan. Silakan buat booking baru jika masih tertarik. + + ) : ( diff --git a/supabase/functions/cancel-expired-consulting-orders/index.ts b/supabase/functions/cancel-expired-consulting-orders/index.ts index eba264f..7019e5a 100644 --- a/supabase/functions/cancel-expired-consulting-orders/index.ts +++ b/supabase/functions/cancel-expired-consulting-orders/index.ts @@ -16,115 +16,65 @@ serve(async (req: Request): Promise => { 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, diff --git a/supabase/migrations/20241228_remove_pg_cron_job.sql b/supabase/migrations/20241228_remove_pg_cron_job.sql new file mode 100644 index 0000000..1a8debf --- /dev/null +++ b/supabase/migrations/20241228_remove_pg_cron_job.sql @@ -0,0 +1,13 @@ +-- ============================================ +-- Remove pg_cron job (migrating to Coolify-only approach) +-- ============================================ +-- We're moving all cron jobs to Coolify for single source of truth + +-- Remove the pg_cron job +SELECT cron.unschedule('cancel-expired-consulting-orders'); + +-- Verify it's removed +SELECT jobname, schedule, command +FROM cron.job +WHERE jobname LIKE 'cancel-expired%'; +-- Should return 0 rows diff --git a/supabase/migrations/20241228_schedule_cancel_expired_orders.sql b/supabase/migrations/20241228_schedule_cancel_expired_orders.sql new file mode 100644 index 0000000..07e750e --- /dev/null +++ b/supabase/migrations/20241228_schedule_cancel_expired_orders.sql @@ -0,0 +1,101 @@ +-- ============================================ +-- SQL Function for Expired Consulting Orders Cleanup +-- ============================================ +-- This creates a reusable SQL function that can be called from +-- Coolify Scheduled Tasks to cancel expired consulting orders +-- +-- NOTE: We use Coolify for ALL cron jobs (single source of truth) +-- instead of mixing pg_cron and Coolify scheduled tasks + +-- Drop existing function if exists (to handle return type change) +DROP FUNCTION IF EXISTS cancel_expired_consulting_orders_sql(); + +-- Create SQL function to cancel expired orders +CREATE OR REPLACE FUNCTION cancel_expired_consulting_orders_sql() +RETURNS jsonb +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + expired_order RECORD; + expired_session RECORD; + processed_count INTEGER := 0; +BEGIN + -- Log start + RAISE NOTICE '[CANCEL-EXPIRED] Starting check for expired consulting orders'; + + -- Loop through expired consulting orders + FOR expired_order IN + SELECT o.id, o.payment_status, o.qr_expires_at + FROM orders o + INNER JOIN consulting_sessions cs ON cs.order_id = o.id + WHERE o.payment_status = 'pending' + AND o.qr_expires_at < NOW() + AND o.status != 'cancelled' + LOOP + RAISE NOTICE '[CANCEL-EXPIRED] Processing order: %', expired_order.id; + + -- Update order status to cancelled AND payment status to failed + UPDATE orders + SET status = 'cancelled', + payment_status = 'failed' + WHERE id = expired_order.id; + + -- Cancel all consulting sessions for this order + FOR expired_session IN + SELECT id, calendar_event_id + FROM consulting_sessions + WHERE order_id = expired_order.id + AND status != 'cancelled' + LOOP + -- Update session status to cancelled + UPDATE consulting_sessions + SET status = 'cancelled' + WHERE id = expired_session.id; + + -- Delete time slots to release them for re-booking + DELETE FROM consulting_time_slots + WHERE session_id = expired_session.id; + + RAISE NOTICE '[CANCEL-EXPIRED] Cancelled session: %', expired_session.id; + END LOOP; + + processed_count := processed_count + 1; + END LOOP; + + RAISE NOTICE '[CANCEL-EXPIRED] Successfully processed % expired orders', processed_count; + + RETURN jsonb_build_object( + 'success', true, + 'processed', processed_count, + 'message', format('Successfully cancelled %s expired consulting orders', processed_count) + ); +END; +$$; + +-- ============================================ +-- Coolify Scheduled Tasks Configuration +-- ============================================ +-- Instead of using pg_cron, configure these in Coolify: +-- +-- Task 1: Database Cleanup (every 10 minutes) +-- ------------------------------------------- +-- Name: cancel-expired-consulting-orders-db +-- Command: psql -h supabase-db -U postgres -d postgres -c "SELECT cancel_expired_consulting_orders_sql();" +-- Frequency: */10 * * * * +-- Timeout: 30 seconds +-- Container: supabase-db (or supabase-rest if it has psql client) +-- +-- Task 2: Calendar Cleanup (every 15 minutes) +-- ------------------------------------------- +-- Name: cancel-expired-consulting-orders-calendar +-- Command: curl -X POST http://supabase-edge-functions:8000/functions/v1/cancel-expired-consulting-orders +-- Frequency: */15 * * * * +-- Timeout: 30 seconds +-- Container: supabase-edge-functions + +-- ============================================ +-- Manual Testing +-- ============================================ +-- Test the function directly: +-- SELECT cancel_expired_consulting_orders_sql();