diff --git a/src/pages/api/events/on-pay.ts b/src/pages/api/events/on-pay.ts index c54752366f..7c3624d84f 100644 --- a/src/pages/api/events/on-pay.ts +++ b/src/pages/api/events/on-pay.ts @@ -20,6 +20,31 @@ const JUICE_API_EVENTS_ENABLED = process.env.JUICE_API_EVENTS_ENABLED === 'true' const logger = getLogger('api/events/on-pay') +enum EmailType { + PayEvent = 'payment-received', + PayReceipt = 'payment-receipt', +} + +type EmailEvent = { + email: string + type: EmailType +} + +type EmailMetadata = { + amount: string + payerName: string + payerEthscanUrl: string + timestamp: string + projectUrl: string + projectName: string + // Used in transaction receipt + transactionUrl: string | undefined + // Used in transaction receipt + transactionName: string | undefined +} + +type OnPayEvent = Awaited> + const BigIntValidator = (errorMessage: string) => { return Yup.mixed() .transform(current => { @@ -56,58 +81,6 @@ const Schema = Yup.object().shape({ }), }) -type OnPayEvent = Awaited> - -const handler = async (req: NextApiRequest, res: NextApiResponse) => { - try { - if (req.method !== 'POST' || !JUICE_API_EVENTS_ENABLED) { - return res.status(404).json({ message: 'Not found.' }) - } - if (!authCheck(req, res)) return - - const event = await Schema.validate(req.body) - - const emailMetadata = await compileEmailMetadata(event) - - const emailEvents = await findEmailEventsForProjectId( - Number(event.data.projectId), - event.data.payer.toLowerCase(), - ) - - await sendEmails(emailMetadata, emailEvents) - - return res.status(200).json('Success!') - } catch (e) { - logger.error({ error: e }) - return res - .status(500) - .json({ message: 'Unexpected server error occurred.' }) - } -} - -enum EmailType { - PayEvent = 'payment-received', - PayReceipt = 'payment-receipt', -} - -type EmailEvent = { - email: string - type: EmailType -} - -type EmailMetadata = { - amount: string - payerName: string - payerEthscanUrl: string - timestamp: string - projectUrl: string - projectName: string - // Used in transaction receipt - transactionUrl: string | undefined - // Used in transaction receipt - transactionName: string | undefined -} - const compileEmailMetadata = async ({ data: { projectId, amount, payer }, metadata: { transactionHash }, @@ -243,4 +216,29 @@ const findEmailEventsForProjectId = async ( }) } -export default handler +export default async (req: NextApiRequest, res: NextApiResponse) => { + try { + if (req.method !== 'POST' || !JUICE_API_EVENTS_ENABLED) { + return res.status(404).json({ message: 'Not found.' }) + } + if (!authCheck(req, res)) return + + const event = await Schema.validate(req.body) + + const emailMetadata = await compileEmailMetadata(event) + + const emailEvents = await findEmailEventsForProjectId( + Number(event.data.projectId), + event.data.payer.toLowerCase(), + ) + + await sendEmails(emailMetadata, emailEvents) + + return res.status(200).json('Success!') + } catch (e) { + logger.error({ error: e }) + return res + .status(500) + .json({ message: 'Unexpected server error occurred.' }) + } +}