Skip to content

Commit

Permalink
refactor: minor reorg of on-pay
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk committed Jan 15, 2024
1 parent d555503 commit 4d6d480
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions src/pages/api/events/on-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof Schema.validate>>

const BigIntValidator = (errorMessage: string) => {
return Yup.mixed<bigint>()
.transform(current => {
Expand Down Expand Up @@ -56,58 +81,6 @@ const Schema = Yup.object().shape({
}),
})

type OnPayEvent = Awaited<ReturnType<typeof Schema.validate>>

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 },
Expand Down Expand Up @@ -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.' })
}
}

2 comments on commit 4d6d480

@vercel
Copy link

@vercel vercel bot commented on 4d6d480 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4d6d480 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.