feat: 基本实现拆单收货--
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import type { Context } from '../utils/handler.js'
|
||||
import { guardMethod } from '../utils/method.js'
|
||||
import { readJson } from '../utils/request.js'
|
||||
import { withErrorBoundary } from '../utils/handler.js'
|
||||
import { orderActionService } from '../services/order-action.service.js'
|
||||
import { orderCreateService, type CheckoutGoods } from '../services/order-create.service.js'
|
||||
import { orderListService } from '../services/order-list.service.js'
|
||||
import { orderPushEventService, type OrderPushEventInput } from '../services/order-push-event.service.js'
|
||||
import { supplierOpenOrderService, type SupplierOrderQueryRequest } from '../services/supplier-open-order.service.js'
|
||||
import { toHttpResponse } from './controller-response.js'
|
||||
|
||||
type OrderListRequest = {
|
||||
supplierId?: string
|
||||
userId?: string
|
||||
}
|
||||
|
||||
type OrderActionRequest = {
|
||||
supplierId?: string
|
||||
userId?: string
|
||||
orderId?: string
|
||||
mainOrderId?: string
|
||||
amount?: number
|
||||
totalPrice?: number
|
||||
wPayOrAPay?: number
|
||||
pointsPrice?: number
|
||||
}
|
||||
|
||||
type OrderCreateRequest = {
|
||||
supplierId?: string
|
||||
userId?: string
|
||||
orderNo?: string
|
||||
orderNumberId?: string
|
||||
amount?: number
|
||||
goods?: CheckoutGoods[]
|
||||
}
|
||||
|
||||
const centsToYuan = (value?: number) => Number((Number(value || 0) / 100).toFixed(2))
|
||||
|
||||
export const orderListController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderListRequest>(request)
|
||||
const supplierId = String(body.supplierId || '').trim()
|
||||
const userId = String(body.userId || '').trim()
|
||||
|
||||
return toHttpResponse(await orderListService.listOrders(supplierId, userId))
|
||||
})
|
||||
|
||||
export const orderCreateController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderCreateRequest>(request)
|
||||
|
||||
return toHttpResponse(await orderCreateService.createOrder({
|
||||
supplierId: String(body.supplierId || '').trim(),
|
||||
userId: String(body.userId || '').trim(),
|
||||
orderNo: String(body.orderNo || '').trim(),
|
||||
orderNumberId: body.orderNumberId,
|
||||
amount: body.amount,
|
||||
goods: Array.isArray(body.goods) ? body.goods : []
|
||||
}))
|
||||
})
|
||||
|
||||
export const orderCancelMainController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderActionRequest>(request)
|
||||
|
||||
return toHttpResponse(await orderActionService.cancelPendingMain({
|
||||
supplierId: String(body.supplierId || '').trim(),
|
||||
userId: String(body.userId || '').trim(),
|
||||
orderId: String(body.orderId || '').trim(),
|
||||
mainOrderId: String(body.mainOrderId || body.orderId || '').trim()
|
||||
}))
|
||||
})
|
||||
|
||||
export const orderCancelPaidMainController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderActionRequest>(request)
|
||||
|
||||
return toHttpResponse(await orderActionService.cancelPaidMain({
|
||||
supplierId: String(body.supplierId || '').trim(),
|
||||
userId: String(body.userId || '').trim(),
|
||||
orderId: String(body.orderId || '').trim(),
|
||||
mainOrderId: String(body.mainOrderId || body.orderId || '').trim()
|
||||
}))
|
||||
})
|
||||
|
||||
export const orderCancelSubController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderActionRequest>(request)
|
||||
|
||||
return toHttpResponse(await orderActionService.cancelSub({
|
||||
supplierId: String(body.supplierId || '').trim(),
|
||||
userId: String(body.userId || '').trim(),
|
||||
orderId: String(body.orderId || '').trim(),
|
||||
mainOrderId: String(body.mainOrderId || '').trim()
|
||||
}))
|
||||
})
|
||||
|
||||
export const orderPayController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderActionRequest>(request)
|
||||
const totalPrice = Number(body.totalPrice ?? centsToYuan(body.amount))
|
||||
|
||||
return toHttpResponse(await orderActionService.payOrder({
|
||||
supplierId: String(body.supplierId || '').trim(),
|
||||
orderId: String(body.orderId || '').trim(),
|
||||
totalPrice,
|
||||
wPayOrAPay: Number(body.wPayOrAPay ?? 0),
|
||||
pointsPrice: Number(body.pointsPrice ?? totalPrice)
|
||||
}))
|
||||
})
|
||||
|
||||
export const orderPushEventController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
const body = await readJson<OrderPushEventInput>(request)
|
||||
|
||||
return toHttpResponse(await orderPushEventService.pushEvent(body))
|
||||
})
|
||||
|
||||
export const supplierOrderInfoController = withErrorBoundary(async (request: Request, _context: Context) => {
|
||||
const methodResponse = guardMethod(request, ['POST'])
|
||||
if (methodResponse) return methodResponse
|
||||
|
||||
let body: SupplierOrderQueryRequest
|
||||
try {
|
||||
body = supplierOpenOrderService.parseBody(await readJson<SupplierOrderQueryRequest>(request))
|
||||
} catch (error) {
|
||||
return Response.json({
|
||||
resultCode: 400,
|
||||
message: error instanceof Error ? error.message : '请求参数解密失败',
|
||||
orderInfo: []
|
||||
})
|
||||
}
|
||||
|
||||
return Response.json(await supplierOpenOrderService.queryOrderInfo(body))
|
||||
})
|
||||
Reference in New Issue
Block a user