import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; import { Migrator } from "../migrator"; type ExpectedAccountType = { profile?: { everHadUserKey?: boolean; }; }; const USER_EVER_HAD_USER_KEY: KeyDefinitionLike = { key: "everHadUserKey", stateDefinition: { name: "crypto", }, }; export class EverHadUserKeyMigrator extends Migrator<9, 10> { async migrate(helper: MigrationHelper): Promise { const accounts = await helper.getAccounts(); async function migrateAccount(userId: string, account: ExpectedAccountType): Promise { const value = account?.profile?.everHadUserKey; await helper.setToUser(userId, USER_EVER_HAD_USER_KEY, value ?? false); if (value != null) { delete account.profile.everHadUserKey; } await helper.set(userId, account); } await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]); } async rollback(helper: MigrationHelper): Promise { const accounts = await helper.getAccounts(); async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise { const value = await helper.getFromUser(userId, USER_EVER_HAD_USER_KEY); if (account) { account.profile = Object.assign(account.profile ?? {}, { everHadUserKey: value, }); await helper.set(userId, account); } await helper.setToUser(userId, USER_EVER_HAD_USER_KEY, null); } await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]); } }