feat: 积分商城-供应商-对账-发票5
This commit is contained in:
@@ -13,6 +13,13 @@ export const merchantInvoiceListController = withErrorBoundary(async request =>
|
||||
return toHttpResponse(await merchantInvoiceService.list(await readJson<MerchantInvoiceListInput>(request)))
|
||||
})
|
||||
|
||||
export const merchantInvoicePushController = withErrorBoundary(async request => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
if (!await adminAuthService.findSession(getBearerToken(request))) return toHttpResponse(serviceFail('登录已失效,请重新登录', 401, 'UNAUTHORIZED'))
|
||||
return toHttpResponse(await merchantInvoiceService.push(await readJson<{ id?: number }>(request)))
|
||||
})
|
||||
|
||||
export const merchantInvoiceFileController = withErrorBoundary(async request => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export { merchantInvoicePushController as default } from '../controllers/merchant-invoice.controller.js'
|
||||
@@ -79,11 +79,12 @@ const app = new Koa()
|
||||
const router = new Router()
|
||||
const port = Number(process.env.PORT || 8888)
|
||||
|
||||
import { merchantInvoiceListController, merchantInvoiceFileController } from './controllers/merchant-invoice.controller.js'
|
||||
import { merchantInvoiceListController, merchantInvoiceFileController, merchantInvoicePushController } from './controllers/merchant-invoice.controller.js'
|
||||
|
||||
const handlers: Record<string, FunctionHandler> = {
|
||||
'merchant-invoice-list': merchantInvoiceListController,
|
||||
'merchant-invoice-file': merchantInvoiceFileController,
|
||||
'merchant-invoice-push': merchantInvoicePushController,
|
||||
'address-default': addressDefaultController,
|
||||
'address-delete': addressDeleteController,
|
||||
'address-list': addressListController,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { bridgeFetchWithAuth } from '../utils/bridge-auth.js'
|
||||
import { ORDER_EVENT_TYPE, ORDER_PUSH_STATUS } from '../constants/order.js'
|
||||
import { prisma } from '../utils/prisma.js'
|
||||
import { serviceFail, serviceOk } from '../types/service-result.js'
|
||||
import { supplierFileService } from './supplier-file.service.js'
|
||||
@@ -28,6 +30,46 @@ export const merchantInvoiceService = {
|
||||
}))
|
||||
return serviceOk({ invoiceItems, totalCount, pageNumber, pageSize })
|
||||
},
|
||||
async push(input: { id?: number }) {
|
||||
const id = Number(input.id)
|
||||
if (!Number.isInteger(id) || id <= 0) return serviceFail('发票参数不正确')
|
||||
const invoice = await prisma.supplierInvoiceInfo.findFirst({ where: { id, deleted: 0 } })
|
||||
if (!invoice) return serviceFail('发票记录不存在', 404, 'INVOICE_NOT_FOUND')
|
||||
if (![2, 3].includes(invoice.invoiceStatus)) return serviceFail('开票尚未结束,暂不能推送发票')
|
||||
const now = new Date()
|
||||
const pad = (value: number) => String(value).padStart(2, '0')
|
||||
const body = {
|
||||
supplierId: invoice.supplierId,
|
||||
userId: invoice.userId || 'SYSTEM',
|
||||
orderType: ORDER_EVENT_TYPE.INVOICE_INFO,
|
||||
orderId: invoice.settlementId || invoice.invoiceBizId,
|
||||
mainOrderId: invoice.mainOrderId || '',
|
||||
operationTime: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`,
|
||||
time: now.getTime()
|
||||
}
|
||||
const apiPath = '/pointOrder/pushOrderInfoMsg'
|
||||
const log = await prisma.supplierOrderPushLog.create({ data: {
|
||||
supplierId: body.supplierId, userId: body.userId, mainOrderId: invoice.mainOrderId,
|
||||
orderId: body.orderId, orderType: body.orderType, apiPath, requestBody: body, pushStatus: ORDER_PUSH_STATUS.PENDING
|
||||
} })
|
||||
try {
|
||||
const result = await bridgeFetchWithAuth<{ resultCode?: number; message?: string }>(invoice.supplierId, apiPath, body)
|
||||
const success = result.response.ok && result.payload.resultCode === 0
|
||||
const message = result.payload.message || '发票推送失败'
|
||||
await prisma.supplierOrderPushLog.update({ where: { id: log.id }, data: {
|
||||
responseBody: result.payload, httpStatus: result.response.status, resultCode: result.payload.resultCode,
|
||||
pushStatus: success ? ORDER_PUSH_STATUS.SUCCESS : ORDER_PUSH_STATUS.FAILED,
|
||||
errorMessage: success ? null : message, lastPushTime: new Date()
|
||||
} })
|
||||
return success ? serviceOk({ message: '发票推送成功' }) : serviceFail(message, 502, 'INVOICE_PUSH_FAILED')
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : '发票推送失败'
|
||||
await prisma.supplierOrderPushLog.update({ where: { id: log.id }, data: {
|
||||
pushStatus: ORDER_PUSH_STATUS.FAILED, errorMessage: message, lastPushTime: new Date()
|
||||
} })
|
||||
return serviceFail(message, 502, 'INVOICE_PUSH_FAILED')
|
||||
}
|
||||
},
|
||||
async file(input: MerchantInvoiceFileInput) {
|
||||
const id = Number(input.id)
|
||||
if (!Number.isInteger(id) || id <= 0) return serviceFail('发票参数不正确')
|
||||
|
||||
Reference in New Issue
Block a user