Skip to content

Commit 4d6d480

Browse files
committed
refactor: minor reorg of on-pay
1 parent d555503 commit 4d6d480

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

src/pages/api/events/on-pay.ts

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ const JUICE_API_EVENTS_ENABLED = process.env.JUICE_API_EVENTS_ENABLED === 'true'
2020

2121
const logger = getLogger('api/events/on-pay')
2222

23+
enum EmailType {
24+
PayEvent = 'payment-received',
25+
PayReceipt = 'payment-receipt',
26+
}
27+
28+
type EmailEvent = {
29+
email: string
30+
type: EmailType
31+
}
32+
33+
type EmailMetadata = {
34+
amount: string
35+
payerName: string
36+
payerEthscanUrl: string
37+
timestamp: string
38+
projectUrl: string
39+
projectName: string
40+
// Used in transaction receipt
41+
transactionUrl: string | undefined
42+
// Used in transaction receipt
43+
transactionName: string | undefined
44+
}
45+
46+
type OnPayEvent = Awaited<ReturnType<typeof Schema.validate>>
47+
2348
const BigIntValidator = (errorMessage: string) => {
2449
return Yup.mixed<bigint>()
2550
.transform(current => {
@@ -56,58 +81,6 @@ const Schema = Yup.object().shape({
5681
}),
5782
})
5883

59-
type OnPayEvent = Awaited<ReturnType<typeof Schema.validate>>
60-
61-
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
62-
try {
63-
if (req.method !== 'POST' || !JUICE_API_EVENTS_ENABLED) {
64-
return res.status(404).json({ message: 'Not found.' })
65-
}
66-
if (!authCheck(req, res)) return
67-
68-
const event = await Schema.validate(req.body)
69-
70-
const emailMetadata = await compileEmailMetadata(event)
71-
72-
const emailEvents = await findEmailEventsForProjectId(
73-
Number(event.data.projectId),
74-
event.data.payer.toLowerCase(),
75-
)
76-
77-
await sendEmails(emailMetadata, emailEvents)
78-
79-
return res.status(200).json('Success!')
80-
} catch (e) {
81-
logger.error({ error: e })
82-
return res
83-
.status(500)
84-
.json({ message: 'Unexpected server error occurred.' })
85-
}
86-
}
87-
88-
enum EmailType {
89-
PayEvent = 'payment-received',
90-
PayReceipt = 'payment-receipt',
91-
}
92-
93-
type EmailEvent = {
94-
email: string
95-
type: EmailType
96-
}
97-
98-
type EmailMetadata = {
99-
amount: string
100-
payerName: string
101-
payerEthscanUrl: string
102-
timestamp: string
103-
projectUrl: string
104-
projectName: string
105-
// Used in transaction receipt
106-
transactionUrl: string | undefined
107-
// Used in transaction receipt
108-
transactionName: string | undefined
109-
}
110-
11184
const compileEmailMetadata = async ({
11285
data: { projectId, amount, payer },
11386
metadata: { transactionHash },
@@ -243,4 +216,29 @@ const findEmailEventsForProjectId = async (
243216
})
244217
}
245218

246-
export default handler
219+
export default async (req: NextApiRequest, res: NextApiResponse) => {
220+
try {
221+
if (req.method !== 'POST' || !JUICE_API_EVENTS_ENABLED) {
222+
return res.status(404).json({ message: 'Not found.' })
223+
}
224+
if (!authCheck(req, res)) return
225+
226+
const event = await Schema.validate(req.body)
227+
228+
const emailMetadata = await compileEmailMetadata(event)
229+
230+
const emailEvents = await findEmailEventsForProjectId(
231+
Number(event.data.projectId),
232+
event.data.payer.toLowerCase(),
233+
)
234+
235+
await sendEmails(emailMetadata, emailEvents)
236+
237+
return res.status(200).json('Success!')
238+
} catch (e) {
239+
logger.error({ error: e })
240+
return res
241+
.status(500)
242+
.json({ message: 'Unexpected server error occurred.' })
243+
}
244+
}

0 commit comments

Comments
 (0)