feat:拼单明细管理加一个撤份额的功能,指定数量
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { resolveShareReduceTargetQuantity } from './work-order-share-quantity.js'
|
||||
|
||||
test('按份数撤份额:正常扣减', () => {
|
||||
assert.deepEqual(resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: 4 }), {
|
||||
nextQuantity: 5,
|
||||
failureReason: null,
|
||||
})
|
||||
assert.deepEqual(resolveShareReduceTargetQuantity({ currentQuantity: 2, reduceQuantity: 1 }), {
|
||||
nextQuantity: 1,
|
||||
failureReason: null,
|
||||
})
|
||||
})
|
||||
|
||||
test('按份数撤份额:不能撤光、不能撤 0、不能超量', () => {
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 1, reduceQuantity: 1 }).failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: 0 }).failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: -2 }).failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: 9 }).failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: 10 }).failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
})
|
||||
|
||||
test('按份数撤份额:非法输入(非数字/小数/空)一律拒绝', () => {
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: Number.NaN, reduceQuantity: 4 })
|
||||
.failureReason,
|
||||
'invalid_quantity',
|
||||
)
|
||||
assert.equal(
|
||||
resolveShareReduceTargetQuantity({ currentQuantity: 9, reduceQuantity: 4.7 }).nextQuantity,
|
||||
5,
|
||||
'小数向下取整后按 4 份处理',
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 后台按份数撤销拼单份额:校验并算出保留下来的份数。
|
||||
*
|
||||
* 口径(已确认):
|
||||
* - 只能部分撤销,撤到 0 份走「单人撤单」,所以 `1 ≤ 撤掉份数 ≤ 当前份数 − 1`;
|
||||
* - 押金不随份数减少而退还(保持原冻结额),这里只算数量。
|
||||
*/
|
||||
export function resolveShareReduceTargetQuantity(input: {
|
||||
currentQuantity: number
|
||||
reduceQuantity: number
|
||||
}): { nextQuantity: number; failureReason: 'invalid_quantity' | null } {
|
||||
const current = Math.floor(Number(input.currentQuantity) || 0)
|
||||
const reduce = Math.floor(Number(input.reduceQuantity) || 0)
|
||||
if (!Number.isFinite(current) || !Number.isFinite(reduce)) {
|
||||
return { nextQuantity: 0, failureReason: 'invalid_quantity' }
|
||||
}
|
||||
if (current < 2 || reduce < 1 || reduce > current - 1) {
|
||||
return { nextQuantity: 0, failureReason: 'invalid_quantity' }
|
||||
}
|
||||
return { nextQuantity: current - reduce, failureReason: null }
|
||||
}
|
||||
Reference in New Issue
Block a user