增加现场提货的配置

This commit is contained in:
2026-08-26 09:22:29 +08:00
parent c7cd0f6cf2
commit 632539e8dc
28 changed files with 393 additions and 105 deletions
+32 -13
View File
@@ -7,6 +7,7 @@ import {
calcOrderBoxCount,
shouldHoldAutoCourierDispatch,
validateMinPurchase,
minBottleQtyForDelivery,
toBottleQuantity,
toMinSaleQuantity,
validateBusinessHours,
@@ -34,36 +35,54 @@ describe('calcBenefitAmount', () => {
});
describe('validateMinPurchase', () => {
const mins = { pickupMinQty: 2, localMinQty: 2, crossMinQty: 6 };
it('local requires 2 bottles', () => {
expect(validateMinPurchase('LOCAL', 1, 2, 6).ok).toBe(false);
expect(validateMinPurchase('LOCAL', 2, 2, 6).ok).toBe(true);
expect(validateMinPurchase('LOCAL', 1, mins).ok).toBe(false);
expect(validateMinPurchase('LOCAL', 2, mins).ok).toBe(true);
});
it('cross city requires 6 bottles', () => {
expect(validateMinPurchase('CROSS_CITY', 5, 2, 6).ok).toBe(false);
expect(validateMinPurchase('CROSS_CITY', 6, 2, 6).ok).toBe(true);
expect(validateMinPurchase('CROSS_CITY', 5, mins).ok).toBe(false);
expect(validateMinPurchase('CROSS_CITY', 6, mins).ok).toBe(true);
});
it('on-site pickup requires at least local min (2 bottles)', () => {
expect(validateMinPurchase('ON_SITE_PICKUP', 0, 2, 6).ok).toBe(false);
expect(validateMinPurchase('ON_SITE_PICKUP', 1, 2, 6).ok).toBe(false);
expect(validateMinPurchase('ON_SITE_PICKUP', 2, 2, 6).ok).toBe(true);
it('on-site pickup requires pickup min (default 2 bottles)', () => {
expect(validateMinPurchase('ON_SITE_PICKUP', 0, mins).ok).toBe(false);
expect(validateMinPurchase('ON_SITE_PICKUP', 1, mins).ok).toBe(false);
expect(validateMinPurchase('ON_SITE_PICKUP', 2, mins).ok).toBe(true);
});
it('box SKU: 1 box (6 bottles) satisfies local and cross mins', () => {
it('on-site pickup uses pickupMinQty independently of localMinQty', () => {
const split = { pickupMinQty: 3, localMinQty: 2, crossMinQty: 6 };
expect(validateMinPurchase('ON_SITE_PICKUP', 2, split).ok).toBe(false);
expect(validateMinPurchase('ON_SITE_PICKUP', 3, split).ok).toBe(true);
expect(validateMinPurchase('LOCAL', 2, split).ok).toBe(true);
});
it('box SKU: 1 box (6 bottles) satisfies local, cross and pickup mins', () => {
const opts = { bottlesPerUnit: 6, saleUnit: 'BOX' as const };
expect(validateMinPurchase('LOCAL', 1, 2, 6, opts).ok).toBe(true);
expect(validateMinPurchase('CROSS_CITY', 1, 2, 6, opts).ok).toBe(true);
expect(validateMinPurchase('ON_SITE_PICKUP', 1, 2, 6, opts).ok).toBe(true);
expect(validateMinPurchase('LOCAL', 1, mins, opts).ok).toBe(true);
expect(validateMinPurchase('CROSS_CITY', 1, mins, opts).ok).toBe(true);
expect(validateMinPurchase('ON_SITE_PICKUP', 1, mins, opts).ok).toBe(true);
});
it('box SKU message uses 箱', () => {
const r = validateMinPurchase('LOCAL', 0, 2, 6, { bottlesPerUnit: 6, saleUnit: 'BOX' });
const r = validateMinPurchase('LOCAL', 0, mins, { bottlesPerUnit: 6, saleUnit: 'BOX' });
expect(r.ok).toBe(false);
expect(r.message).toContain('箱');
});
});
describe('minBottleQtyForDelivery', () => {
it('picks pickup / local / cross independently', () => {
const mins = { pickupMinQty: 2, localMinQty: 3, crossMinQty: 6 };
expect(minBottleQtyForDelivery('ON_SITE_PICKUP', mins)).toBe(2);
expect(minBottleQtyForDelivery('LOCAL', mins)).toBe(3);
expect(minBottleQtyForDelivery('CROSS_CITY', mins)).toBe(6);
});
});
describe('toBottleQuantity / toMinSaleQuantity', () => {
it('converts sale qty to bottles', () => {
expect(toBottleQuantity(2, 1)).toBe(2);
+31 -19
View File
@@ -33,36 +33,48 @@ export function saleUnitLabel(saleUnit: ProductSaleUnit | string | null | undefi
return saleUnit === 'BOX' ? '箱' : '瓶';
}
export type DeliveryFulfillmentType = 'LOCAL' | 'CROSS_CITY' | 'ON_SITE_PICKUP';
/** 城市履约起购(瓶当量) */
export type MinPurchaseMins = {
pickupMinQty: number;
localMinQty: number;
crossMinQty: number;
};
export function minBottleQtyForDelivery(
deliveryType: DeliveryFulfillmentType,
mins: MinPurchaseMins,
): number {
if (deliveryType === 'ON_SITE_PICKUP') {
return mins.pickupMinQty > 0 ? mins.pickupMinQty : 2;
}
if (deliveryType === 'LOCAL') {
return mins.localMinQty;
}
return mins.crossMinQty;
}
export function validateMinPurchase(
deliveryType: 'LOCAL' | 'CROSS_CITY' | 'ON_SITE_PICKUP',
deliveryType: DeliveryFulfillmentType,
quantity: number,
localMinQty: number,
crossMinQty: number,
mins: MinPurchaseMins,
options?: { bottlesPerUnit?: number; saleUnit?: ProductSaleUnit },
): { ok: boolean; message?: string } {
const bottlesPerUnit = options?.bottlesPerUnit ?? 1;
const saleUnit = options?.saleUnit ?? (bottlesPerUnit > 1 ? 'BOX' : 'BOTTLE');
const bottleQty = toBottleQuantity(quantity, bottlesPerUnit);
const unit = saleUnitLabel(saleUnit);
if (deliveryType === 'ON_SITE_PICKUP') {
const minBottles = localMinQty > 0 ? localMinQty : 2;
if (bottleQty < minBottles) {
const minSale = toMinSaleQuantity(minBottles, bottlesPerUnit);
return { ok: false, message: `现场提货至少购买 ${minSale}${unit}` };
}
return { ok: true };
}
const minBottles = deliveryType === 'LOCAL' ? localMinQty : crossMinQty;
const minBottles = minBottleQtyForDelivery(deliveryType, mins);
if (bottleQty < minBottles) {
const minSale = toMinSaleQuantity(minBottles, bottlesPerUnit);
return {
ok: false,
message:
deliveryType === 'LOCAL'
const message =
deliveryType === 'ON_SITE_PICKUP'
? `现场提货至少购买 ${minSale}${unit}`
: deliveryType === 'LOCAL'
? `同城配送至少购买 ${minSale}${unit}`
: `跨城配送至少购买 ${minSale}${unit}`,
};
: `跨城配送至少购买 ${minSale}${unit}`;
return { ok: false, message };
}
return { ok: true };
}