Fix edge function and add to deployment script
- Update create-google-meet-event with improved JWT handling - Fix jose library import and token signing - Add better error logging - Include create-google-meet-event in deployment script 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
6
add-google-service-account-column.sql
Normal file
6
add-google-service-account-column.sql
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-- Add google_service_account_json column to platform_settings
|
||||||
|
ALTER TABLE platform_settings
|
||||||
|
ADD COLUMN IF NOT EXISTS google_service_account_json TEXT;
|
||||||
|
|
||||||
|
-- Add comment for documentation
|
||||||
|
COMMENT ON COLUMN platform_settings.google_service_account_json IS 'Google Service Account JSON for Calendar API integration (use service account to avoid OAuth)';
|
||||||
@@ -33,6 +33,7 @@ deploy_function() {
|
|||||||
deploy_function "pakasir-webhook"
|
deploy_function "pakasir-webhook"
|
||||||
deploy_function "send-test-email"
|
deploy_function "send-test-email"
|
||||||
deploy_function "create-meet-link" # Includes n8n test mode toggle
|
deploy_function "create-meet-link" # Includes n8n test mode toggle
|
||||||
|
deploy_function "create-google-meet-event" # Direct Google Calendar API integration
|
||||||
deploy_function "send-consultation-reminder"
|
deploy_function "send-consultation-reminder"
|
||||||
deploy_function "send-notification"
|
deploy_function "send-notification"
|
||||||
deploy_function "send-email-v2"
|
deploy_function "send-email-v2"
|
||||||
|
|||||||
5
refresh-schema.sql
Normal file
5
refresh-schema.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
-- This query will force a schema refresh
|
||||||
|
SELECT column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'platform_settings'
|
||||||
|
AND column_name = 'google_service_account_json';
|
||||||
@@ -13,7 +13,6 @@ interface GoogleServiceAccount {
|
|||||||
private_key: string;
|
private_key: string;
|
||||||
client_email: string;
|
client_email: string;
|
||||||
client_id: string;
|
client_id: string;
|
||||||
auth_uri: string;
|
|
||||||
token_uri: string;
|
token_uri: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,11 +27,17 @@ interface CreateMeetRequest {
|
|||||||
notes?: string;
|
notes?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to create JWT for Google API authentication
|
// Function to create JWT and get access token
|
||||||
async function getGoogleAccessToken(serviceAccount: GoogleServiceAccount): Promise<string> {
|
async function getGoogleAccessToken(serviceAccount: GoogleServiceAccount): Promise<string> {
|
||||||
|
try {
|
||||||
|
// Import JWT library
|
||||||
|
const { jwt } = await import("https://deno.land/x/jose@v4.15.1/index.ts");
|
||||||
|
|
||||||
|
// Create JWT header and payload
|
||||||
const header = {
|
const header = {
|
||||||
alg: "RS256",
|
alg: "RS256",
|
||||||
typ: "JWT",
|
typ: "JWT",
|
||||||
|
kid: serviceAccount.private_key_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
const now = Math.floor(Date.now() / 1000);
|
const now = Math.floor(Date.now() / 1000);
|
||||||
@@ -44,18 +49,14 @@ async function getGoogleAccessToken(serviceAccount: GoogleServiceAccount): Promi
|
|||||||
iat: now,
|
iat: now,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Import JWT functionality
|
// Import key
|
||||||
const { default: jwt } = await import("https://deno.land/x/jose@v4.14.4/index.ts");
|
const privateKey = serviceAccount.private_key;
|
||||||
|
|
||||||
const privateKey = await crypto.subtle.importKey(
|
// Sign JWT
|
||||||
"pkcs8",
|
const token = await jwt.sign(payload, privateKey, {
|
||||||
StringEncoder.encode(serviceAccount.private_key),
|
algorithm: 'RS256',
|
||||||
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
header: header,
|
||||||
false,
|
});
|
||||||
["sign"]
|
|
||||||
);
|
|
||||||
|
|
||||||
const token = await jwt.sign(header, payload, privateKey);
|
|
||||||
|
|
||||||
// Exchange JWT for access token
|
// Exchange JWT for access token
|
||||||
const response = await fetch(serviceAccount.token_uri, {
|
const response = await fetch(serviceAccount.token_uri, {
|
||||||
@@ -67,12 +68,23 @@ async function getGoogleAccessToken(serviceAccount: GoogleServiceAccount): Promi
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
if (!response.ok) {
|
||||||
return data.access_token;
|
const errorText = await response.text();
|
||||||
|
throw new Error(`Token exchange failed: ${errorText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// String encoder helper
|
const data = await response.json();
|
||||||
const StringEncoder = new TextEncoder();
|
|
||||||
|
if (!data.access_token) {
|
||||||
|
throw new Error("No access token in response");
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.access_token;
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error("Error getting Google access token:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serve(async (req: Request): Promise<Response> => {
|
serve(async (req: Request): Promise<Response> => {
|
||||||
if (req.method === "OPTIONS") {
|
if (req.method === "OPTIONS") {
|
||||||
@@ -88,13 +100,17 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
console.log("Creating Google Meet event for slot:", body.slot_id);
|
console.log("Creating Google Meet event for slot:", body.slot_id);
|
||||||
|
|
||||||
// Get platform settings
|
// Get platform settings
|
||||||
const { data: settings } = await supabase
|
const { data: settings, error: settingsError } = await supabase
|
||||||
.from("platform_settings")
|
.from("platform_settings")
|
||||||
.select("integration_google_calendar_id, brand_name, google_service_account_json")
|
.select("integration_google_calendar_id, google_service_account_json")
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
|
if (settingsError) {
|
||||||
|
console.error("Error fetching settings:", settingsError);
|
||||||
|
throw settingsError;
|
||||||
|
}
|
||||||
|
|
||||||
const calendarId = settings?.integration_google_calendar_id;
|
const calendarId = settings?.integration_google_calendar_id;
|
||||||
const brandName = settings?.brand_name || "LearnHub";
|
|
||||||
|
|
||||||
if (!calendarId) {
|
if (!calendarId) {
|
||||||
return new Response(
|
return new Response(
|
||||||
@@ -106,24 +122,14 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get service account from settings or environment
|
// Get service account from settings
|
||||||
let serviceAccountJson: string | null = null;
|
const serviceAccountJson = settings?.google_service_account_json;
|
||||||
|
|
||||||
// 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) {
|
if (!serviceAccountJson) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: false,
|
success: false,
|
||||||
message: "Google Service Account JSON belum dikonfigurasi. Tambahkan di environment variables atau platform_settings."
|
message: "Google Service Account JSON belum dikonfigurasi. Tambahkan di Pengaturan > Integrasi."
|
||||||
}),
|
}),
|
||||||
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||||
);
|
);
|
||||||
@@ -133,12 +139,12 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
let serviceAccount: GoogleServiceAccount;
|
let serviceAccount: GoogleServiceAccount;
|
||||||
try {
|
try {
|
||||||
serviceAccount = JSON.parse(serviceAccountJson);
|
serviceAccount = JSON.parse(serviceAccountJson);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error("Failed to parse service account JSON:", error);
|
console.error("Failed to parse service account JSON:", error);
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: false,
|
success: false,
|
||||||
message: "Format Google Service Account JSON tidak valid"
|
message: "Format Google Service Account JSON tidak valid: " + error.message
|
||||||
}),
|
}),
|
||||||
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||||
);
|
);
|
||||||
@@ -146,6 +152,7 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
|
|
||||||
// Get access token
|
// Get access token
|
||||||
const accessToken = await getGoogleAccessToken(serviceAccount);
|
const accessToken = await getGoogleAccessToken(serviceAccount);
|
||||||
|
console.log("Got access token");
|
||||||
|
|
||||||
// Build event data
|
// Build event data
|
||||||
const startDate = new Date(`${body.date}T${body.start_time}`);
|
const startDate = new Date(`${body.date}T${body.start_time}`);
|
||||||
@@ -153,7 +160,7 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
|
|
||||||
const eventData = {
|
const eventData = {
|
||||||
summary: `Konsultasi: ${body.topic} - ${body.client_name}`,
|
summary: `Konsultasi: ${body.topic} - ${body.client_name}`,
|
||||||
description: `Client: ${body.client_email}\n\nNotes: ${body.notes || '-'}\n\nSlot ID: ${body.slot_id}\nBrand: ${brandName}`,
|
description: `Client: ${body.client_email}\n\nNotes: ${body.notes || '-'}\n\nSlot ID: ${body.slot_id}`,
|
||||||
start: {
|
start: {
|
||||||
dateTime: startDate.toISOString(),
|
dateTime: startDate.toISOString(),
|
||||||
timeZone: "Asia/Jakarta",
|
timeZone: "Asia/Jakarta",
|
||||||
@@ -177,6 +184,8 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
guestsCanSeeOtherGuests: false,
|
guestsCanSeeOtherGuests: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log("Creating event in calendar:", calendarId);
|
||||||
|
|
||||||
// Create event via Google Calendar API
|
// Create event via Google Calendar API
|
||||||
const calendarResponse = await fetch(
|
const calendarResponse = await fetch(
|
||||||
`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendarId)}/events?conferenceDataVersion=1`,
|
`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendarId)}/events?conferenceDataVersion=1`,
|
||||||
@@ -203,6 +212,7 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const eventDataResult = await calendarResponse.json();
|
const eventDataResult = await calendarResponse.json();
|
||||||
|
console.log("Event created:", eventDataResult.id);
|
||||||
|
|
||||||
// Update the slot with the meet link
|
// Update the slot with the meet link
|
||||||
if (eventDataResult.hangoutLink) {
|
if (eventDataResult.hangoutLink) {
|
||||||
@@ -233,7 +243,10 @@ serve(async (req: Request): Promise<Response> => {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Error creating Google Meet event:", error);
|
console.error("Error creating Google Meet event:", error);
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ success: false, message: error.message }),
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
message: error.message || "Unknown error occurred"
|
||||||
|
}),
|
||||||
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user