diff --git a/src/modules/metadata/Metadata.constants.ts b/src/modules/metadata/Metadata.constants.ts index 0e6b89c3..16e23526 100644 --- a/src/modules/metadata/Metadata.constants.ts +++ b/src/modules/metadata/Metadata.constants.ts @@ -160,6 +160,7 @@ export const GLOBAL_METADATA_KEYS: AppMetadataKeys[] = [ * 1. app metadata key prefix * 2. app metadata values * 3. app metadata keys + * 4. app metadata keys deletion * * @example * // changes: diff --git a/src/modules/metadata/Metadata.types.ts b/src/modules/metadata/Metadata.types.ts index dd34fc72..ee079d34 100644 --- a/src/modules/metadata/Metadata.types.ts +++ b/src/modules/metadata/Metadata.types.ts @@ -24,6 +24,7 @@ export interface IMetadataMigration { newValue: string | ((oldValue: string) => string); }[]; keys?: { oldKey: string; newKey: string }[]; + deleteKeys?: string[]; } export type Metadata = { diff --git a/src/modules/metadata/services/MetadataMigrations.ts b/src/modules/metadata/services/MetadataMigrations.ts index 4c7e2a46..a51c914f 100644 --- a/src/modules/metadata/services/MetadataMigrations.ts +++ b/src/modules/metadata/services/MetadataMigrations.ts @@ -132,6 +132,32 @@ const applyMetadataKeyMigration = (meta: Metadata, migration: IMetadataMigration return migratedMetadata; }; +const applyMetadataDeleteKeysMigration = ( + meta: Metadata, + migration: IMetadataMigration, + appKeyPrefix: string, +): Metadata => { + const migratedMetadata: Metadata = {}; + + if (!migration.deleteKeys) { + return migratedMetadata; + } + + const appMetadata = getAppMetadataFrom(meta, undefined, appKeyPrefix); + + migration.deleteKeys.forEach((keyToDelete) => { + Object.keys(appMetadata).forEach((key) => { + if (key.endsWith(keyToDelete)) { + return; + } + + migratedMetadata[key] = appMetadata[key]; + }); + }); + + return migratedMetadata; +}; + const getOutdatedMetadataKeys = (metadata?: Metadata): AppMetadataKeys[] => { if (!metadata) { return []; @@ -146,6 +172,11 @@ const getOutdatedMetadataKeys = (metadata?: Metadata): AppMetadataKeys[] => { return [...acc, oldPrefix]; }, [] as string[]); + const keyToDeleteInMigrations = METADATA_MIGRATIONS.reduce( + (acc, migration) => [...acc, ...(migration.deleteKeys ?? [])], + [] as string[], + ); + return Object.keys(metadata).filter((key) => { const appKeyPrefixOfKey = key.split('_')[0]; @@ -159,7 +190,13 @@ const getOutdatedMetadataKeys = (metadata?: Metadata): AppMetadataKeys[] => { return true; } - return !VALID_APP_METADATA_KEYS.includes(extractOriginalKey(key)); + const extractedKey = extractOriginalKey(key); + + if (keyToDeleteInMigrations.includes(extractedKey)) { + return true; + } + + return !VALID_APP_METADATA_KEYS.includes(extractedKey); }) as AppMetadataKeys[]; }; @@ -297,8 +334,13 @@ export const applyMetadataMigrations = ( migration, appKeyPrefixForMigration, ); + const metadataKeysDeletedMigrated = applyMetadataDeleteKeysMigration( + metadataKeysMigrated, + migration, + appKeyPrefixForMigration, + ); - migrationToMetadata.push([migrationId, metadataKeysMigrated]); + migrationToMetadata.push([migrationId, metadataKeysDeletedMigrated]); }); const migratedMetadata = migrationToMetadata.pop()?.[1] ?? meta;