Wires pg_net-based database triggers on bookings/inquiries to invoke the two Edge Functions on insert. Two empirically-discovered fixes over the brief's literal SQL were required to make this work locally: - ALTER DATABASE ... SET for a custom GUC needs superuser, but the local migration role (postgres) isn't superuser in this Supabase CLI stack (only supabase_admin is) - inlined the base URL as a literal constant in each trigger function instead. - pg_net's http_post lives in schema `net`, not `extensions.net` - fixed the function calls accordingly. Also disabled verify_jwt for both functions in config.toml since they're invoked by pg_net (no Authorization header), not by end-user clients. Claude-Session: https://claude.ai/code/session_01NhxA4npAXAxmCDEJt6kxSH
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { createClient } from 'jsr:@supabase/supabase-js@2'
|
|
import { buildBookingNotificationEmail } from '../_shared/email.ts'
|
|
|
|
const FROM_ADDRESS = 'bookings@braidsbymira.com'
|
|
const MIRA_EMAIL = 'hello@braidsbymira.com'
|
|
|
|
Deno.serve(async (req) => {
|
|
const payload = await req.json()
|
|
const booking = payload.record
|
|
|
|
const supabaseAdmin = createClient(
|
|
Deno.env.get('SUPABASE_URL')!,
|
|
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
|
|
)
|
|
|
|
const { data: service } = await supabaseAdmin
|
|
.from('services')
|
|
.select('name')
|
|
.eq('id', booking.service_id)
|
|
.single()
|
|
|
|
const { subject, html } = buildBookingNotificationEmail({
|
|
serviceName: service?.name ?? booking.service_id,
|
|
date: booking.date,
|
|
time: booking.time,
|
|
contact: {
|
|
name: booking.contact_name,
|
|
phone: booking.contact_phone,
|
|
email: booking.contact_email,
|
|
notes: booking.contact_notes,
|
|
},
|
|
})
|
|
|
|
const resendApiKey = Deno.env.get('RESEND_API_KEY')
|
|
await fetch('https://api.resend.com/emails', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${resendApiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ from: FROM_ADDRESS, to: MIRA_EMAIL, subject, html }),
|
|
})
|
|
|
|
return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } })
|
|
})
|