forked from trent_larson/crowd-funder-for-time-pwa
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
This commit is contained in:
@@ -118,6 +118,7 @@ onNumNewOffersToUserChange(newValue: number, oldValue: number) {
|
||||
**Status**: 23 components successfully migrated. 11 components remaining. API layer ready for systematic updates.
|
||||
|
||||
### Phase 4: Testing 🟡 PARTIALLY STARTED
|
||||
|
||||
- [x] Test Web platform (verified working)
|
||||
- [ ] Test Electron platform
|
||||
- [ ] Test iOS platform
|
||||
|
||||
@@ -608,126 +608,54 @@ export const PlatformServiceMixin = {
|
||||
*/
|
||||
async $getActiveIdentity(): Promise<{ activeDid: string }> {
|
||||
try {
|
||||
logger.debug(
|
||||
"[PlatformServiceMixin] $getActiveIdentity() called - API layer verification",
|
||||
);
|
||||
|
||||
logger.debug(
|
||||
"[PlatformServiceMixin] Getting active identity from active_identity table",
|
||||
);
|
||||
const result = await this.$dbQuery(
|
||||
"SELECT activeDid FROM active_identity WHERE id = 1",
|
||||
);
|
||||
|
||||
if (result?.values?.length) {
|
||||
const activeDid = result.values[0][0] as string;
|
||||
logger.debug("[PlatformServiceMixin] Active identity found:", {
|
||||
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 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
|
||||
if (activeDid) {
|
||||
const accountExists = await this.$dbQuery(
|
||||
"SELECT did FROM accounts WHERE did = ?",
|
||||
[activeDid],
|
||||
);
|
||||
|
||||
if (accountExists?.values?.length) {
|
||||
logger.debug(
|
||||
"[PlatformServiceMixin] Active identity validated in accounts",
|
||||
);
|
||||
return { activeDid };
|
||||
} 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: "" };
|
||||
}
|
||||
if (!result?.values?.length) {
|
||||
logger.warn("[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue");
|
||||
return { activeDid: "" };
|
||||
}
|
||||
|
||||
// Handle empty active_identity table - this indicates a migration issue
|
||||
// Instead of auto-fixing, we log the issue for user awareness
|
||||
logger.warn(
|
||||
"[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue",
|
||||
);
|
||||
const activeDid = result.values[0][0] as string | null;
|
||||
|
||||
// Check if there are any accounts available for user selection
|
||||
const availableAccounts = await this.$dbQuery(
|
||||
"SELECT did FROM accounts ORDER BY did LIMIT 5",
|
||||
);
|
||||
|
||||
if (availableAccounts?.values?.length) {
|
||||
const accountDids = availableAccounts.values.map(
|
||||
(row: SqlValue[]) => row[0] as string,
|
||||
// Handle null activeDid (initial state after migration) - auto-select first account
|
||||
if (activeDid === null) {
|
||||
const firstAccount = await this.$dbQuery(
|
||||
"SELECT did FROM accounts ORDER BY dateCreated, did LIMIT 1",
|
||||
);
|
||||
logger.debug(
|
||||
"[PlatformServiceMixin] Available accounts for user selection:",
|
||||
{ accountDids },
|
||||
);
|
||||
} else {
|
||||
logger.warn("[PlatformServiceMixin] No accounts found in database");
|
||||
|
||||
if (firstAccount?.values?.length) {
|
||||
const firstAccountDid = firstAccount.values[0][0] as string;
|
||||
await this.$dbExec(
|
||||
"UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1",
|
||||
[firstAccountDid],
|
||||
);
|
||||
return { activeDid: firstAccountDid };
|
||||
}
|
||||
|
||||
logger.warn("[PlatformServiceMixin] No accounts available for auto-selection");
|
||||
return { activeDid: "" };
|
||||
}
|
||||
|
||||
logger.debug(
|
||||
"[PlatformServiceMixin] No active identity found, returning empty",
|
||||
// Validate activeDid exists in accounts
|
||||
const accountExists = await this.$dbQuery(
|
||||
"SELECT did FROM accounts WHERE did = ?",
|
||||
[activeDid],
|
||||
);
|
||||
|
||||
if (accountExists?.values?.length) {
|
||||
return { activeDid };
|
||||
}
|
||||
|
||||
// Clear corrupted activeDid and return 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: "" };
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"[PlatformServiceMixin] Error getting active identity:",
|
||||
error,
|
||||
);
|
||||
logger.error("[PlatformServiceMixin] Error getting active identity:", error);
|
||||
return { activeDid: "" };
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user