11import { serve } from "https://deno.land/std@0.168.0/http/server.ts" ;
22import { createClient } from "npm:@supabase/supabase-js@2" ;
3+ import { corsHeaders , handleCorsPreflight } from "../_shared/cors.ts" ;
34
45const VEXA_API_KEY = Deno . env . get ( "VEXA_API_KEY" ) ;
56const SUPABASE_URL = Deno . env . get ( "SUPABASE_URL" ) ;
67const SUPABASE_SERVICE_ROLE_KEY = Deno . env . get ( "SUPABASE_SERVICE_ROLE_KEY" ) ;
78
89serve ( async ( req ) => {
10+ const preflight = handleCorsPreflight ( req ) ;
11+ if ( preflight ) return preflight ;
12+
913 console . log ( "Start-bot function called" ) ;
1014
1115 // Handle GET requests for testing
@@ -14,7 +18,7 @@ serve(async (req) => {
1418 JSON . stringify ( {
1519 message : "This endpoint requires a POST request with meeting_url and meeting_id in the body"
1620 } ) ,
17- { status : 400 , headers : { "Content-Type" : "application/json" } }
21+ { status : 400 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
1822 ) ;
1923 }
2024
@@ -24,15 +28,15 @@ serve(async (req) => {
2428 console . error ( "VEXA_API_KEY is not set" ) ;
2529 return new Response (
2630 JSON . stringify ( { error : "VEXA_API_KEY is not set" } ) ,
27- { status : 500 , headers : { "Content-Type" : "application/json" } }
31+ { status : 500 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
2832 ) ;
2933 }
3034
3135 if ( ! SUPABASE_URL || ! SUPABASE_SERVICE_ROLE_KEY ) {
3236 console . error ( "SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY is not set" ) ;
3337 return new Response (
3438 JSON . stringify ( { error : "Database credentials are not set" } ) ,
35- { status : 500 , headers : { "Content-Type" : "application/json" } }
39+ { status : 500 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
3640 ) ;
3741 }
3842
@@ -45,7 +49,7 @@ serve(async (req) => {
4549 console . error ( "Error parsing request body:" , e ) ;
4650 return new Response (
4751 JSON . stringify ( { error : "Invalid JSON body" } ) ,
48- { status : 400 , headers : { "Content-Type" : "application/json" } }
52+ { status : 400 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
4953 ) ;
5054 }
5155
@@ -56,15 +60,15 @@ serve(async (req) => {
5660 console . error ( "Missing meeting_url in request" ) ;
5761 return new Response (
5862 JSON . stringify ( { error : "Missing meeting_url in request" } ) ,
59- { status : 400 , headers : { "Content-Type" : "application/json" } }
63+ { status : 400 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
6064 ) ;
6165 }
6266
6367 if ( ! meeting_id ) {
6468 console . error ( "Missing meeting_id in request" ) ;
6569 return new Response (
6670 JSON . stringify ( { error : "Missing meeting_id in request" } ) ,
67- { status : 400 , headers : { "Content-Type" : "application/json" } }
71+ { status : 400 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
6872 ) ;
6973 }
7074
@@ -73,7 +77,7 @@ serve(async (req) => {
7377 console . error ( "Only Google Meet URLs are supported" ) ;
7478 return new Response (
7579 JSON . stringify ( { error : "Only Google Meet URLs are supported" } ) ,
76- { status : 400 , headers : { "Content-Type" : "application/json" } }
80+ { status : 400 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
7781 ) ;
7882 }
7983
@@ -114,20 +118,20 @@ serve(async (req) => {
114118 error : "Bot started but failed to update meeting record" ,
115119 details : updateError . message
116120 } ) ,
117- { status : 500 , headers : { "Content-Type" : "application/json" } }
121+ { status : 500 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
118122 ) ;
119123 }
120124
121125 console . log ( "Meeting record updated successfully" ) ;
122126
123127 return new Response ( JSON . stringify ( result ) , {
124- headers : { "Content-Type" : "application/json" } ,
128+ headers : { ... corsHeaders , "Content-Type" : "application/json" } ,
125129 } ) ;
126130 } catch ( error ) {
127131 console . error ( "Error in start-bot function:" , error ) ;
128132 return new Response (
129133 JSON . stringify ( { error : error instanceof Error ? error . message : "Unknown error" } ) ,
130- { status : 500 , headers : { "Content-Type" : "application/json" } }
134+ { status : 500 , headers : { ... corsHeaders , "Content-Type" : "application/json" } }
131135 ) ;
132136 }
133137} )
0 commit comments