Commit 47fc03fc by 张立顺

fix: close low-price catalog filter gaps

1 parent 50d7c8c2
...@@ -2049,12 +2049,13 @@ Page({ ...@@ -2049,12 +2049,13 @@ Page({
if (code == 0) { if (code == 0) {
const { first, orderObj } = this.data const { first, orderObj } = this.data
let subOrderList = orderObj?.subOrderList let subOrderList = orderObj?.subOrderList
data.forEach(val => { const rawCatalogData = Array.isArray(data) ? data : []
rawCatalogData.forEach(val => {
normalizeMarketingMetaOnItem(val) normalizeMarketingMetaOnItem(val)
preprocessSelectOrderCatalogItem(val) preprocessSelectOrderCatalogItem(val)
}) })
const catalogData = filterLowPriceSelectOrderCatalog( const catalogData = filterLowPriceSelectOrderCatalog(
data, rawCatalogData,
orderObj?.lowPriceFlag orderObj?.lowPriceFlag
) )
const classified = classifySelectOrderCatalog(catalogData, { const classified = classifySelectOrderCatalog(catalogData, {
......
...@@ -8,6 +8,56 @@ function hasMinPriceValidation(goods) { ...@@ -8,6 +8,56 @@ function hasMinPriceValidation(goods) {
return goods?.validMinSellPrice === 'Y' return goods?.validMinSellPrice === 'Y'
} }
function filterSpecificationGoodsMap(specificationGoodsMap) {
return Object.entries(specificationGoodsMap).reduce(
(filteredMap, [specificationKey, goodsList]) => {
const enabledGoodsList = Array.isArray(goodsList)
? goodsList.filter(hasMinPriceValidation)
: []
if (enabledGoodsList.length) {
filteredMap[specificationKey] = enabledGoodsList
}
return filteredMap
},
{},
)
}
function filterSpecificationList(specificationList, specificationKeys) {
if (!Array.isArray(specificationList)) return specificationList
const allowedValuesByIndex = specificationKeys.reduce(
(allowedValues, specificationKey) => {
specificationKey.split(';~;').forEach((value, index) => {
allowedValues[index] ??= new Set()
allowedValues[index].add(value)
})
return allowedValues
},
[],
)
return specificationList.map((specification, index) => {
const allowedValues = allowedValuesByIndex[index] || new Set()
const filteredSpecification = { ...specification }
if (Array.isArray(specification.specificationSelectValueList)) {
filteredSpecification.specificationSelectValueList =
specification.specificationSelectValueList.filter((value) =>
allowedValues.has(value),
)
}
if (Array.isArray(specification.specificationValueList)) {
filteredSpecification.specificationValueList =
specification.specificationValueList.filter((value) =>
allowedValues.has(value?.specificationValue),
)
}
return filteredSpecification
})
}
/** /**
* 低价单只展示已开启最低价格校验的商品。 * 低价单只展示已开启最低价格校验的商品。
* @param {Array<object>} items * @param {Array<object>} items
...@@ -29,10 +79,36 @@ export function filterLowPriceSelectOrderCatalog(items, lowPriceFlag) { ...@@ -29,10 +79,36 @@ export function filterLowPriceSelectOrderCatalog(items, lowPriceFlag) {
const skuGoodsList = Array.isArray(root?.skuGoodsList) const skuGoodsList = Array.isArray(root?.skuGoodsList)
? root.skuGoodsList.filter(hasMinPriceValidation) ? root.skuGoodsList.filter(hasMinPriceValidation)
: [] : []
if (skuGoodsList.length) { const hasSpecificationGoodsMap =
root?.specificationGoodsMap &&
typeof root.specificationGoodsMap === 'object' &&
!Array.isArray(root.specificationGoodsMap)
const specificationGoodsMap = hasSpecificationGoodsMap
? filterSpecificationGoodsMap(root.specificationGoodsMap)
: undefined
const specificationKeys = specificationGoodsMap
? Object.keys(specificationGoodsMap)
: []
const hasSelectableSku = hasSpecificationGoodsMap
? specificationKeys.length > 0
: skuGoodsList.length > 0
if (hasSelectableSku) {
result.push({ result.push({
...item, ...item,
multipleSpecificationsGoods: { ...root, skuGoodsList } multipleSpecificationsGoods: {
...root,
skuGoodsList,
...(hasSpecificationGoodsMap
? {
specificationGoodsMap,
specificationList: filterSpecificationList(
root.specificationList,
specificationKeys,
),
}
: {}),
},
}) })
} }
return result return result
......
...@@ -6,15 +6,15 @@ function single(testId, validMinSellPrice) { ...@@ -6,15 +6,15 @@ function single(testId, validMinSellPrice) {
return { return {
testId, testId,
goodsFlag: 1, goodsFlag: 1,
goods: { validMinSellPrice } goods: { validMinSellPrice },
} }
} }
function multi(testId, skuGoodsList) { function multi(testId, skuGoodsList, extra = {}) {
return { return {
testId, testId,
goodsFlag: 2, goodsFlag: 2,
multipleSpecificationsGoods: { skuGoodsList } multipleSpecificationsGoods: { skuGoodsList, ...extra },
} }
} }
...@@ -22,7 +22,7 @@ function packageItem(testId, goodsList) { ...@@ -22,7 +22,7 @@ function packageItem(testId, goodsList) {
return { return {
testId, testId,
goodsFlag: 3, goodsFlag: 3,
packageGoods: { goodsList } packageGoods: { goodsList },
} }
} }
...@@ -34,33 +34,95 @@ describe('filterLowPriceSelectOrderCatalog', () => { ...@@ -34,33 +34,95 @@ describe('filterLowPriceSelectOrderCatalog', () => {
assert.deepEqual( assert.deepEqual(
filterLowPriceSelectOrderCatalog([enabled, disabled, missing], 1), filterLowPriceSelectOrderCatalog([enabled, disabled, missing], 1),
[enabled] [enabled],
) )
}) })
it('keeps only enabled SKUs without mutating the original multi-spec item', () => { it('keeps only enabled SKUs without mutating the original multi-spec item', () => {
const enabledSku = { id: 'sku-enabled', validMinSellPrice: 'Y' } const enabledSku = { id: 'sku-enabled', validMinSellPrice: 'Y' }
const disabledSku = { id: 'sku-disabled', validMinSellPrice: 'N' } const disabledSku = { id: 'sku-disabled', validMinSellPrice: 'N' }
const item = multi('mixed-multi', [enabledSku, disabledSku]) const item = multi('mixed-multi', [enabledSku, disabledSku], {
specificationGoodsMap: {
red: [enabledSku],
blue: [disabledSku],
mixed: [enabledSku, disabledSku],
},
specificationList: [
{
specificationSelectValueList: ['red', 'blue'],
specificationValueList: [
{ show: true, specificationValue: 'red' },
{ show: true, specificationValue: 'blue' },
],
},
],
})
const [filtered] = filterLowPriceSelectOrderCatalog([item], '1') const [filtered] = filterLowPriceSelectOrderCatalog([item], '1')
assert.notEqual(filtered, item) assert.notEqual(filtered, item)
assert.notEqual( assert.notEqual(
filtered.multipleSpecificationsGoods, filtered.multipleSpecificationsGoods,
item.multipleSpecificationsGoods item.multipleSpecificationsGoods,
)
assert.deepEqual(
filtered.multipleSpecificationsGoods.skuGoodsList.map((sku) => sku.id),
['sku-enabled'],
) )
assert.deepEqual( assert.deepEqual(
filtered.multipleSpecificationsGoods.skuGoodsList.map(sku => sku.id), Object.keys(filtered.multipleSpecificationsGoods.specificationGoodsMap),
['sku-enabled'] ['red', 'mixed'],
)
assert.deepEqual(
filtered.multipleSpecificationsGoods.specificationGoodsMap.mixed.map(
(sku) => sku.id,
),
['sku-enabled'],
)
assert.deepEqual(
filtered.multipleSpecificationsGoods.specificationList[0]
.specificationSelectValueList,
['red'],
) )
assert.equal(item.multipleSpecificationsGoods.skuGoodsList.length, 2) assert.equal(item.multipleSpecificationsGoods.skuGoodsList.length, 2)
assert.equal(
item.multipleSpecificationsGoods.specificationGoodsMap.blue.length,
1,
)
assert.deepEqual(
item.multipleSpecificationsGoods.specificationList[0]
.specificationSelectValueList,
['red', 'blue'],
)
})
it('uses the specification map as the selectable SKU source', () => {
const enabledSku = { id: 'map-enabled', validMinSellPrice: 'Y' }
const enabled = multi('map-enabled-multi', [], {
specificationGoodsMap: { red: [enabledSku] },
})
const disabled = multi('map-disabled-multi', [enabledSku], {
specificationGoodsMap: {
blue: [{ id: 'map-disabled', validMinSellPrice: 'N' }],
},
})
const filtered = filterLowPriceSelectOrderCatalog([enabled, disabled], 1)
assert.deepEqual(
filtered.map((item) => item.testId),
['map-enabled-multi'],
)
assert.equal(
filtered[0].multipleSpecificationsGoods.specificationGoodsMap.red[0].id,
'map-enabled',
)
}) })
it('hides a multi-spec item when no enabled SKU remains', () => { it('hides a multi-spec item when no enabled SKU remains', () => {
const item = multi('disabled-multi', [ const item = multi('disabled-multi', [
{ id: 'sku-disabled', validMinSellPrice: 'N' }, { id: 'sku-disabled', validMinSellPrice: 'N' },
{ id: 'sku-missing' } { id: 'sku-missing' },
]) ])
assert.deepEqual(filterLowPriceSelectOrderCatalog([item], 1), []) assert.deepEqual(filterLowPriceSelectOrderCatalog([item], 1), [])
...@@ -69,24 +131,21 @@ describe('filterLowPriceSelectOrderCatalog', () => { ...@@ -69,24 +131,21 @@ describe('filterLowPriceSelectOrderCatalog', () => {
it('keeps a package only when its non-empty child list is fully enabled', () => { it('keeps a package only when its non-empty child list is fully enabled', () => {
const allEnabled = packageItem('enabled-package', [ const allEnabled = packageItem('enabled-package', [
{ validMinSellPrice: 'Y' }, { validMinSellPrice: 'Y' },
{ validMinSellPrice: 'Y' } { validMinSellPrice: 'Y' },
]) ])
const mixed = packageItem('mixed-package', [ const mixed = packageItem('mixed-package', [
{ validMinSellPrice: 'Y' }, { validMinSellPrice: 'Y' },
{ validMinSellPrice: 'N' } { validMinSellPrice: 'N' },
]) ])
const missing = packageItem('missing-package', [ const missing = packageItem('missing-package', [
{ validMinSellPrice: 'Y' }, { validMinSellPrice: 'Y' },
{} {},
]) ])
const empty = packageItem('empty-package', []) const empty = packageItem('empty-package', [])
assert.deepEqual( assert.deepEqual(
filterLowPriceSelectOrderCatalog( filterLowPriceSelectOrderCatalog([allEnabled, mixed, missing, empty], 1),
[allEnabled, mixed, missing, empty], [allEnabled],
1
),
[allEnabled]
) )
}) })
...@@ -96,9 +155,9 @@ describe('filterLowPriceSelectOrderCatalog', () => { ...@@ -96,9 +155,9 @@ describe('filterLowPriceSelectOrderCatalog', () => {
multi('disabled-multi', [{ validMinSellPrice: 'N' }]), multi('disabled-multi', [{ validMinSellPrice: 'N' }]),
packageItem('mixed-package', [ packageItem('mixed-package', [
{ validMinSellPrice: 'Y' }, { validMinSellPrice: 'Y' },
{ validMinSellPrice: 'N' } { validMinSellPrice: 'N' },
]), ]),
{ testId: 'unknown', goodsFlag: 99 } { testId: 'unknown', goodsFlag: 99 },
] ]
assert.equal(filterLowPriceSelectOrderCatalog(fullCatalog, 0), fullCatalog) assert.equal(filterLowPriceSelectOrderCatalog(fullCatalog, 0), fullCatalog)
...@@ -108,7 +167,7 @@ describe('filterLowPriceSelectOrderCatalog', () => { ...@@ -108,7 +167,7 @@ describe('filterLowPriceSelectOrderCatalog', () => {
assert.deepEqual(filterLowPriceSelectOrderCatalog(null, 1), []) assert.deepEqual(filterLowPriceSelectOrderCatalog(null, 1), [])
assert.deepEqual( assert.deepEqual(
filterLowPriceSelectOrderCatalog([{ goodsFlag: 99 }], 1), filterLowPriceSelectOrderCatalog([{ goodsFlag: 99 }], 1),
[] [],
) )
}) })
}) })
...@@ -4,28 +4,30 @@ import { describe, it } from 'node:test' ...@@ -4,28 +4,30 @@ import { describe, it } from 'node:test'
register( register(
'../../../../tests/work-order-migration/shims/create-order-context-loader.mjs', '../../../../tests/work-order-migration/shims/create-order-context-loader.mjs',
import.meta.url import.meta.url,
) )
async function loadPageConfig() { async function loadPageConfig() {
let pageConfig let pageConfig
globalThis.getApp = () => ({ globalThis.getApp = () => ({
globalData: { positionTypeList: [], configInfo: {} } globalData: { positionTypeList: [], configInfo: {} },
}) })
globalThis.Page = config => { globalThis.Page = (config) => {
pageConfig = config pageConfig = config
} }
globalThis.wx = { globalThis.wx = {
getAccountInfoSync: () => ({ getAccountInfoSync: () => ({
miniProgram: { appId: 'test-app-id', envVersion: 'develop' } miniProgram: { appId: 'test-app-id', envVersion: 'develop' },
}), }),
getSystemInfoSync: () => ({ platform: 'devtools' }), getSystemInfoSync: () => ({ platform: 'devtools' }),
getStorageSync: () => '', getStorageSync: () => '',
hideLoading: () => {}, hideLoading: () => {},
$util: { getUserInfo: () => ({}) } $util: { getUserInfo: () => ({}) },
} }
await import(`../createOrder.js?catalog-filter=${Date.now()}-${Math.random()}`) await import(
`../createOrder.js?catalog-filter=${Date.now()}-${Math.random()}`
)
return pageConfig return pageConfig
} }
...@@ -38,8 +40,8 @@ function createCatalog() { ...@@ -38,8 +40,8 @@ function createCatalog() {
merchantGoodsId: 'single-enabled', merchantGoodsId: 'single-enabled',
merchantGoodsPosition: 1, merchantGoodsPosition: 1,
validMinSellPrice: 'Y', validMinSellPrice: 'Y',
sellPrice: 80 sellPrice: 80,
} },
}, },
{ {
testId: 'disabled-single', testId: 'disabled-single',
...@@ -48,27 +50,50 @@ function createCatalog() { ...@@ -48,27 +50,50 @@ function createCatalog() {
merchantGoodsId: 'single-disabled', merchantGoodsId: 'single-disabled',
merchantGoodsPosition: 2, merchantGoodsPosition: 2,
validMinSellPrice: 'N', validMinSellPrice: 'N',
sellPrice: 90 sellPrice: 90,
} },
}, },
{ {
testId: 'mixed-multi', testId: 'mixed-multi',
goodsFlag: 2, goodsFlag: 2,
multipleSpecificationsGoods: { multipleSpecificationsGoods: {
specificationList: [], specificationList: [
{
specificationValueList: [
{ show: true, specificationValue: 'red' },
{ show: true, specificationValue: 'blue' },
],
},
],
skuGoodsList: [ skuGoodsList: [
{ {
id: 'sku-enabled', id: 'sku-enabled',
validMinSellPrice: 'Y', validMinSellPrice: 'Y',
sellPrice: 100 sellPrice: 100,
}, },
{ {
id: 'sku-disabled', id: 'sku-disabled',
validMinSellPrice: 'N', validMinSellPrice: 'N',
sellPrice: 110 sellPrice: 110,
} },
] ],
} specificationGoodsMap: {
red: [
{
id: 'sku-enabled',
validMinSellPrice: 'Y',
sellPrice: 100,
},
],
blue: [
{
id: 'sku-disabled',
validMinSellPrice: 'N',
sellPrice: 110,
},
],
},
},
}, },
{ {
testId: 'mixed-package', testId: 'mixed-package',
...@@ -76,10 +101,10 @@ function createCatalog() { ...@@ -76,10 +101,10 @@ function createCatalog() {
packageGoods: { packageGoods: {
goodsList: [ goodsList: [
{ validMinSellPrice: 'Y', sellPrice: 50 }, { validMinSellPrice: 'Y', sellPrice: 50 },
{ validMinSellPrice: 'N', sellPrice: 60 } { validMinSellPrice: 'N', sellPrice: 60 },
] ],
} },
} },
] ]
} }
...@@ -93,25 +118,29 @@ function createPage(pageConfig, lowPriceFlag) { ...@@ -93,25 +118,29 @@ function createPage(pageConfig, lowPriceFlag) {
lowPriceFlag, lowPriceFlag,
merchantId: 100, merchantId: 100,
sceneType: 1, sceneType: 1,
subOrderList: [] subOrderList: [],
} },
}, },
setData(patch, callback) { setData(patch, callback) {
Object.assign(this.data, patch) Object.assign(this.data, patch)
callback?.() callback?.()
}, },
async syncCreateOrderMarketingFlags() {}, async syncCreateOrderMarketingFlags() {},
getChooseList() {} getChooseList() {},
} }
} }
async function loadCatalog(pageConfig, lowPriceFlag) { async function loadCatalog(
pageConfig,
lowPriceFlag,
responseData = createCatalog(),
) {
globalThis.__WORK_ORDER_MOCK_API__ = { globalThis.__WORK_ORDER_MOCK_API__ = {
getSelectOrderGoodsList: () => ({ code: 0, data: createCatalog() }) getSelectOrderGoodsList: () => ({ code: 0, data: responseData }),
} }
const page = createPage(pageConfig, lowPriceFlag) const page = createPage(pageConfig, lowPriceFlag)
await pageConfig.getSelectOrderGoodsList.call(page, 100, 1) await pageConfig.getSelectOrderGoodsList.call(page, 100, 1)
await new Promise(resolve => setImmediate(resolve)) await new Promise((resolve) => setImmediate(resolve))
return page return page
} }
...@@ -120,15 +149,33 @@ describe('createOrder low-price catalog filtering', () => { ...@@ -120,15 +149,33 @@ describe('createOrder low-price catalog filtering', () => {
const pageConfig = await loadPageConfig() const pageConfig = await loadPageConfig()
const page = await loadCatalog(pageConfig, 1) const page = await loadCatalog(pageConfig, 1)
assert.deepEqual(page.data.goodsData.map(item => item.testId), [ assert.deepEqual(
'enabled-single', page.data.goodsData.map((item) => item.testId),
'mixed-multi' ['enabled-single', 'mixed-multi'],
]) )
assert.deepEqual( assert.deepEqual(
page.data.goodsData[1].multipleSpecificationsGoods.skuGoodsList.map( page.data.goodsData[1].multipleSpecificationsGoods.skuGoodsList.map(
item => item.id (item) => item.id,
),
['sku-enabled'],
)
assert.deepEqual(
Object.keys(
page.data.goodsData[1].multipleSpecificationsGoods
.specificationGoodsMap,
), ),
['sku-enabled'] ['red'],
)
assert.deepEqual(
page.data.goodsData[1].multipleSpecificationsGoods.specificationGoodsMap.red.map(
(item) => item.id,
),
['sku-enabled'],
)
assert.deepEqual(
page.data.goodsData[1].multipleSpecificationsGoods.specificationList[0]
.specificationSelectValueList,
['red'],
) )
assert.equal(page.data.frontGearArr.length, 1) assert.equal(page.data.frontGearArr.length, 1)
assert.equal(page.data.otherProduct.length, 1) assert.equal(page.data.otherProduct.length, 1)
...@@ -139,18 +186,27 @@ describe('createOrder low-price catalog filtering', () => { ...@@ -139,18 +186,27 @@ describe('createOrder low-price catalog filtering', () => {
const pageConfig = await loadPageConfig() const pageConfig = await loadPageConfig()
const page = await loadCatalog(pageConfig, 0) const page = await loadCatalog(pageConfig, 0)
assert.deepEqual(page.data.goodsData.map(item => item.testId), [ assert.deepEqual(
'enabled-single', page.data.goodsData.map((item) => item.testId),
'disabled-single', ['enabled-single', 'disabled-single', 'mixed-multi', 'mixed-package'],
'mixed-multi', )
'mixed-package'
])
assert.deepEqual( assert.deepEqual(
page.data.goodsData[2].multipleSpecificationsGoods.skuGoodsList.map( page.data.goodsData[2].multipleSpecificationsGoods.skuGoodsList.map(
item => item.id (item) => item.id,
), ),
['sku-enabled', 'sku-disabled'] ['sku-enabled', 'sku-disabled'],
) )
assert.equal(page.data.packageList.length, 1) assert.equal(page.data.packageList.length, 1)
}) })
it('treats a successful response with null data as an empty catalog', async () => {
const pageConfig = await loadPageConfig()
const page = await loadCatalog(pageConfig, 1, null)
assert.deepEqual(page.data.goodsData, [])
assert.deepEqual(page.data.copyGoodsData, [])
assert.equal(page.data.frontGearArr.length, 0)
assert.equal(page.data.otherProduct.length, 0)
assert.equal(page.data.packageList.length, 0)
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!