Files
meet-hub/supabase/functions/create-meet-link/index.ts
dwindown dfda71053c Add n8n test mode toggle and edge function improvements
- Add test/production toggle for n8n webhook URLs in IntegrasiTab
- Update create-meet-link function to use database test_mode setting
- Add send-email-v2 edge function for Mailketing API integration
- Update daily-reminders and send-consultation-reminder to use send-email-v2
- Remove deprecated branding field from BrandingTab
- Update domain references from hub.dwindi.com to with.dwindi.com
- Add environment variables for Coolify deployment
- Add comprehensive edge function test script
- Update payment flow redirect to order detail page

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:24:40 +07:00

133 lines
4.6 KiB
TypeScript

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 CreateMeetRequest {
slot_id: string;
date: string;
start_time: string;
end_time: string;
client_name: string;
client_email: string;
topic: string;
notes?: string;
}
serve(async (req: Request): Promise<Response> => {
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 meet link for slot:", body.slot_id);
// Get platform settings for Google Calendar ID
const { data: settings } = await supabase
.from("platform_settings")
.select("integration_google_calendar_id, brand_name")
.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" } }
);
}
// For now, this is a placeholder that returns a message
// In production, you would integrate with Google Calendar API via OAuth or service account
// Or call an n8n webhook to handle the calendar creation
const { data: integrationSettings } = await supabase
.from("platform_settings")
.select("integration_n8n_base_url, integration_n8n_test_mode")
.single();
if (integrationSettings?.integration_n8n_base_url) {
// Check if we're in test mode (controlled by the integration_n8n_test_mode setting)
const isTestMode = integrationSettings.integration_n8n_test_mode || false;
const webhookPath = isTestMode ? "/webhook-test/" : "/webhook/";
const n8nUrl = `${integrationSettings.integration_n8n_base_url}${webhookPath}create-meet`;
console.log(`Calling n8n webhook: ${n8nUrl} (Test mode: ${isTestMode})`);
try {
const n8nResponse = await fetch(n8nUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
slot_id: body.slot_id,
date: body.date,
start_time: body.start_time,
end_time: body.end_time,
client_name: body.client_name,
client_email: body.client_email,
topic: body.topic,
notes: body.notes,
calendar_id: calendarId,
brand_name: brandName,
test_mode: isTestMode, // Add test_mode flag for n8n to use
}),
});
if (n8nResponse.ok) {
const result = await n8nResponse.json();
if (result.meet_link) {
// Update the slot with the meet link
await supabase
.from("consulting_slots")
.update({ meet_link: result.meet_link })
.eq("id", body.slot_id);
return new Response(
JSON.stringify({ success: true, meet_link: result.meet_link }),
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
}
} catch (n8nError) {
console.error("n8n webhook error:", n8nError);
}
}
// Fallback: Return instructions for manual setup
return new Response(
JSON.stringify({
success: false,
message: "Integrasi otomatis belum tersedia. Silakan buat link Meet secara manual atau konfigurasi n8n webhook di Pengaturan > Integrasi.",
manual_instructions: {
calendar_id: calendarId,
event_title: `Konsultasi: ${body.topic} - ${body.client_name}`,
event_date: body.date,
event_time: `${body.start_time} - ${body.end_time}`,
}
}),
{ status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
} catch (error: any) {
console.error("Error creating meet link:", error);
return new Response(
JSON.stringify({ success: false, message: error.message }),
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
});