feat: integrate Prisma into createPayer function and enhance error handling for existing ABN
This commit is contained in:
parent
3446954648
commit
361bfc6d75
|
@ -5,6 +5,7 @@ import Debug from 'debug'
|
|||
import koaBody from 'koa-body'
|
||||
import func from './func'
|
||||
import { ErrorDescEnum, HttpError } from './types/HttpError'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
dotenv.config()
|
||||
console.log = Debug('invoiceIssuer:app.ts')
|
||||
|
@ -12,10 +13,13 @@ console.log = Debug('invoiceIssuer:app.ts')
|
|||
const app = new Koa()
|
||||
app.use(koaBody())
|
||||
|
||||
// 错误处理中间件
|
||||
// 语境中间件
|
||||
app.use(async (ctx, next) => {
|
||||
try {
|
||||
const prismaClient = new PrismaClient()
|
||||
ctx.prisma = prismaClient
|
||||
await next()
|
||||
prismaClient.$disconnect()
|
||||
} catch (e) {
|
||||
if (e instanceof HttpError) {
|
||||
ctx.status = e.status
|
||||
|
@ -65,7 +69,7 @@ app.use(route.post('/payer', async (ctx) => {
|
|||
if (abn && !abnRegex.test(abn)) throw new HttpError(ErrorDescEnum.invalid_field_format, 400, ['abn'])
|
||||
|
||||
// 创建新的付款人
|
||||
await func.createPayer(name, address, email, abn)
|
||||
await func.createPayer(ctx.prisma, name, address, email, abn)
|
||||
ctx.status = 204
|
||||
}))
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import Debug from 'debug'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { ErrorDescEnum, HttpError } from '../types/HttpError'
|
||||
import { PrismaClient, Prisma } from '@prisma/client'
|
||||
|
||||
console.log = Debug('invoiceIssuer:func/createPayer.ts')
|
||||
|
||||
|
@ -11,24 +12,23 @@ console.log = Debug('invoiceIssuer:func/createPayer.ts')
|
|||
* @param {string} email - 付款人的电子邮件地址
|
||||
* @param {string} abn - 付款人的澳大利亚商业号码
|
||||
*/
|
||||
export default async (name: string, address: string[], email: string, abn?: string) => {
|
||||
const prisma = new PrismaClient()
|
||||
export default async (prisma: PrismaClient<Prisma.PrismaClientOptions, never>, name: string, address: string[], email: string, abn?: string) => {
|
||||
// 验证 ABN 是否已存在
|
||||
const existingPayer = await prisma.payer.findFirst({
|
||||
where: {
|
||||
payer_abn: abn
|
||||
}
|
||||
})
|
||||
if (existingPayer) throw new HttpError(ErrorDescEnum.item_exists, 400, ['abn'])
|
||||
|
||||
try {
|
||||
// 创建新的付款人
|
||||
const payer = await prisma.payer.create({
|
||||
data: {
|
||||
payer_name: name,
|
||||
payer_address: address.join('\n'),
|
||||
payer_email: email,
|
||||
payer_abn: abn
|
||||
}
|
||||
})
|
||||
return payer
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw new Error('unknown_issues')
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
// 创建新的付款人
|
||||
const payer = await prisma.payer.create({
|
||||
data: {
|
||||
payer_name: name,
|
||||
payer_address: address.join('\n'),
|
||||
payer_email: email,
|
||||
payer_abn: abn
|
||||
}
|
||||
})
|
||||
return payer
|
||||
}
|
|
@ -34,14 +34,21 @@ enum ErrorDescEnum {
|
|||
* 必填字段缺失。通常与 HTTP 状态码 400 Bad Request 一起使用。
|
||||
*/
|
||||
required_fields_missing = 'required_fields_missing',
|
||||
|
||||
/**
|
||||
* 字段格式不正确。通常与 HTTP 状态码 400 Bad Request 一起使用。
|
||||
*/
|
||||
invalid_field_format = 'invalid_field_format',
|
||||
|
||||
/**
|
||||
* 未知错误。通常与 HTTP 状态码 500 Internal Server Error 一起使用。
|
||||
*/
|
||||
unknown_issues = 'unknown_issues'
|
||||
unknown_issues = 'unknown_issues',
|
||||
|
||||
/**
|
||||
* 项目已存在。通常与 HTTP 状态码 400 Bad Request 一起使用。
|
||||
*/
|
||||
item_exists = 'item_exists'
|
||||
}
|
||||
|
||||
export { HttpError, ErrorDescEnum }
|
Loading…
Reference in New Issue
Block a user