fix(core): fix migrate filter list error (#13022)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
EYHN
2025-07-04 14:12:21 +08:00
committed by GitHub
parent 5a49d5cd24
commit f961d9986f
@@ -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 [];
}
}
}