From 7432525f4cdce61ea41cef4df603e95c20070b87 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Thu, 2 Oct 2025 06:29:56 +0000 Subject: [PATCH] refactor(services): align Capacitor and Web platform services with active_identity architecture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update CapacitorPlatformService.updateDefaultSettings() to use active_identity table instead of hard-coded id=1 - Update CapacitorPlatformService.retrieveSettingsForActiveAccount() to query by accountDid from active_identity - Add getActiveIdentity() method to CapacitorPlatformService for consistency with WebPlatformService - Update WebPlatformService.retrieveSettingsForActiveAccount() to match CapacitorPlatformService pattern - Both services now consistently use active_identity table instead of legacy MASTER_SETTINGS_KEY approach - Maintains backward compatibility with databaseUtil.ts for PWA migration support Technical details: - CapacitorPlatformService: Fixed hard-coded WHERE id = 1 → WHERE accountDid = ? - WebPlatformService: Fixed retrieval pattern to match new architecture - Platform services now aligned with migration 004 active_identity table schema - databaseUtil.ts remains unchanged for PWA-to-SQLite migration bridge --- .../platforms/CapacitorPlatformService.ts | 38 ++++++++++++++++--- src/services/platforms/WebPlatformService.ts | 14 +++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/services/platforms/CapacitorPlatformService.ts b/src/services/platforms/CapacitorPlatformService.ts index 2db74656..57df0f9a 100644 --- a/src/services/platforms/CapacitorPlatformService.ts +++ b/src/services/platforms/CapacitorPlatformService.ts @@ -1343,10 +1343,21 @@ export class CapacitorPlatformService implements PlatformService { async updateDefaultSettings( settings: Record, ): Promise { + // Get current active DID and update that identity's settings + const activeIdentity = await this.getActiveIdentity(); + const activeDid = activeIdentity.activeDid; + + if (!activeDid) { + logger.warn( + "[CapacitorPlatformService] No active DID found, cannot update default settings", + ); + return; + } + const keys = Object.keys(settings); const setClause = keys.map((key) => `${key} = ?`).join(", "); - const sql = `UPDATE settings SET ${setClause} WHERE id = 1`; - const params = keys.map((key) => settings[key]); + const sql = `UPDATE settings SET ${setClause} WHERE accountDid = ?`; + const params = [...keys.map((key) => settings[key]), activeDid]; await this.dbExec(sql, params); } @@ -1357,6 +1368,15 @@ export class CapacitorPlatformService implements PlatformService { ); } + async getActiveIdentity(): Promise<{ activeDid: string }> { + const result = await this.dbQuery( + "SELECT activeDid FROM active_identity WHERE id = 1", + ); + return { + activeDid: (result?.values?.[0]?.[0] as string) || "", + }; + } + async insertNewDidIntoSettings(did: string): Promise { // Import constants dynamically to avoid circular dependencies const { DEFAULT_ENDORSER_API_SERVER, DEFAULT_PARTNER_API_SERVER } = @@ -1385,7 +1405,15 @@ export class CapacitorPlatformService implements PlatformService { string, unknown > | null> { - const result = await this.dbQuery("SELECT * FROM settings WHERE id = 1"); + // Get current active DID from active_identity table + const activeIdentity = await this.getActiveIdentity(); + const activeDid = activeIdentity.activeDid; + + if (!activeDid) { + return null; + } + + const result = await this.dbQuery("SELECT * FROM settings WHERE accountDid = ?", [activeDid]); if (result?.values?.[0]) { // Convert the row to an object const row = result.values[0]; @@ -1393,8 +1421,8 @@ export class CapacitorPlatformService implements PlatformService { const settings: Record = {}; columns.forEach((column, index) => { - if (column !== "id") { - // Exclude the id column + if (column !== "id" && column !== "accountDid") { + // Exclude the id and accountDid columns settings[column] = row[index]; } }); diff --git a/src/services/platforms/WebPlatformService.ts b/src/services/platforms/WebPlatformService.ts index 3d8248f5..944bbe80 100644 --- a/src/services/platforms/WebPlatformService.ts +++ b/src/services/platforms/WebPlatformService.ts @@ -753,7 +753,15 @@ export class WebPlatformService implements PlatformService { string, unknown > | null> { - const result = await this.dbQuery("SELECT * FROM settings WHERE id = 1"); + // Get current active DID from active_identity table + const activeIdentity = await this.getActiveIdentity(); + const activeDid = activeIdentity.activeDid; + + if (!activeDid) { + return null; + } + + const result = await this.dbQuery("SELECT * FROM settings WHERE accountDid = ?", [activeDid]); if (result?.values?.[0]) { // Convert the row to an object const row = result.values[0]; @@ -761,8 +769,8 @@ export class WebPlatformService implements PlatformService { const settings: Record = {}; columns.forEach((column, index) => { - if (column !== "id") { - // Exclude the id column + if (column !== "id" && column !== "accountDid") { + // Exclude the id and accountDid columns settings[column] = row[index]; } });