import { serve } from "https://deno.land/std@0.190.0/http/server.ts"; import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type", }; interface GoogleOAuthConfig { client_id: string; client_secret: string; refresh_token: string; access_token?: string; expires_at?: number; // Timestamp when access_token expires } interface CreateMeetRequest { slot_id: string; date: string; start_time: string; end_time: string; client_name: string; client_email: string; topic: string; notes?: string; } // Function to get access token from refresh token (OAuth2) async function getGoogleAccessToken(oauthConfig: GoogleOAuthConfig): Promise<{ access_token: string; expires_in: number }> { try { console.log("Attempting to exchange refresh token for access token..."); console.log("Client ID:", oauthConfig.client_id); const tokenRequest = { client_id: oauthConfig.client_id, client_secret: oauthConfig.client_secret, refresh_token: oauthConfig.refresh_token, grant_type: "refresh_token", }; console.log("Token request payload:", JSON.stringify({ ...tokenRequest, client_secret: "***REDACTED***", refresh_token: tokenRequest.refresh_token.substring(0, 20) + "..." })); const response = await fetch("https://oauth2.googleapis.com/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(tokenRequest), }); const responseText = await response.text(); console.log("Token response status:", response.status); console.log("Token response body:", responseText); if (!response.ok) { throw new Error(`Token exchange failed: ${responseText}`); } const data = await response.json(); if (!data.access_token) { throw new Error("No access token in response"); } console.log("Successfully obtained access token"); return { access_token: data.access_token, expires_in: data.expires_in || 3600 }; } catch (error: any) { console.error("Error getting Google access token:", error); throw error; } } serve(async (req: Request): Promise => { if (req.method === "OPTIONS") { return new Response(null, { headers: corsHeaders }); } try { const supabaseUrl = Deno.env.get("SUPABASE_URL")!; const supabaseServiceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!; const supabase = createClient(supabaseUrl, supabaseServiceKey); // Clone the request to avoid stream consumption issues // Read body text first, then parse JSON let body: CreateMeetRequest; let debugInfo: any = { method: req.method, headers: Object.fromEntries(req.headers.entries()), contentType: req.headers.get("content-type"), bodyConsumed: false }; try { debugInfo.bodyReadAttempt = "Starting req.text()"; const bodyText = await req.text(); debugInfo.bodyLength = bodyText.length; debugInfo.bodyPreview = bodyText.substring(0, 200); console.log("Raw body text:", bodyText.substring(0, 100) + "..."); body = JSON.parse(bodyText); debugInfo.parsedBody = body; } catch (bodyError) { debugInfo.readError = (bodyError as Error).message; console.error("Error reading body:", bodyError); console.error("Debug info:", JSON.stringify(debugInfo, null, 2)); return new Response( JSON.stringify({ success: false, message: "Invalid request body: " + (bodyError as Error).message, debug: debugInfo }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } console.log("Creating Google Meet event for slot:", body.slot_id); // Get platform settings const { data: settings, error: settingsError } = await supabase .from("platform_settings") .select("integration_google_calendar_id, google_oauth_config") .single(); if (settingsError) { console.error("Error fetching settings:", settingsError); throw settingsError; } const calendarId = settings?.integration_google_calendar_id; if (!calendarId) { return new Response( JSON.stringify({ success: false, message: "Google Calendar ID belum dikonfigurasi di Pengaturan > Integrasi" }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } // Get OAuth config from settings const oauthConfigJson = settings?.google_oauth_config; if (!oauthConfigJson) { return new Response( JSON.stringify({ success: false, message: "Google OAuth Config belum dikonfigurasi. Tambahkan di Pengaturan > Integrasi. Format: {\"client_id\":\"...\",\"client_secret\":\"...\",\"refresh_token\":\"...\"}" }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } // Parse OAuth config JSON let oauthConfig: GoogleOAuthConfig; try { oauthConfig = JSON.parse(oauthConfigJson); } catch (error: any) { console.error("Failed to parse OAuth config JSON:", error); return new Response( JSON.stringify({ success: false, message: "Format Google OAuth Config tidak valid: " + error.message }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } // Check if we have a valid cached access_token let accessToken: string; const now = Math.floor(Date.now() / 1000); // Current time in seconds if (oauthConfig.access_token && oauthConfig.expires_at && oauthConfig.expires_at > now + 60) { // Token is still valid (with 60 second buffer) console.log("Using cached access_token (expires at:", new Date(oauthConfig.expires_at * 1000).toISOString(), ")"); accessToken = oauthConfig.access_token; } else { // Need to refresh the token console.log("Access token expired or missing, refreshing..."); const tokenData = await getGoogleAccessToken(oauthConfig); accessToken = tokenData.access_token; // Update the cached token in database with new expiry const newExpiresAt = now + tokenData.expires_in; const updatedConfig = { ...oauthConfig, access_token: accessToken, expires_at: newExpiresAt }; // Save updated config back to database await supabase .from("platform_settings") .update({ google_oauth_config: JSON.stringify(updatedConfig) }) .eq("id", settings.id); console.log("Updated cached access_token in database"); } console.log("Got access token"); // Build event data const startDate = new Date(`${body.date}T${body.start_time}`); const endDate = new Date(`${body.date}T${body.end_time}`); const eventData = { summary: `Konsultasi: ${body.topic} - ${body.client_name}`, description: `Client: ${body.client_email}\n\nNotes: ${body.notes || '-'}\n\nSlot ID: ${body.slot_id}`, start: { dateTime: startDate.toISOString(), timeZone: "Asia/Jakarta", }, end: { dateTime: endDate.toISOString(), timeZone: "Asia/Jakarta", }, conferenceData: { createRequest: { requestId: body.slot_id, conferenceSolutionKey: { type: "hangoutsMeet" } }, }, }; console.log("Creating event in calendar:", calendarId); console.log("Event data:", JSON.stringify(eventData, null, 2)); // Create event via Google Calendar API with better error handling let calendarResponse: Response; try { calendarResponse = await fetch( `https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendarId)}/events?conferenceDataVersion=1`, { method: "POST", headers: { "Authorization": `Bearer ${accessToken}`, "Content-Type": "application/json", "User-Agent": "Deno/1.0 (Supabase Edge Function)", }, body: JSON.stringify(eventData), } ); } catch (fetchError: any) { console.error("Network error calling Google Calendar API:", fetchError); return new Response( JSON.stringify({ success: false, message: "Network error calling Google Calendar API: " + fetchError.message }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } console.log("Calendar API response status:", calendarResponse.status); if (!calendarResponse.ok) { const errorText = await calendarResponse.text(); console.error("Google Calendar API error:", errorText); return new Response( JSON.stringify({ success: false, message: "Gagal membuat event di Google Calendar: " + errorText }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } const eventDataResult = await calendarResponse.json(); console.log("Event created:", eventDataResult.id); console.log("Full event response:", JSON.stringify(eventDataResult, null, 2)); // Check if conference data was created if (eventDataResult.conferenceData && eventDataResult.conferenceData.entryPoints) { const meetLink = eventDataResult.conferenceData.entryPoints.find((ep: any) => ep.entryPointType === "video")?.uri; if (meetLink) { await supabase .from("consulting_slots") .update({ meet_link: meetLink }) .eq("id", body.slot_id); return new Response( JSON.stringify({ success: true, meet_link: meetLink, event_id: eventDataResult.id, html_link: eventDataResult.htmlLink, }), { headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } } // Fallback to hangoutLink for backwards compatibility if (eventDataResult.hangoutLink) { await supabase .from("consulting_slots") .update({ meet_link: eventDataResult.hangoutLink }) .eq("id", body.slot_id); return new Response( JSON.stringify({ success: true, meet_link: eventDataResult.hangoutLink, event_id: eventDataResult.id, html_link: eventDataResult.htmlLink, }), { headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } return new Response( JSON.stringify({ success: false, message: "Event berhasil dibuat tapi tidak ada meet link" }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } catch (error: any) { console.error("Error creating Google Meet event:", error); return new Response( JSON.stringify({ success: false, message: error.message || "Unknown error occurred" }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } });