From 65330e76ba9dbac418875fcccc8d88828bb8da0d Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Fri, 11 Oct 2024 12:25:33 +0800 Subject: [PATCH] fix(region): fix filter error (#5390) * fix(region): fix filter error * test: add unit test --- .../filter-provider/__tests__/utiles.test.ts | 25 +++++++++++++++++++ .../core/client/src/filter-provider/utils.ts | 4 +++ 2 files changed, 29 insertions(+) diff --git a/packages/core/client/src/filter-provider/__tests__/utiles.test.ts b/packages/core/client/src/filter-provider/__tests__/utiles.test.ts index 1d6c886f9b..b1ddff1fe6 100644 --- a/packages/core/client/src/filter-provider/__tests__/utiles.test.ts +++ b/packages/core/client/src/filter-provider/__tests__/utiles.test.ts @@ -167,6 +167,8 @@ describe('transformToFilter', () => { if (name === `${collectionName}.field1`) return {}; if (name === `${collectionName}.field2`) return {}; if (name === `${collectionName}.field3`) return { target: 'targetCollection', targetKey: 'id' }; + if (name === `${collectionName}.chinaRegion`) + return { target: 'chinaRegions', targetKey: 'code', interface: 'chinaRegion' }; return {}; }); @@ -242,4 +244,27 @@ describe('transformToFilter', () => { expect(filter).toEqual(expectedFilter); }); + + it('should handle chinaRegion', () => { + const values = { + chinaRegion: [ + { + code: '1', + }, + { + code: '2', + }, + { + code: '3', + }, + ], + }; + const expectedFilter = { + $and: [{ 'chinaRegion.code': { $eq: '3' } }], + }; + + const filter = transformToFilter(values, operators, getCollectionJoinField, collectionName); + + expect(filter).toEqual(expectedFilter); + }); }); diff --git a/packages/core/client/src/filter-provider/utils.ts b/packages/core/client/src/filter-provider/utils.ts index 627635367c..eba79e2667 100644 --- a/packages/core/client/src/filter-provider/utils.ts +++ b/packages/core/client/src/filter-provider/utils.ts @@ -142,6 +142,10 @@ export const transformToFilter = ( if (collectionField?.target) { value = getValuesByPath(value, collectionField.targetKey || 'id'); key = `${key}.${collectionField.targetKey || 'id'}`; + + if (collectionField?.interface === 'chinaRegion') { + value = _.last(value); + } } if (!value && value !== 0 && value !== false) {