forked from PayButton/paybutton-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[id].ts
More file actions
49 lines (44 loc) · 2.5 KB
/
[id].ts
File metadata and controls
49 lines (44 loc) · 2.5 KB
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
import { RESPONSE_MESSAGES, TX_PAGE_SIZE_LIMIT, DEFAULT_TX_PAGE_SIZE } from 'constants/index'
import { fetchTransactionsByPaybuttonIdWithPagination } from 'services/transactionService'
import * as paybuttonService from 'services/paybuttonService'
import { setSession } from 'utils/setSession'
import { parseError } from 'utils/validators'
export default async (req: any, res: any): Promise<void> => {
if (req.method === 'GET') {
await setSession(req, res)
const userId = req.session.userId
const paybuttonId = req.query.id as string
const page = (req.query.page === '' || req.query.page === undefined) ? 0 : Number(req.query.page)
const pageSize = (req.query.pageSize === '' || req.query.pageSize === undefined) ? DEFAULT_TX_PAGE_SIZE : Number(req.query.pageSize)
const orderBy = (req.query.orderBy === '' || req.query.orderBy === undefined) ? undefined : req.query.orderBy as string
const orderDesc: boolean = !!(req.query.orderDesc === '' || req.query.orderDesc === undefined || req.query.orderDesc === 'true')
const includeInputs: boolean = req.query.includeInputs === 'true'
if (isNaN(page) || isNaN(pageSize)) {
throw new Error(RESPONSE_MESSAGES.PAGE_SIZE_AND_PAGE_SHOULD_BE_NUMBERS_400.message)
}
if (pageSize > TX_PAGE_SIZE_LIMIT) {
throw new Error(RESPONSE_MESSAGES.PAGE_SIZE_LIMIT_EXCEEDED_400.message)
}
try {
const paybutton = await paybuttonService.fetchPaybuttonById(paybuttonId)
if (paybutton.providerUserId !== userId) {
throw new Error(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.message)
}
const transactions = await fetchTransactionsByPaybuttonIdWithPagination(paybuttonId, page, pageSize, orderDesc, orderBy, undefined, includeInputs)
res.status(200).json({ transactions })
} catch (err: any) {
const parsedError = parseError(err)
if (parsedError.message === RESPONSE_MESSAGES.NO_TRANSACTION_FOUND_404.message) {
res.status(RESPONSE_MESSAGES.NO_TRANSACTION_FOUND_404.statusCode)
.json(RESPONSE_MESSAGES.NO_TRANSACTION_FOUND_404)
} else if (parsedError.message === RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.message) {
res.status(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.statusCode)
.json(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400)
} else {
res.status(500).json({ statusCode: 500, message: parsedError.message })
}
}
} else {
res.status(405).json(RESPONSE_MESSAGES.METHOD_NOT_ALLOWED_405)
}
}