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 GoogleServiceAccount { type: string; project_id: string; private_key_id: string; private_key: string; client_email: string; client_id: string; auth_uri: string; token_uri: string; } 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 create JWT for Google API authentication async function getGoogleAccessToken(serviceAccount: GoogleServiceAccount): Promise { const header = { alg: "RS256", typ: "JWT", }; const now = Math.floor(Date.now() / 1000); const payload = { iss: serviceAccount.client_email, scope: "https://www.googleapis.com/auth/calendar", aud: serviceAccount.token_uri, exp: now + 3600, iat: now, }; // Import JWT functionality const { default: jwt } = await import("https://deno.land/x/jose@v4.14.4/index.ts"); const privateKey = await crypto.subtle.importKey( "pkcs8", StringEncoder.encode(serviceAccount.private_key), { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["sign"] ); const token = await jwt.sign(header, payload, privateKey); // Exchange JWT for access token const response = await fetch(serviceAccount.token_uri, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", assertion: token, }), }); const data = await response.json(); return data.access_token; } // String encoder helper const StringEncoder = new TextEncoder(); 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); const body: CreateMeetRequest = await req.json(); console.log("Creating Google Meet event for slot:", body.slot_id); // Get platform settings const { data: settings } = await supabase .from("platform_settings") .select("integration_google_calendar_id, brand_name, google_service_account_json") .single(); const calendarId = settings?.integration_google_calendar_id; const brandName = settings?.brand_name || "LearnHub"; 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 service account from settings or environment let serviceAccountJson: string | null = null; // Priority 1: Check if stored in platform_settings (encrypted field recommended) if (settings?.google_service_account_json) { serviceAccountJson = settings.google_service_account_json; } // Priority 2: Check environment variable if (!serviceAccountJson) { serviceAccountJson = Deno.env.get("GOOGLE_SERVICE_ACCOUNT_JSON") || null; } if (!serviceAccountJson) { return new Response( JSON.stringify({ success: false, message: "Google Service Account JSON belum dikonfigurasi. Tambahkan di environment variables atau platform_settings." }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } // Parse service account JSON let serviceAccount: GoogleServiceAccount; try { serviceAccount = JSON.parse(serviceAccountJson); } catch (error) { console.error("Failed to parse service account JSON:", error); return new Response( JSON.stringify({ success: false, message: "Format Google Service Account JSON tidak valid" }), { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } // Get access token const accessToken = await getGoogleAccessToken(serviceAccount); // 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}\nBrand: ${brandName}`, start: { dateTime: startDate.toISOString(), timeZone: "Asia/Jakarta", }, end: { dateTime: endDate.toISOString(), timeZone: "Asia/Jakarta", }, attendees: [ { email: body.client_email }, ], conferenceData: { createRequest: { requestId: body.slot_id, conferenceSolutionKey: { type: "hangoutsMeet" }, }, }, sendUpdates: "all", guestsCanInviteOthers: false, guestsCanModify: false, guestsCanSeeOtherGuests: false, }; // Create event via Google Calendar API const 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", }, body: JSON.stringify(eventData), } ); 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(); // Update the slot with the meet link 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 }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } } ); } });