feat: implement createPayer function and enhance /payer route with error handling

This commit is contained in:
Astrian Zheng 2025-01-11 21:57:51 +11:00
parent a273bba786
commit d183b82a7c
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
4 changed files with 48 additions and 13 deletions

View File

@ -3,6 +3,7 @@ import dotenv from 'dotenv'
import route from 'koa-route'
import Debug from 'debug'
import koaBody from 'koa-body'
import func from './func'
dotenv.config()
console.log = Debug('invoiceIssuer:app.ts')
@ -19,21 +20,27 @@ const getRoot = route.get('/', (ctx) => {
ctx.body = 'Hello World'
})
/**
* POST /payer
* @summary 便
* @param {Koa.Context} ctx - Koa context object
*/
const postPayer = route.post('/payer', async (ctx) => {
// TODO: 请求头验证 bearer token
// TODO: 请求头验证 bearer token
const { name, address, email, abn } = ctx.request.body
if (!name || !address || !email) {
ctx.throw(400, 'required_fields_missing')
}
const { name, address, abn, email } = ctx.request.body
if (!name || !address || !email) {
ctx.status = 400;
ctx.body = { error: 'required_fields_missing' }
return;
}
try {
const payer = await func.createPayer(name, address, email, abn)
ctx.body = payer
} catch (e) {
if (e instanceof HttpError) {
ctx.status = e.status
ctx.body = e.message
} else {
console.error(e)
ctx.throw(500, 'unknown_issues')
}
}
})
// 导出路由

View File

@ -0,0 +1,10 @@
/**
* POST /payer
* @summary 便
* @param {string} name -
* @param {string} address -
* @param {string} email -
* @param {string} abn -
*/
export default async (name: string, address: string, email: string, abn?: string) => {
}

View File

@ -0,0 +1,5 @@
import createPayer from "./createPayer"
export default {
createPayer
}

13
backend/src/types/HttpError.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
/**
* 便 i18n
* HTTP
*/
class HttpError extends Error {
/**
* HTTP
* @param message
* @param status
*/
constructor(message: 'required_fields_missing' | 'unknown_issues', status: number);
status: 400 | 401 | 403 | 404 | 500;
}