-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment.ts
53 lines (45 loc) · 1.39 KB
/
payment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { getServerSession } from 'next-auth/next'
import { captureAPIError } from '@/lib/api'
import { authOptions } from '@/lib/auth'
import type { PaymentDTO } from '@/lib/dto/PaymentDTO'
import { METHOD_NOT_ALLOWED, UNAUTHORIZED } from '@/lib/messages'
import type { ApiResponse } from '@/lib/types/api'
import { createPayment } from '@/useCases/createPayment'
import type { NextApiRequest, NextApiResponse } from 'next'
export type PaymentCreateApiResponse = ApiResponse<PaymentDTO>
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const session = await getServerSession(req, res, authOptions)
if (!session?.userId) {
res.status(401)
res.json({ success: false, ...UNAUTHORIZED })
return res.end()
}
switch (req.method) {
case 'POST':
return await handlePOST(session.userId, req, res)
default:
res.status(405)
res.json({ success: false, ...METHOD_NOT_ALLOWED })
return res.end()
}
} catch (e) {
return captureAPIError(e, res)
}
}
const handlePOST = async (
userId: string,
req: NextApiRequest,
res: NextApiResponse<PaymentCreateApiResponse>
) => {
const payment = await createPayment(
userId,
req.query.id as string,
req.query.recipient_id as string,
req.body
)
res.status(201)
res.json({ success: true, data: payment })
return res.end()
}
export default handler