{"record":{"id":"e737006ef3d6ede6","repo":"calcom/cal.diy","slug":"user-not-found-e73700","errorCode":null,"errorMessage":"User not found","messagePattern":"User not found","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"apps/api/v2/src/platform/calendars/services/calendars.service.ts","lineNumber":67,"sourceCode":"        delegatedToId: null,\n        delegationCredentialId: null,\n      }))\n      .filter((credential) => !!credential);\n  }\n\n  async getCalendars(\n    userId: number,\n    ensureDefaultSelectedCalendars = false\n  ): Promise<ConnectedDestinationCalendars> {\n    const cachedResult = await this.calendarsCacheService.getConnectedAndDestinationCalendarsCache(userId);\n\n    if (cachedResult && !ensureDefaultSelectedCalendars) {\n      return cachedResult;\n    }\n\n    const userWithCalendars = await this.usersRepository.findByIdWithCalendars(userId);\n    if (!userWithCalendars) {\n      throw new NotFoundException(\"User not found\");\n    }\n    const result = await getConnectedDestinationCalendarsAndEnsureDefaultsInDb({\n      user: {\n        ...userWithCalendars,\n        allSelectedCalendars: userWithCalendars.selectedCalendars,\n        userLevelSelectedCalendars: userWithCalendars.selectedCalendars.filter(\n          (calendar) => !calendar.eventTypeId\n        ),\n      },\n      onboarding: ensureDefaultSelectedCalendars,\n      eventTypeId: null,\n      prisma: this.dbWrite.prisma as unknown as PrismaClient,\n    });\n    await this.calendarsCacheService.setConnectedAndDestinationCalendarsCache(userId, result);\n\n    return result;\n  }\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/calcom/cal.diy/blob/176037d0afbe572f870a3c702985e7cd83fe6c0c/apps/api/v2/src/platform/calendars/services/calendars.service.ts#L49-L85","documentation":"Thrown by CalendarsService.getCalendars (calendars.service.ts:67) as NotFoundException (HTTP 404) when usersRepository.findByIdWithCalendars(userId) returns null — no User row exists for the given id. The auth guard usually guarantees a live user, so this points to a deleted user, a stale token, or an internal caller passing a wrong identifier.","triggerScenarios":"User was deleted but their access token is still accepted briefly; an internal service call passes 0/undefined/a teamId as userId; test fixture created credentials but not the User row.","commonSituations":"Post-deletion race window; cross-service call mis-mapping userId; seeded test data missing the user; misrouted request after an account merge.","solutions":["Confirm the user exists: SELECT id FROM users WHERE id=<id>.","Check the caller is passing the authenticated user's id, not 0/null/a teamId.","Re-seed test data or recreate the user if intentionally present."],"exampleFix":"// before: internal call with an unverified id\nawait calendarsService.getCalendars(maybeUserId); // 404 if null\n\n// after: guard at the boundary\nif (!maybeUserId || !(await usersRepo.exists(maybeUserId))) {\n  throw new Error(`refusing to call getCalendars for unknown user ${maybeUserId}`);\n}\nawait calendarsService.getCalendars(maybeUserId);","handlingStrategy":"validation","validationCode":"// Before calling getCalendars with an internal userId, confirm the user exists\nconst user = await usersRepository.findByIdWithCalendars(userId);\nif (!user) throw new NotFoundError(`User ${userId} does not exist`);","typeGuard":"function isKnownUserId(id) {\n  return Number.isInteger(id) && id > 0;\n}","tryCatchPattern":"try {\n  await calendarsService.getCalendars(userId);\n} catch (e) {\n  if (e instanceof NotFoundException && /user not found/i.test(e.message)) {\n    // user was deleted or the id is wrong — do not retry; surface to auth layer\n    throw new AuthorizationError('User does not exist');\n  }\n  throw e;\n}","preventionTips":["Never pass 0/null/undefined as userId to internal services.","Invalidate access tokens promptly when a user is deleted.","Validate that internal callers receive userId from the authenticated principal, not from request params."],"tags":["user","not-found","auth","http-404"],"backgroundTag":null,"analyzedSha":"176037d0afbe572f870a3c702985e7cd83fe6c0c","analyzedAt":"2026-08-12T19:12:41.464Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}