feat(preferences): added a getter function and modified schema to use boolean

This commit is contained in:
Matthieu Haineault 2025-11-26 15:31:50 -05:00
parent 439e005936
commit 5f7f639c62
5 changed files with 49 additions and 19 deletions

View File

@ -41,7 +41,7 @@ model userModuleAccess {
employee_list Boolean @default(false) //wich shows the lists of employee to show names, emails, titles and profile picture
employee_management Boolean @default(false) //wich offers CRUD for employees, schedule_presets and manage module access
personnal_profile Boolean @default(false) //wich governs profile details, preferances and dashboard access
blocked Boolean @default(false)
blocked Boolean @default(false)
@@map("user_module_access")
}
@ -347,14 +347,12 @@ model Preferences {
user Users @relation("UserPreferences", fields: [user_id], references: [id])
user_id String @unique @db.Uuid
notifications Int @default(0)
dark_mode Int @default(0)
lang_switch Int @default(0)
lefty_mode Int @default(0)
employee_list_display Int @default(0)
validation_display Int @default(0)
timesheet_display Int @default(0)
notifications Boolean @default(false)
dark_mode Boolean @default(false)
lang_switch Boolean @default(false)
lefty_mode Boolean @default(false)
employee_list_display Boolean @default(false)
approval_display Boolean @default(false)
@@map("preferences")
}

View File

@ -1,4 +1,4 @@
import { Body, Controller, Patch, Req } from "@nestjs/common";
import { Body, Controller, Get, Patch, Query, Req } from "@nestjs/common";
import { PreferencesService } from "../services/preferences.service";
import { PreferencesDto } from "../dtos/preferences.dto";
import { Result } from "src/common/errors/result-error.factory";
@ -7,7 +7,7 @@ import { Result } from "src/common/errors/result-error.factory";
export class PreferencesController {
constructor(private readonly service: PreferencesService){}
@Patch('update_preferences')
@Patch('update')
async updatePreferences(
@Req()req,
@Body() payload: PreferencesDto
@ -16,4 +16,10 @@ export class PreferencesController {
return this.service.updatePreferences(email, payload);
}
@Get()
async findPreferences(@Req() req, @Query() employee_email?:string) {
const email = req.user?.email;
return this.service.findPreferences(email, employee_email);
}
}

View File

@ -6,6 +6,5 @@ export class PreferencesDto {
lang_switch: number;
lefty_mode: number;
employee_list_display: number;
validation_display: number;
timesheet_display: number;
approval_display: number;
}

View File

@ -10,9 +10,37 @@ export class PreferencesService {
constructor(
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
) { }
async findPreferences(email: string, employee_email?: string): Promise<Result<PreferencesDto, string>> {
const account_email = employee_email ?? email;
const user_id = await this.emailResolver.resolveUserIdWithEmail(account_email);
if (!user_id.success) return { success: false, error: 'EMPLOYEE_NOT_FOUND' };
const user_preferences = await this.prisma.preferences.findUnique({
where: { user_id: user_id.data },
select: {
dark_mode: true,
lang_switch: true,
lefty_mode: true,
notifications: true,
employee_list_display: true,
approval_display: true,
},
});
if (!user_preferences) return { success: false, error: 'PREFERENCES_NOT_FOUND' };
const preferences: PreferencesDto = {
dark_mode: user_preferences.dark_mode,
lang_switch: user_preferences.lang_switch,
lefty_mode: user_preferences.lefty_mode,
notifications: user_preferences.notifications,
employee_list_display: user_preferences.employee_list_display,
approval_display: user_preferences.approval_display,
};
return { success: true, data: preferences };
}
async updatePreferences(email: string, dto: PreferencesDto): Promise<Result<Preferences, string>> {
const user_id = await this.emailResolver.resolveUserIdWithEmail(email);
if (!user_id.success) return { success: false, error: user_id.error }
@ -26,8 +54,7 @@ export class PreferencesService {
lang_switch: dto.lang_switch,
lefty_mode: dto.lefty_mode,
employee_list_display: dto.employee_list_display,
validation_display: dto.validation_display,
timesheet_display: dto.timesheet_display,
approval_display: dto.approval_display,
},
include: { user: true },
})

View File

@ -3,7 +3,7 @@ import { ModuleAccess } from "src/identity-and-account/user-module-access/dtos/m
import { AccessGetService } from "src/identity-and-account/user-module-access/services/module-access-get.service";
import { AccessUpdateService } from "src/identity-and-account/user-module-access/services/module-access-update.service";
@Controller()
@Controller('module_access')
export class ModuleAccessController {
constructor(
private readonly getService: AccessGetService,
@ -19,7 +19,7 @@ export class ModuleAccessController {
await this.getService.findModuleAccess(email, employee_email);
};
@Patch()
@Patch('update')
async updateAccess(
@Req() req,
@Body() dto: ModuleAccess,
@ -29,7 +29,7 @@ export class ModuleAccessController {
await this.updateService.updateModuleAccess(email, dto, employee_email);
};
@Patch()
@Patch('revoke')
async revokeModuleAccess(
@Req() req,
@Query('employee_email') employee_email?: string