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:
@@ -191,7 +191,9 @@ export class CollectionStore extends Store {
|
|||||||
id: legacyCollectionInfo.id,
|
id: legacyCollectionInfo.id,
|
||||||
name: legacyCollectionInfo.name,
|
name: legacyCollectionInfo.name,
|
||||||
rules: {
|
rules: {
|
||||||
filters: this.migrateFilterList(legacyCollectionInfo.filterList),
|
filters: legacyCollectionInfo.filterList
|
||||||
|
? this.migrateFilterList(legacyCollectionInfo.filterList)
|
||||||
|
: [],
|
||||||
},
|
},
|
||||||
allowList: legacyCollectionInfo.allowList,
|
allowList: legacyCollectionInfo.allowList,
|
||||||
};
|
};
|
||||||
@@ -200,104 +202,112 @@ export class CollectionStore extends Store {
|
|||||||
migrateFilterList(
|
migrateFilterList(
|
||||||
filterList: LegacyCollectionInfo['filterList']
|
filterList: LegacyCollectionInfo['filterList']
|
||||||
): FilterParams[] {
|
): FilterParams[] {
|
||||||
return filterList.map(filter => {
|
try {
|
||||||
const leftValue = filter.left.name;
|
return filterList.map(filter => {
|
||||||
const method = filter.funcName;
|
const leftValue = filter.left.name;
|
||||||
const args = filter.args.filter(arg => !!arg).map(arg => arg.value);
|
const method = filter.funcName;
|
||||||
const arg0 = args[0];
|
const args = filter.args.filter(arg => !!arg).map(arg => arg.value);
|
||||||
if (leftValue === 'Created' || leftValue === 'Updated') {
|
const arg0 = args[0];
|
||||||
const key = leftValue === 'Created' ? 'createdAt' : 'updatedAt';
|
if (leftValue === 'Created' || leftValue === 'Updated') {
|
||||||
if (method === 'after' && typeof arg0 === 'number') {
|
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 {
|
return {
|
||||||
type: 'system',
|
type: 'system',
|
||||||
key,
|
key: 'shared',
|
||||||
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',
|
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 {
|
return {
|
||||||
type: 'unknown',
|
type: 'unknown',
|
||||||
key: 'unknown',
|
key: 'unknown',
|
||||||
method: 'unknown',
|
method: 'unknown',
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to migrate filter list', err);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user