diff --git a/backend/src/app.ts b/backend/src/app.ts index cd0cb2a..066e8d5 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -267,6 +267,19 @@ app.use(route.put('/invoice/INV-:date(\\d{8})-:suffix(\\d+)/note', async (ctx, i ctx.status = 204 })) +app.use(route.delete('/invoice/INV-:date(\\d{8})-:suffix(\\d+)', async (ctx, date, suffix) => { + // 请求头验证 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]) + + // 删除收据 + await func.deleteInvoice(ctx.prisma, date, suffix) + ctx.status = 204 +})) + const port = parseInt(process.env.PORT ?? '3000') app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`) diff --git a/backend/src/func/deleteInvoice.ts b/backend/src/func/deleteInvoice.ts new file mode 100644 index 0000000..d4afefd --- /dev/null +++ b/backend/src/func/deleteInvoice.ts @@ -0,0 +1,32 @@ +import { PrismaClient } from '@prisma/client' +import { ErrorDescEnum, HttpError } from '../classes/HttpError' +import dayjs from 'dayjs' + +export default async (prisma: PrismaClient, issueDateRaw: string, suffix: string) => { + console.log('Deleting invoice', issueDateRaw, suffix) + const issueDate = dayjs(issueDateRaw, 'YYYYMMDD').startOf('day').toDate() + const invoice = await prisma.invoice.findFirst({ + where: { + invoice_date: issueDate, + invoice_suffix_code: parseInt(suffix) + } + }) + + if (!invoice) throw new HttpError(ErrorDescEnum.item_not_found, 404, ['invoice']) + + await prisma.invoiceItem.deleteMany({ + where: { + invoice_suffix_code: parseInt(suffix), + invoice_date: issueDate + } + }) + + await prisma.invoice.delete({ + where: { + invoice_date_invoice_suffix_code: { + invoice_date: issueDate, + invoice_suffix_code: parseInt(suffix) + } + } + }) +} \ No newline at end of file diff --git a/backend/src/func/index.ts b/backend/src/func/index.ts index 4d10718..afd917d 100644 --- a/backend/src/func/index.ts +++ b/backend/src/func/index.ts @@ -6,6 +6,7 @@ import getInvoices from "./getInvoices" import getPayers from "./getPayers" import updatePayer from "./updatePayer" import updateInvoiceNote from "./updateInvoiceNote" +import deleteInvoice from "./deleteInvoice" export default { createPayer, @@ -15,5 +16,6 @@ export default { getInvoices, getPayers, updatePayer, - updateInvoiceNote + updateInvoiceNote, + deleteInvoice } \ No newline at end of file