From 354a9f9af0cac09286c00ec51f0728a51ee67820 Mon Sep 17 00:00:00 2001 From: Jiong Yan Yap Date: Sat, 9 Nov 2024 00:34:54 -0800 Subject: [PATCH] Add waiver webhook --- .../pages/api/tally/webhook-waiver.ts | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 apps/dashboard/pages/api/tally/webhook-waiver.ts diff --git a/apps/dashboard/pages/api/tally/webhook-waiver.ts b/apps/dashboard/pages/api/tally/webhook-waiver.ts new file mode 100644 index 00000000..8b6b95ae --- /dev/null +++ b/apps/dashboard/pages/api/tally/webhook-waiver.ts @@ -0,0 +1,72 @@ +import { getEnv } from '@hibiscus/env'; +import { createClient, PostgrestSingleResponse } from '@supabase/supabase-js'; +import { NextApiHandler, NextApiResponse } from 'next'; + +const handler: NextApiHandler = async (req, res) => { + if (req.method === 'POST') { + const body = req.body; + + const field = body.data.fields.find( + ({ label, type }) => label === 'question_DB9A6R' && type === 'INPUT_TEXT' + ); + if (field === undefined || field === null || field.value === '') { + return createResponse( + res, + 400, + `No email provided for application ID ${body.data.responseId}` + ); + } + + // Update DB + const { data, error } = await updateDb(field.value); + + if (error !== null) { + return createResponse( + res, + 500, + `Unable to update user ${field.value} application ID ${body.data.responseId} in Supabase: ${error.message}` + ); + } else if (data === null || data.length === 0) { + return createResponse( + res, + 500, + `Unable to update user ${field.value} application ID ${body.data.responseId} in Supabase: user does not exist` + ); + } + + return createResponse(res, 200, 'Success'); + } else { + return res.status(405).send('Method not allowed'); + } +}; + +async function updateDb( + email: string +): Promise> { + const supabase = createClient( + getEnv().Hibiscus.Supabase.apiUrl, + getEnv().Hibiscus.Supabase.serviceKey + ); + + const resUP = await supabase + .from('user_profiles') + .select('user_id') + .eq('email', email); + if (resUP.data == null || resUP.data.length === 0) { + return resUP; + } + + const res = await supabase + .from('participants') + .update({ waiver_signed: true }) + .eq('id', resUP.data[0].user_id) + .select(); + + return res; +} + +function createResponse(res: NextApiResponse, status: number, message: string) { + return res.status(status).json({ meta: { statusCode: status, message } }); +} + +export default handler;