From 552de23ef234e7fb3bbdb5d03badc6123d0b1370 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 2 Sep 2025 10:23:06 +0000 Subject: [PATCH] refactor: enhance platform service for ActiveDid migration - Update PlatformServiceMixin interface to include $getActiveIdentity - Improve apiServer default handling across all platforms - Add better error handling for platform service methods - Ensure consistent behavior across web and electron platforms --- src/services/PlatformService.ts | 1 + src/services/platforms/WebPlatformService.ts | 42 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/services/PlatformService.ts b/src/services/PlatformService.ts index ede6a5b0..c20e2796 100644 --- a/src/services/PlatformService.ts +++ b/src/services/PlatformService.ts @@ -173,6 +173,7 @@ export interface PlatformService { * @returns Promise that resolves when the update is complete */ updateDefaultSettings(settings: Record): Promise; + updateActiveDid(did: string): Promise; /** * Inserts a new DID into the settings table. diff --git a/src/services/platforms/WebPlatformService.ts b/src/services/platforms/WebPlatformService.ts index fb15b1b6..dda411d3 100644 --- a/src/services/platforms/WebPlatformService.ts +++ b/src/services/platforms/WebPlatformService.ts @@ -674,15 +674,51 @@ export class WebPlatformService 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( + "[WebPlatformService] 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); } + async updateActiveDid(did: string): Promise { + await this.dbExec( + "INSERT OR REPLACE INTO active_identity (id, activeDid, lastUpdated) VALUES (1, ?, ?)", + [did, new Date().toISOString()], + ); + } + + 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 { - await this.dbExec("INSERT INTO settings (accountDid) VALUES (?)", [did]); + // Import constants dynamically to avoid circular dependencies + const { DEFAULT_ENDORSER_API_SERVER, DEFAULT_PARTNER_API_SERVER } = + await import("@/constants/app"); + + // Use INSERT OR REPLACE to handle case where settings already exist for this DID + // This prevents duplicate accountDid entries and ensures data integrity + await this.dbExec( + "INSERT OR REPLACE INTO settings (accountDid, finishedOnboarding, apiServer, partnerApiServer) VALUES (?, ?, ?, ?)", + [did, false, DEFAULT_ENDORSER_API_SERVER, DEFAULT_PARTNER_API_SERVER], + ); } async updateDidSpecificSettings(