diff --git a/src/libs/util.ts b/src/libs/util.ts index c65f2f8a..652ce5b7 100644 --- a/src/libs/util.ts +++ b/src/libs/util.ts @@ -655,7 +655,38 @@ export async function saveNewIdentity( ]; await platformService.dbExec(sql, params); - await platformService.updateDefaultSettings({ activeDid: identity.did }); + // Set the new identity as active using Active Identity façade + // Check if we need to avoid legacy settings table (Phase C) + const FLAGS = await import("@/config/featureFlags"); + + if (!FLAGS.FLAGS.DROP_SETTINGS_ACTIVEDID) { + // Phase A/B: Update legacy settings table + await platformService.updateDefaultSettings({ activeDid: identity.did }); + } + + // Always update/insert into new active_identity table + const DEFAULT_SCOPE = "default"; + const existingRecord = await platformService.dbQuery( + "SELECT id FROM active_identity WHERE scope = ? LIMIT 1", + [DEFAULT_SCOPE], + ); + + if (existingRecord?.values?.length) { + // Update existing record + await platformService.dbExec( + `UPDATE active_identity + SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') + WHERE scope = ?`, + [identity.did, DEFAULT_SCOPE], + ); + } else { + // Insert new record + await platformService.dbExec( + `INSERT INTO active_identity (scope, active_did, updated_at) + VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`, + [DEFAULT_SCOPE, identity.did], + ); + } await platformService.insertDidSpecificSettings(identity.did); } catch (error) { @@ -714,7 +745,40 @@ export const registerSaveAndActivatePasskey = async ( ): Promise => { const account = await registerAndSavePasskey(keyName); const platformService = await getPlatformService(); - await platformService.updateDefaultSettings({ activeDid: account.did }); + + // Set the new account as active using Active Identity façade + // Check if we need to avoid legacy settings table (Phase C) + const FLAGS = await import("@/config/featureFlags"); + + if (!FLAGS.FLAGS.DROP_SETTINGS_ACTIVEDID) { + // Phase A/B: Update legacy settings table + await platformService.updateDefaultSettings({ activeDid: account.did }); + } + + // Always update/insert into new active_identity table + const DEFAULT_SCOPE = "default"; + const existingRecord = await platformService.dbQuery( + "SELECT id FROM active_identity WHERE scope = ? LIMIT 1", + [DEFAULT_SCOPE], + ); + + if (existingRecord?.values?.length) { + // Update existing record + await platformService.dbExec( + `UPDATE active_identity + SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') + WHERE scope = ?`, + [account.did, DEFAULT_SCOPE], + ); + } else { + // Insert new record + await platformService.dbExec( + `INSERT INTO active_identity (scope, active_did, updated_at) + VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`, + [DEFAULT_SCOPE, account.did], + ); + } + await platformService.updateDidSpecificSettings(account.did, { isRegistered: false, });