Browse Source

refactor: clean up $getActiveIdentity method and fix null handling

- Remove excessive debug logging statements
- Fix critical bug: cast activeDid as string | null instead of string
- Refactor to use early return pattern, reducing nesting from 4 to 2-3 levels
- Eliminate redundant logic and improve code readability
- Maintain all original functionality while simplifying flow
- Fix null activeDid case that was breaking app initialization
pull/188/head
Matthew Raymer 5 days ago
parent
commit
cf854d5054
  1. 1
      doc/activeDid-migration-plan.md
  2. 132
      src/utils/PlatformServiceMixin.ts

1
doc/activeDid-migration-plan.md

@ -118,6 +118,7 @@ onNumNewOffersToUserChange(newValue: number, oldValue: number) {
**Status**: 23 components successfully migrated. 11 components remaining. API layer ready for systematic updates. **Status**: 23 components successfully migrated. 11 components remaining. API layer ready for systematic updates.
### Phase 4: Testing 🟡 PARTIALLY STARTED ### Phase 4: Testing 🟡 PARTIALLY STARTED
- [x] Test Web platform (verified working) - [x] Test Web platform (verified working)
- [ ] Test Electron platform - [ ] Test Electron platform
- [ ] Test iOS platform - [ ] Test iOS platform

132
src/utils/PlatformServiceMixin.ts

@ -608,126 +608,54 @@ export const PlatformServiceMixin = {
*/ */
async $getActiveIdentity(): Promise<{ activeDid: string }> { async $getActiveIdentity(): Promise<{ activeDid: string }> {
try { try {
logger.debug(
"[PlatformServiceMixin] $getActiveIdentity() called - API layer verification",
);
logger.debug(
"[PlatformServiceMixin] Getting active identity from active_identity table",
);
const result = await this.$dbQuery( const result = await this.$dbQuery(
"SELECT activeDid FROM active_identity WHERE id = 1", "SELECT activeDid FROM active_identity WHERE id = 1",
); );
if (result?.values?.length) { if (!result?.values?.length) {
const activeDid = result.values[0][0] as string; logger.warn("[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue");
logger.debug("[PlatformServiceMixin] Active identity found:", { return { activeDid: "" };
activeDid, }
});
logger.debug(
"[PlatformServiceMixin] $getActiveIdentity(): activeDid resolved",
{ activeDid },
);
// Handle null activeDid (initial state after migration)
if (activeDid === null) {
logger.debug(
"[PlatformServiceMixin] Active identity is null (initial state), attempting auto-selection",
);
// Try to auto-select first available account
const availableAccounts = await this.$dbQuery(
"SELECT did FROM accounts ORDER BY dateCreated, did LIMIT 1",
);
if (availableAccounts?.values?.length) { const activeDid = result.values[0][0] as string | null;
const firstAccountDid = availableAccounts.values[0][0] as string;
logger.debug(
"[PlatformServiceMixin] Auto-selecting first account as active:",
{ firstAccountDid },
);
// Update active_identity table with the first account
await this.$dbExec(
"UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1",
[firstAccountDid],
);
logger.debug(
"[PlatformServiceMixin] Active identity auto-selected successfully",
);
return { activeDid: firstAccountDid };
} else {
logger.warn(
"[PlatformServiceMixin] No accounts available for auto-selection",
);
return { activeDid: "" };
}
}
// Validate activeDid exists in accounts // Handle null activeDid (initial state after migration) - auto-select first account
if (activeDid) { if (activeDid === null) {
const accountExists = await this.$dbQuery( const firstAccount = await this.$dbQuery(
"SELECT did FROM accounts WHERE did = ?", "SELECT did FROM accounts ORDER BY dateCreated, did LIMIT 1",
[activeDid], );
);
if (accountExists?.values?.length) { if (firstAccount?.values?.length) {
logger.debug( const firstAccountDid = firstAccount.values[0][0] as string;
"[PlatformServiceMixin] Active identity validated in accounts", await this.$dbExec(
); "UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1",
return { activeDid }; [firstAccountDid],
} else {
// Clear corrupted activeDid
logger.warn(
"[PlatformServiceMixin] Active identity not found in accounts, clearing",
);
await this.$dbExec(
"UPDATE active_identity SET activeDid = NULL, lastUpdated = datetime('now') WHERE id = 1",
);
return { activeDid: "" };
}
} else {
// activeDid is empty string, return it
logger.debug(
"[PlatformServiceMixin] Active identity is empty string, returning as-is",
); );
return { activeDid: "" }; return { activeDid: firstAccountDid };
} }
}
// Handle empty active_identity table - this indicates a migration issue logger.warn("[PlatformServiceMixin] No accounts available for auto-selection");
// Instead of auto-fixing, we log the issue for user awareness return { activeDid: "" };
logger.warn( }
"[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue",
);
// Check if there are any accounts available for user selection // Validate activeDid exists in accounts
const availableAccounts = await this.$dbQuery( const accountExists = await this.$dbQuery(
"SELECT did FROM accounts ORDER BY did LIMIT 5", "SELECT did FROM accounts WHERE did = ?",
[activeDid],
); );
if (availableAccounts?.values?.length) { if (accountExists?.values?.length) {
const accountDids = availableAccounts.values.map( return { activeDid };
(row: SqlValue[]) => row[0] as string,
);
logger.debug(
"[PlatformServiceMixin] Available accounts for user selection:",
{ accountDids },
);
} else {
logger.warn("[PlatformServiceMixin] No accounts found in database");
} }
logger.debug( // Clear corrupted activeDid and return empty
"[PlatformServiceMixin] No active identity found, returning empty", logger.warn("[PlatformServiceMixin] Active identity not found in accounts, clearing");
await this.$dbExec(
"UPDATE active_identity SET activeDid = NULL, lastUpdated = datetime('now') WHERE id = 1",
); );
return { activeDid: "" }; return { activeDid: "" };
} catch (error) { } catch (error) {
logger.error( logger.error("[PlatformServiceMixin] Error getting active identity:", error);
"[PlatformServiceMixin] Error getting active identity:",
error,
);
return { activeDid: "" }; return { activeDid: "" };
} }
}, },

Loading…
Cancel
Save