refactor(presets): modified routes name and switch Param(email) to use employee_id instead. ajusted these methods to use employee_id accordingly

This commit is contained in:
Matthieu Haineault 2025-10-30 12:22:12 -04:00
parent 2a250567ad
commit 1385777122
3 changed files with 20 additions and 34 deletions

View File

@ -1,11 +1,9 @@
import { Controller, Put, Param, Query, Body, Get, Post, BadRequestException, ParseIntPipe, Delete, Patch } from "@nestjs/common"; import { Controller, Param, Query, Body, Get, Post, BadRequestException, ParseIntPipe, Delete, Patch } from "@nestjs/common";
import { SchedulePresetsCommandService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-command.service"; import { SchedulePresetsCommandService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-command.service";
import { SchedulePresetsApplyService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-apply.service"; import { SchedulePresetsApplyService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-apply.service";
import { SchedulePresetsQueryService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-query.service"; import { SchedulePresetsQueryService } from "src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-query.service";
import { SchedulePresetsDto } from "src/time-and-attendance/modules/time-tracker/schedule-presets/dtos/create-schedule-presets.dto";
import { SchedulePresetsUpdateDto } from "src/time-and-attendance/modules/time-tracker/schedule-presets/dtos/update-schedule-presets.dto"; import { SchedulePresetsUpdateDto } from "src/time-and-attendance/modules/time-tracker/schedule-presets/dtos/update-schedule-presets.dto";
import { SchedulePresetsDto } from "src/time-and-attendance/modules/time-tracker/schedule-presets/dtos/create-schedule-presets.dto";
@Controller('schedule-presets') @Controller('schedule-presets')
export class SchedulePresetsController { export class SchedulePresetsController {
@ -16,7 +14,7 @@ export class SchedulePresetsController {
){} ){}
//used to create a schedule preset //used to create a schedule preset
@Post(':employee_id') @Post('create/:employee_id')
async createPreset( async createPreset(
@Param('employee_id', ParseIntPipe) employee_id: number, @Param('employee_id', ParseIntPipe) employee_id: number,
@Body() dto: SchedulePresetsDto, @Body() dto: SchedulePresetsDto,
@ -25,7 +23,7 @@ export class SchedulePresetsController {
} }
//used to update an already existing schedule preset //used to update an already existing schedule preset
@Patch(':preset_id') @Patch('update/:preset_id')
async updatePreset( async updatePreset(
@Param('preset_id', ParseIntPipe) preset_id: number, @Param('preset_id', ParseIntPipe) preset_id: number,
@Body() dto: SchedulePresetsUpdateDto, @Body() dto: SchedulePresetsUpdateDto,
@ -34,7 +32,7 @@ export class SchedulePresetsController {
} }
//used to delete a schedule preset //used to delete a schedule preset
@Delete(':preset_id') @Delete('delete/:preset_id')
async deletePreset( async deletePreset(
@Param('preset_id') preset_id: number, @Param('preset_id') preset_id: number,
) { ) {
@ -43,21 +41,23 @@ export class SchedulePresetsController {
//used to show the list of available schedule presets //used to show the list of available schedule presets
@Get(':email') @Get('find/:employee_id')
async findListByEmail( async findListById(
@Param('email') email: string, @Param('employee_id', ParseIntPipe) employee_id: number,
) { ) {
return this.queryService.findSchedulePresetsByEmail(email); return this.queryService.findSchedulePresets(employee_id);
} }
//used to apply a preset to a timesheet //used to apply a preset to a timesheet
@Post('/apply-presets/:email') @Post('/apply-presets/:employee_id')
async applyPresets( async applyPresets(
@Param('email') email: string, @Param('employee_id') employee_id: number,
@Query('preset') preset_name: string, @Query('preset') preset_name: string,
@Query('start') start_date: string, @Query('start') start_date: string,
) { ) {
if(!preset_name?.trim()) throw new BadRequestException('Query "preset" is required'); if(!preset_name?.trim()) throw new BadRequestException('Query "preset" is required');
if(!start_date?.trim()) throw new BadRequestException('Query "start" is required YYYY-MM-DD'); if(!start_date?.trim()) throw new BadRequestException('Query "start" is required YYYY-MM-DD');
return this.applyPresetsService.applyToTimesheet(email, preset_name, start_date); return this.applyPresetsService.applyToTimesheet(employee_id, preset_name, start_date);
} }
} }

View File

@ -1,5 +1,4 @@
import { Injectable, BadRequestException, NotFoundException, ConflictException } from "@nestjs/common"; import { Injectable, BadRequestException, NotFoundException, ConflictException } from "@nestjs/common";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
import { Weekday, Prisma } from "@prisma/client"; import { Weekday, Prisma } from "@prisma/client";
import { DATE_ISO_FORMAT } from "src/time-and-attendance/utils/constants.utils"; import { DATE_ISO_FORMAT } from "src/time-and-attendance/utils/constants.utils";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
@ -9,22 +8,16 @@ import { WEEKDAY } from "src/time-and-attendance/utils/mappers.utils";
@Injectable() @Injectable()
export class SchedulePresetsApplyService { export class SchedulePresetsApplyService {
constructor( constructor( private readonly prisma: PrismaService) {}
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
) {}
async applyToTimesheet( async applyToTimesheet(
email: string, employee_id: number,
preset_name: string, preset_name: string,
start_date_iso: string, start_date_iso: string,
): Promise<ApplyResult> { ): Promise<ApplyResult> {
if(!preset_name?.trim()) throw new BadRequestException('A preset_name is required'); if(!preset_name?.trim()) throw new BadRequestException('A preset_name is required');
if(!DATE_ISO_FORMAT.test(start_date_iso)) throw new BadRequestException('start_date must be of format :YYYY-MM-DD'); if(!DATE_ISO_FORMAT.test(start_date_iso)) throw new BadRequestException('start_date must be of format :YYYY-MM-DD');
const employee_id = await this.emailResolver.findIdByEmail(email);
if(!employee_id) throw new NotFoundException(`Employee with email: ${email} not found`);
const preset = await this.prisma.schedulePresets.findFirst({ const preset = await this.prisma.schedulePresets.findFirst({
where: { employee_id, name: preset_name }, where: { employee_id, name: preset_name },
include: { include: {

View File

@ -1,20 +1,13 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { PresetResponse, ShiftResponse } from "src/time-and-attendance/utils/type.utils"; import { PresetResponse, ShiftResponse } from "src/time-and-attendance/utils/type.utils";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import { Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
@Injectable() @Injectable()
export class SchedulePresetsQueryService { export class SchedulePresetsQueryService {
constructor( constructor( private readonly prisma: PrismaService ){}
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
){}
async findSchedulePresetsByEmail(email:string): Promise<PresetResponse[]> {
const employee_id = await this.emailResolver.findIdByEmail(email);
if(!employee_id) throw new NotFoundException(`Employee with email: ${email} not found`);
async findSchedulePresets(employee_id: number): Promise<PresetResponse[]> {
try { try {
const presets = await this.prisma.schedulePresets.findMany({ const presets = await this.prisma.schedulePresets.findMany({
where: { employee_id }, where: { employee_id },