{"record":{"id":"4fda4ca6c5579f0f","repo":"hcengineering/platform","slug":"accountnotfound-4fda4c","errorCode":"AccountNotFound","errorMessage":"AccountNotFound","messagePattern":"AccountNotFound","errorType":"error_code","errorClass":"PlatformError","httpStatus":404,"severity":"error","filePath":"server/account/src/utils.ts","lineNumber":875,"sourceCode":"  let role = await db.getWorkspaceRole(accountUuid, workspace.uuid)\n  if (role == null && extra?.admin === 'true') {\n    role = AccountRole.Admin\n  }\n  let account = await db.account.findOne({ uuid: accountUuid })\n\n  if ((role == null || account == null) && workspace.allowReadOnlyGuest) {\n    accountUuid = readOnlyGuestAccountUuid\n    role = await db.getWorkspaceRole(accountUuid, workspace.uuid)\n    account = await db.account.findOne({ uuid: accountUuid })\n  }\n\n  if (role == null) {\n    ctx.error('Not a member of the workspace being selected', { workspaceUrl, accountUuid })\n    throw new PlatformError(new Status(Severity.ERROR, platform.status.Forbidden, {}))\n  }\n\n  if (accountUuid !== systemAccountUuid && account == null) {\n    throw new PlatformError(new Status(Severity.ERROR, platform.status.AccountNotFound, {}))\n  }\n\n  if (accountUuid !== systemAccountUuid && meta !== undefined) {\n    void setTimezone(ctx, db, accountUuid, account, meta)\n  }\n\n  if (role === AccountRole.ReadOnlyGuest) {\n    if (extra == null) {\n      extra = {}\n    }\n    extra.readonly = 'true'\n  }\n\n  const wsStatus = await db.workspaceStatus.findOne({ workspaceUuid: workspace.uuid })\n\n  if (wsStatus != null) {\n    if (wsStatus.isDisabled && isActiveMode(wsStatus.mode)) {\n      ctx.error('Selecting a disabled workspace', { workspaceUrl, accountUuid })","sourceCodeStart":857,"sourceCodeEnd":893,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/server/account/src/utils.ts#L857-L893","documentation":"In selectWorkspace (server/account/src/utils.ts), after confirming the caller has a workspace role, the service loads the account record (db.account.findOne). If the selecting account is not the system account but no account document exists, AccountNotFound is thrown. The role lookup succeeded while the account row is missing, indicating an inconsistent account database.","triggerScenarios":"Calling selectWorkspace when db.account.findOne({ uuid: accountUuid }) returns null and accountUuid !== systemAccountUuid — e.g. the person/workspace role survived but the account record was deleted, a migration partially imported accounts, or the accountUuid in the token refers to an account from another database.","commonSituations":"Partial restore from backup (person records restored but accounts not), cross-region/region-merge setups where an account exists in one DB but roles were copied to another, manually deleted account documents while memberships remained.","solutions":["Recreate the missing account record for that UUID (e.g. re-register or insert via account admin tooling) so account.findOne succeeds.","Audit consistency between person, account and workspace-role collections; repair orphans by removing stale roles or restoring the account row.","Verify the token was issued by the same account database instance you are querying — connecting to the wrong region/DB yields missing accounts.","If the account is truly gone, log in again to get a token bound to an existing account."],"exampleFix":"// before: selecting with a token whose account vanished from the DB\nawait accountClient.selectWorkspace(oldToken, workspaceUrl) // AccountNotFound\n\n// after: re-authenticate to ensure account exists server-side\nconst fresh = await accountClient.login(email, password)\nawait accountClient.selectWorkspace(fresh.token, workspaceUrl)","handlingStrategy":"try-catch","validationCode":"// client-side: ensure token belongs to a live, logged-in account on this instance\nconst payload = decodeJwt(token)\nif (payload.account !== systemAccountUuid && !await accountClient.accountExists(payload.account)) {\n  throw new Error('Account record missing; re-login required')\n}","typeGuard":"function hasAccount(a: { uuid: string } | null | undefined): a is { uuid: string } {\n  return a != null\n}","tryCatchPattern":"try {\n  await accountClient.selectWorkspace(token, url)\n} catch (e) {\n  if ((e as PlatformError).status.code === platform.status.AccountNotFound) {\n    await session.logout()\n    await session.relogin() // token referenced an account that no longer exists\n  } else throw e\n}","preventionTips":["Re-login after database restores or migrations","Never reuse tokens across environments/regions with separate account DBs","Keep account and person collections consistent when running custom DB tooling","Monitor for orphaned workspace roles pointing at deleted accounts"],"tags":["account-service","data-consistency","database"],"backgroundTag":"account-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}