修复电子凭证销毁与飞飞订单金额
This commit is contained in:
@@ -4,6 +4,7 @@ import assert from 'node:assert/strict'
|
||||
import {
|
||||
buildFeifeiPlatformOrderNo,
|
||||
resolveKuaishouFeifeiClaimUrl,
|
||||
resolveKuaishouFeifeiPlatformAmount,
|
||||
} from './index.js'
|
||||
import type { TaskRow } from '../../../types/repository/rows.js'
|
||||
|
||||
@@ -66,6 +67,27 @@ test('buildFeifeiPlatformOrderNo falls back to task number', () => {
|
||||
)
|
||||
})
|
||||
|
||||
test('resolveKuaishouFeifeiPlatformAmount converts order fen amount to yuan', () => {
|
||||
assert.equal(resolveKuaishouFeifeiPlatformAmount({
|
||||
totalAmountFen: 3800,
|
||||
quantity: 1,
|
||||
}), 38)
|
||||
})
|
||||
|
||||
test('resolveKuaishouFeifeiPlatformAmount splits amount by item quantity', () => {
|
||||
assert.equal(resolveKuaishouFeifeiPlatformAmount({
|
||||
totalAmountFen: 7600,
|
||||
quantity: 2,
|
||||
}), 38)
|
||||
})
|
||||
|
||||
test('resolveKuaishouFeifeiPlatformAmount skips empty amount', () => {
|
||||
assert.equal(resolveKuaishouFeifeiPlatformAmount({
|
||||
totalAmountFen: 0,
|
||||
quantity: 1,
|
||||
}), 0)
|
||||
})
|
||||
|
||||
function createTask(patch: Partial<TaskRow> = {}): TaskRow {
|
||||
return {
|
||||
id: 123,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { getOrderItemById } from '../../../repositories/order-item-repo.js'
|
||||
import { getOrderById } from '../../../repositories/order-repo.js'
|
||||
import { createTaskEvent } from '../../../repositories/task-event-repo.js'
|
||||
import { updateTask } from '../../../repositories/task-repo.js'
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
@@ -59,11 +61,17 @@ export async function prepareKuaishouFeifeiTask(task: TaskRow) {
|
||||
}
|
||||
|
||||
const platformOrderNo = flow.platformOrderNo || buildFeifeiPlatformOrderNo(task)
|
||||
const order = await createKuaishouFeifeiOrder({
|
||||
const platformAmount = await resolveKuaishouFeifeiTaskPlatformAmount(task)
|
||||
const orderInput: Parameters<typeof createKuaishouFeifeiOrder>[0] = {
|
||||
platformOrderNo,
|
||||
productCode: flow.productCode,
|
||||
platformBuyNum: 1,
|
||||
})
|
||||
}
|
||||
if (platformAmount > 0) {
|
||||
orderInput.platformAmount = platformAmount
|
||||
}
|
||||
|
||||
const order = await createKuaishouFeifeiOrder(orderInput)
|
||||
const feifeiClaimUrl = resolveKuaishouFeifeiOrderClaimUrl(order)
|
||||
if (!feifeiClaimUrl) {
|
||||
throw createHttpError('kuaishou-feifei 未返回领取链接', {
|
||||
@@ -96,6 +104,7 @@ export async function prepareKuaishouFeifeiTask(task: TaskRow) {
|
||||
platformOrderNo,
|
||||
orderNo: order.orderNo,
|
||||
productCode: flow.productCode,
|
||||
platformAmount,
|
||||
rechargeStatus: order.rechargeStatus,
|
||||
rechargeStatusLabel: order.rechargeStatusLabel,
|
||||
rechargeUrl: order.h5.rechargeUrl,
|
||||
@@ -356,3 +365,36 @@ export function buildFeifeiPlatformOrderNo(task: TaskRow) {
|
||||
|
||||
return `OS-FEIFEI-${task.id}`
|
||||
}
|
||||
|
||||
export async function resolveKuaishouFeifeiTaskPlatformAmount(task: TaskRow) {
|
||||
const [order, orderItem] = await Promise.all([
|
||||
getOrderById(task.order_id),
|
||||
getOrderItemById(task.order_item_id),
|
||||
])
|
||||
|
||||
return resolveKuaishouFeifeiPlatformAmount({
|
||||
totalAmountFen: order?.total_amount,
|
||||
quantity: orderItem?.quantity,
|
||||
})
|
||||
}
|
||||
|
||||
export function resolveKuaishouFeifeiPlatformAmount({
|
||||
totalAmountFen,
|
||||
quantity,
|
||||
}: {
|
||||
totalAmountFen?: unknown
|
||||
quantity?: unknown
|
||||
} = {}) {
|
||||
const totalFen = normalizePositiveFen(totalAmountFen)
|
||||
if (totalFen <= 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const normalizedQuantity = Math.max(1, Math.trunc(Number(quantity || 1)) || 1)
|
||||
return Number((totalFen / normalizedQuantity / 100).toFixed(2))
|
||||
}
|
||||
|
||||
function normalizePositiveFen(value: unknown) {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) && parsed > 0 ? Math.round(parsed) : 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user