From f961d9986fe518b1781e57b0d1ad28475877d687 Mon Sep 17 00:00:00 2001 From: EYHN Date: Fri, 4 Jul 2025 14:12:21 +0800 Subject: [PATCH] fix(core): fix migrate filter list error (#13022) ## Summary by CodeRabbit * **Bug Fixes** * Improved error handling for collection filter migrations, reducing the chance of failures affecting filter lists. * **New Features** * Expanded support for filter conditions on the "Tags" field, including options like "is empty," "is not empty," "contains all," and more. * **Enhancements** * Improved handling of "Is Favourited" and "Is Public" filters for more consistent results. --- .../modules/collection/stores/collection.ts | 198 +++++++++--------- 1 file changed, 104 insertions(+), 94 deletions(-) diff --git a/packages/frontend/core/src/modules/collection/stores/collection.ts b/packages/frontend/core/src/modules/collection/stores/collection.ts index 58277c90bc..d2c6d5262f 100644 --- a/packages/frontend/core/src/modules/collection/stores/collection.ts +++ b/packages/frontend/core/src/modules/collection/stores/collection.ts @@ -191,7 +191,9 @@ export class CollectionStore extends Store { id: legacyCollectionInfo.id, name: legacyCollectionInfo.name, rules: { - filters: this.migrateFilterList(legacyCollectionInfo.filterList), + filters: legacyCollectionInfo.filterList + ? this.migrateFilterList(legacyCollectionInfo.filterList) + : [], }, allowList: legacyCollectionInfo.allowList, }; @@ -200,104 +202,112 @@ export class CollectionStore extends Store { migrateFilterList( filterList: LegacyCollectionInfo['filterList'] ): FilterParams[] { - return filterList.map(filter => { - const leftValue = filter.left.name; - const method = filter.funcName; - const args = filter.args.filter(arg => !!arg).map(arg => arg.value); - const arg0 = args[0]; - if (leftValue === 'Created' || leftValue === 'Updated') { - const key = leftValue === 'Created' ? 'createdAt' : 'updatedAt'; - if (method === 'after' && typeof arg0 === 'number') { + try { + return filterList.map(filter => { + const leftValue = filter.left.name; + const method = filter.funcName; + const args = filter.args.filter(arg => !!arg).map(arg => arg.value); + const arg0 = args[0]; + if (leftValue === 'Created' || leftValue === 'Updated') { + const key = leftValue === 'Created' ? 'createdAt' : 'updatedAt'; + if (method === 'after' && typeof arg0 === 'number') { + return { + type: 'system', + key, + method: 'after', + value: dayjs(arg0).format('YYYY-MM-DD'), + }; + } else if (method === 'before' && typeof arg0 === 'number') { + return { + type: 'system', + key, + method: 'before', + value: dayjs(arg0).format('YYYY-MM-DD'), + }; + } else if (method === 'last' && typeof arg0 === 'number') { + return { + type: 'system', + key, + method: 'last', + value: dayjs().subtract(arg0, 'day').format('YYYY-MM-DD'), + }; + } + } else if (leftValue === 'Is Favourited') { + if (method === 'is') { + const value = arg0 ? 'true' : 'false'; + return { + type: 'system', + key: 'favorite', + method: 'is', + value, + }; + } + } else if (leftValue === 'Tags') { + if (method === 'is not empty') { + return { + type: 'system', + key: 'tags', + method: 'is-not-empty', + }; + } else if (method === 'is empty') { + return { + type: 'system', + key: 'tags', + method: 'is-empty', + }; + } else if (method === 'contains all' && Array.isArray(arg0)) { + return { + type: 'system', + key: 'tags', + method: 'include-all', + value: arg0.join(','), + }; + } else if (method === 'contains one of' && Array.isArray(arg0)) { + return { + type: 'system', + key: 'tags', + method: 'include-any-of', + value: arg0.join(','), + }; + } else if ( + method === 'does not contains all' && + Array.isArray(arg0) + ) { + return { + type: 'system', + key: 'tags', + method: 'not-include-all', + value: arg0.join(','), + }; + } else if ( + method === 'does not contains one of' && + Array.isArray(arg0) + ) { + return { + type: 'system', + key: 'tags', + method: 'not-include-any-of', + value: arg0.join(','), + }; + } + } else if (leftValue === 'Is Public' && method === 'is') { return { type: 'system', - key, - method: 'after', - value: dayjs(arg0).format('YYYY-MM-DD'), - }; - } else if (method === 'before' && typeof arg0 === 'number') { - return { - type: 'system', - key, - method: 'before', - value: dayjs(arg0).format('YYYY-MM-DD'), - }; - } else if (method === 'last' && typeof arg0 === 'number') { - return { - type: 'system', - key, - method: 'last', - value: dayjs().subtract(arg0, 'day').format('YYYY-MM-DD'), - }; - } - } else if (leftValue === 'Is Favourited') { - if (method === 'is') { - const value = arg0 ? 'true' : 'false'; - return { - type: 'system', - key: 'favorite', + key: 'shared', method: 'is', - value, + value: arg0 ? 'true' : 'false', }; } - } else if (leftValue === 'Tags') { - if (method === 'is not empty') { - return { - type: 'system', - key: 'tags', - method: 'is-not-empty', - }; - } else if (method === 'is empty') { - return { - type: 'system', - key: 'tags', - method: 'is-empty', - }; - } else if (method === 'contains all' && Array.isArray(arg0)) { - return { - type: 'system', - key: 'tags', - method: 'include-all', - value: arg0.join(','), - }; - } else if (method === 'contains one of' && Array.isArray(arg0)) { - return { - type: 'system', - key: 'tags', - method: 'include-any-of', - value: arg0.join(','), - }; - } else if (method === 'does not contains all' && Array.isArray(arg0)) { - return { - type: 'system', - key: 'tags', - method: 'not-include-all', - value: arg0.join(','), - }; - } else if ( - method === 'does not contains one of' && - Array.isArray(arg0) - ) { - return { - type: 'system', - key: 'tags', - method: 'not-include-any-of', - value: arg0.join(','), - }; - } - } else if (leftValue === 'Is Public' && method === 'is') { - return { - type: 'system', - key: 'shared', - method: 'is', - value: arg0 ? 'true' : 'false', - }; - } - return { - type: 'unknown', - key: 'unknown', - method: 'unknown', - }; - }); + return { + type: 'unknown', + key: 'unknown', + method: 'unknown', + }; + }); + } catch (err) { + console.error('Failed to migrate filter list', err); + return []; + } } }