diff --git a/backend/src/app.ts b/backend/src/app.ts index 066e8d5..4ce74e0 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -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(' ') diff --git a/backend/src/func/deletePayer.ts b/backend/src/func/deletePayer.ts new file mode 100644 index 0000000..d95fbfd --- /dev/null +++ b/backend/src/func/deletePayer.ts @@ -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) + } + }) +} \ No newline at end of file diff --git a/backend/src/func/index.ts b/backend/src/func/index.ts index afd917d..d9279ad 100644 --- a/backend/src/func/index.ts +++ b/backend/src/func/index.ts @@ -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 } \ No newline at end of file