@@ -20,6 +20,31 @@ const JUICE_API_EVENTS_ENABLED = process.env.JUICE_API_EVENTS_ENABLED === 'true'
20
20
21
21
const logger = getLogger ( 'api/events/on-pay' )
22
22
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
+
23
48
const BigIntValidator = ( errorMessage : string ) => {
24
49
return Yup . mixed < bigint > ( )
25
50
. transform ( current => {
@@ -56,58 +81,6 @@ const Schema = Yup.object().shape({
56
81
} ) ,
57
82
} )
58
83
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
-
111
84
const compileEmailMetadata = async ( {
112
85
data : { projectId, amount, payer } ,
113
86
metadata : { transactionHash } ,
@@ -243,4 +216,29 @@ const findEmailEventsForProjectId = async (
243
216
} )
244
217
}
245
218
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