feat: add endpoint to delete invoice with bearer token validation and implement deletion logic
This commit is contained in:
parent
a0d6e3cbc6
commit
7991024972
|
@ -267,6 +267,19 @@ app.use(route.put('/invoice/INV-:date(\\d{8})-:suffix(\\d+)/note', async (ctx, i
|
||||||
ctx.status = 204
|
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')
|
const port = parseInt(process.env.PORT ?? '3000')
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server is running at http://localhost:${port}`)
|
console.log(`Server is running at http://localhost:${port}`)
|
||||||
|
|
32
backend/src/func/deleteInvoice.ts
Normal file
32
backend/src/func/deleteInvoice.ts
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import getInvoices from "./getInvoices"
|
||||||
import getPayers from "./getPayers"
|
import getPayers from "./getPayers"
|
||||||
import updatePayer from "./updatePayer"
|
import updatePayer from "./updatePayer"
|
||||||
import updateInvoiceNote from "./updateInvoiceNote"
|
import updateInvoiceNote from "./updateInvoiceNote"
|
||||||
|
import deleteInvoice from "./deleteInvoice"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
createPayer,
|
createPayer,
|
||||||
|
@ -15,5 +16,6 @@ export default {
|
||||||
getInvoices,
|
getInvoices,
|
||||||
getPayers,
|
getPayers,
|
||||||
updatePayer,
|
updatePayer,
|
||||||
updateInvoiceNote
|
updateInvoiceNote,
|
||||||
|
deleteInvoice
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user