fix(backend): treat missing aiEnabled as enabled for legacy users

Users seeded before the aiEnabled field existed read as undefined via lean(),
and the copilot runtime treated undefined as disabled, 403-ing session
creation. Only an explicit false now disables; toUserAdmin also defaults
the field to true for display.
This commit is contained in:
lakshit verma 2026-08-19 21:46:21 +05:30
parent 0de104568f
commit 303def2205
No known key found for this signature in database
2 changed files with 4 additions and 2 deletions

View file

@ -35,7 +35,9 @@ function isAbortError(err: unknown): boolean {
async function assertUserEnabled(userId: string): Promise<void> {
const user = await userRepo.findById(userId);
if (!user || !user.aiEnabled) throw forbidden('AI is disabled for this account');
// aiEnabled defaults to true; only an explicit false disables (legacy
// documents created before the field existed read as undefined).
if (!user || user.aiEnabled === false) throw forbidden('AI is disabled for this account');
}
const providerTools: ProviderTool[] = copilotTools.map((tool) => ({

View file

@ -64,7 +64,7 @@ export function toUserAdmin(user: UserDoc): UserAdmin {
return {
...toUserPublic(user),
isActive: user.isActive,
aiEnabled: user.aiEnabled,
lastLoginAt: user.lastLoginAt ? user.lastLoginAt.toISOString() : null,
aiEnabled: user.aiEnabled ?? true,
};
}