feat: add endpoint to delete payer with bearer token validation and implement deletion logic

This commit is contained in:
Astrian Zheng 2025-01-12 14:03:08 +11:00
parent 7991024972
commit f0856b048a
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
3 changed files with 62 additions and 1 deletions

View File

@ -143,6 +143,21 @@ app.use(route.put('/payer/:id', async (ctx, id) => {
ctx.status = 204
}))
app.use(route.delete('/payer/:id', async (ctx, id) => {
// 请求头验证 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])
if (isNaN(parseInt(id))) throw new HttpError(ErrorDescEnum.invalid_field_format, 400, ['id'])
// 删除付款人
await func.deletePayer(ctx.prisma, id)
ctx.status = 204
}))
app.use(route.post('/invoice', async (ctx) => {
// 请求头验证 bearer token
const bearerToken = ctx.request.headers['authorization']?.split(' ')

View File

@ -0,0 +1,44 @@
import { PrismaClient } from '@prisma/client'
import { ErrorDescEnum, HttpError } from '../classes/HttpError'
export default async (prisma: PrismaClient, id: string) => {
const payer = await prisma.payer.findFirst({
where: {
payer_id: parseInt(id)
}
})
if (!payer) throw new HttpError(ErrorDescEnum.item_not_found, 404, ['payer'])
// 删除所有与付款人相关的发票,包括发票项目
const invoices = await prisma.invoice.findMany({
where: {
payer_id: parseInt(id)
}
})
for (let i in invoices) {
await prisma.invoiceItem.deleteMany({
where: {
invoice_suffix_code: invoices[i].invoice_suffix_code,
invoice_date: invoices[i].invoice_date
}
})
await prisma.invoice.delete({
where: {
invoice_date_invoice_suffix_code: {
invoice_date: invoices[i].invoice_date,
invoice_suffix_code: invoices[i].invoice_suffix_code
}
}
})
}
// 删除付款人
await prisma.payer.delete({
where: {
payer_id: parseInt(id)
}
})
}

View File

@ -7,6 +7,7 @@ import getPayers from "./getPayers"
import updatePayer from "./updatePayer"
import updateInvoiceNote from "./updateInvoiceNote"
import deleteInvoice from "./deleteInvoice"
import deletePayer from "./deletePayer"
export default {
createPayer,
@ -17,5 +18,6 @@ export default {
getPayers,
updatePayer,
updateInvoiceNote,
deleteInvoice
deleteInvoice,
deletePayer
}