feat: add endpoint to retrieve paginated payers with bearer token validation
This commit is contained in:
parent
940468f23b
commit
28e14224e7
|
@ -79,6 +79,28 @@ app.use(route.post('/payer', async (ctx) => {
|
|||
ctx.status = 204
|
||||
}))
|
||||
|
||||
app.use(route.get('/payer', async (ctx) => {
|
||||
// 请求头验证 bearer token
|
||||
const bearerToken = ctx.request.headers['authorization']?.split(' ')
|
||||
if (!bearerToken) throw new HttpError(ErrorDescEnum.unauthorized, 401)
|
||||
if (bearerToken[0] !== 'Bearer') throw new HttpError(ErrorDescEnum.unauthorized, 401)
|
||||
if (!bearerToken[1]) throw new HttpError(ErrorDescEnum.unauthorized, 401)
|
||||
await func.verifyBearerToken(bearerToken[1])
|
||||
|
||||
// 获取 pager 和 limit
|
||||
const pageQuery = ctx.request.query.page
|
||||
if (Array.isArray(pageQuery)) throw new HttpError(ErrorDescEnum.invalid_field_format, 400, ['page'])
|
||||
const page = pageQuery ? parseInt(pageQuery) : 1
|
||||
const limitQuery = ctx.request.query.limit
|
||||
if (Array.isArray(limitQuery)) throw new HttpError(ErrorDescEnum.invalid_field_format, 400, ['limit'])
|
||||
const limit = limitQuery ? parseInt(limitQuery) : 10
|
||||
if (page <= 0 || limit < 5) throw new HttpError(ErrorDescEnum.invalid_field_format, 400, ['page', 'limit'])
|
||||
|
||||
// 获取付款人
|
||||
const payers = await func.getPayers(ctx.prisma, page, limit)
|
||||
ctx.body = payers
|
||||
}))
|
||||
|
||||
app.use(route.post('/invoice', async (ctx) => {
|
||||
// 请求头验证 bearer token
|
||||
const bearerToken = ctx.request.headers['authorization']?.split(' ')
|
||||
|
|
20
backend/src/func/getPayers.ts
Normal file
20
backend/src/func/getPayers.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
export default async function getPayers(prisma: PrismaClient, page: number, limit: number) {
|
||||
const payers = await prisma.payer.findMany({
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
orderBy: {
|
||||
payer_id: 'asc'
|
||||
}
|
||||
})
|
||||
return payers.map(payer => {
|
||||
return {
|
||||
id: payer.payer_id,
|
||||
name: payer.payer_name,
|
||||
address: payer.payer_address.split('\n'),
|
||||
email: payer.payer_email,
|
||||
abn: payer.payer_abn
|
||||
}
|
||||
})
|
||||
}
|
|
@ -3,6 +3,7 @@ import issueInvoice from "./issueInvoice"
|
|||
import verifyBearerToken from "./verifyBearerToken"
|
||||
import getSpecificInvoice from "./getSpecificInvoice"
|
||||
import getInvoices from "./getInvoices"
|
||||
import getPayers from "./getPayers"
|
||||
|
||||
export default {
|
||||
createPayer,
|
||||
|
@ -10,4 +11,5 @@ export default {
|
|||
verifyBearerToken,
|
||||
getSpecificInvoice,
|
||||
getInvoices,
|
||||
getPayers,
|
||||
}
|
Loading…
Reference in New Issue
Block a user