import { supabase, supabaseServiceRole } from '@/lib/supabase'; import type { APIRoute } from 'astro'; export const POST: APIRoute = async ({ request }) => { const headers = { 'Content-Type': 'application/json' }; try { const { email, userId } = await request.json(); if (!email || !userId) { return new Response( JSON.stringify({ success: false, message: 'Please verify your email so that we can find your submissions!', }), { status: 400, headers } ); } const db = supabaseServiceRole ?? supabase; const { data: signatures, error } = await db .from('unesco_petition_signatures') .select( 'id, email, real_name, home_location, instagram_username, photo_proof_url, creation_time' ) .eq('email', email.toLowerCase()) .eq('workos_user_id', userId) .order('creation_time', { ascending: false }); if (error || !signatures || signatures.length === 0) { return new Response( JSON.stringify({ success: false, message: 'Database does not have any petition signatures from you. I hope you will positively consider signing the petition!', }), { status: 404, headers } ); } return new Response(JSON.stringify({ success: true, signatures }), { status: 200, headers }); } catch (error) { return new Response( JSON.stringify({ success: false, message: 'Database is having trouble finding your petition signatures. Please try again, and if database fails again, please text me on Instagram (@palacejunwon) so that I can fix the problem for you!', }), { status: 500, headers } ); } };