107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
import type { Context } from '@netlify/functions'
|
|
import { guardMethod } from '../utils/method.js'
|
|
import { fail, ok } from '../utils/http.js'
|
|
import { readJson } from '../utils/request.js'
|
|
import { prisma } from '../utils/prisma.js'
|
|
import { withErrorBoundary } from '../utils/handler.js'
|
|
|
|
type OrderListRequest = {
|
|
supplierId?: string
|
|
userId?: string
|
|
}
|
|
|
|
type OrderStatusView = {
|
|
text: string
|
|
statusClass: string
|
|
actionText: string
|
|
buttonClass: string
|
|
}
|
|
|
|
const statusMap: Record<number, OrderStatusView> = {
|
|
0: { text: '待付款', statusClass: 'pending', actionText: '去支付', buttonClass: 'primary-action' },
|
|
1: { text: '待发货', statusClass: 'pending', actionText: '查看订单', buttonClass: 'primary-action' },
|
|
2: { text: '待发货', statusClass: 'pending', actionText: '查看订单', buttonClass: 'primary-action' },
|
|
3: { text: '待收货', statusClass: 'pending', actionText: '确认收货', buttonClass: 'dark-action' },
|
|
4: { text: '已完成', statusClass: 'done', actionText: '再次购买', buttonClass: 'dark-action' },
|
|
5: { text: '已完成', statusClass: 'done', actionText: '再次购买', buttonClass: 'dark-action' }
|
|
}
|
|
|
|
const toYuanText = (value: { toFixed: (digits: number) => string } | number | string | null | undefined) => {
|
|
if (value && typeof value === 'object' && 'toFixed' in value) {
|
|
return value.toFixed(2)
|
|
}
|
|
|
|
return Number(value || 0).toFixed(2)
|
|
}
|
|
|
|
const firstImage = (picInfo: unknown) => {
|
|
if (Array.isArray(picInfo)) {
|
|
return String(picInfo[0] || '')
|
|
}
|
|
|
|
return ''
|
|
}
|
|
|
|
export default 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()
|
|
|
|
if (!supplierId || !userId) {
|
|
return fail('供应商或用户信息不能为空')
|
|
}
|
|
|
|
const subOrders = await prisma.supplierOrderSub.findMany({
|
|
where: {
|
|
supplierId,
|
|
userId,
|
|
deleted: 0
|
|
},
|
|
include: {
|
|
mainOrder: true,
|
|
products: {
|
|
where: { deleted: 0 },
|
|
orderBy: { createAt: 'asc' }
|
|
}
|
|
},
|
|
orderBy: [
|
|
{ createTime: 'desc' },
|
|
{ createAt: 'desc' }
|
|
]
|
|
})
|
|
|
|
const orders = subOrders.map(order => {
|
|
const status = statusMap[order.orderState] || statusMap[1]
|
|
const products = order.products.map(product => ({
|
|
id: product.id,
|
|
sku: product.sku,
|
|
name: product.name,
|
|
number: product.number,
|
|
price: toYuanText(product.price),
|
|
image: product.productImgUrl || firstImage(product.picInfo)
|
|
}))
|
|
|
|
return {
|
|
id: order.orderId,
|
|
orderId: order.orderId,
|
|
orderNumberId: order.orderNumberId || '',
|
|
mainOrderId: order.mainOrderId,
|
|
mainOrderNumberId: order.mainOrder?.orderNumberId || '',
|
|
supplierId: order.supplierId,
|
|
status: status.text,
|
|
statusClass: status.statusClass,
|
|
actionText: status.actionText,
|
|
buttonClass: status.buttonClass,
|
|
orderState: order.orderState,
|
|
amount: toYuanText(order.totalPrice),
|
|
count: products.reduce((total, item) => total + Number(item.number || 0), 0),
|
|
products
|
|
}
|
|
})
|
|
|
|
return ok({ orders })
|
|
})
|