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 { 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 { 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 { SchedulePresetsDto } from "src/time-and-attendance/modules/time-tracker/schedule-presets/dtos/create-schedule-presets.dto";
@Controller('schedule-presets')
export class SchedulePresetsController {
@ -16,7 +14,7 @@ export class SchedulePresetsController {
){}
//used to create a schedule preset
@Post(':employee_id')
@Post('create/:employee_id')
async createPreset(
@Param('employee_id', ParseIntPipe) employee_id: number,
@Body() dto: SchedulePresetsDto,
@ -25,7 +23,7 @@ export class SchedulePresetsController {
}
//used to update an already existing schedule preset
@Patch(':preset_id')
@Patch('update/:preset_id')
async updatePreset(
@Param('preset_id', ParseIntPipe) preset_id: number,
@Body() dto: SchedulePresetsUpdateDto,
@ -34,7 +32,7 @@ export class SchedulePresetsController {
}
//used to delete a schedule preset
@Delete(':preset_id')
@Delete('delete/:preset_id')
async deletePreset(
@Param('preset_id') preset_id: number,
) {
@ -43,21 +41,23 @@ export class SchedulePresetsController {
//used to show the list of available schedule presets
@Get(':email')
async findListByEmail(
@Param('email') email: string,
@Get('find/:employee_id')
async findListById(
@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
@Post('/apply-presets/:email')
@Post('/apply-presets/:employee_id')
async applyPresets(
@Param('email') email: string,
@Param('employee_id') employee_id: number,
@Query('preset') preset_name: string,
@Query('start') start_date: string,
) {
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');
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 { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
import { Weekday, Prisma } from "@prisma/client";
import { DATE_ISO_FORMAT } from "src/time-and-attendance/utils/constants.utils";
import { PrismaService } from "src/prisma/prisma.service";
@ -9,22 +8,16 @@ import { WEEKDAY } from "src/time-and-attendance/utils/mappers.utils";
@Injectable()
export class SchedulePresetsApplyService {
constructor(
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
) {}
constructor( private readonly prisma: PrismaService) {}
async applyToTimesheet(
email: string,
employee_id: number,
preset_name: string,
start_date_iso: string,
): Promise<ApplyResult> {
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');
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({
where: { employee_id, name: preset_name },
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 { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
import { PrismaService } from "src/prisma/prisma.service";
import { Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client";
@Injectable()
export class SchedulePresetsQueryService {
constructor(
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`);
constructor( private readonly prisma: PrismaService ){}
async findSchedulePresets(employee_id: number): Promise<PresetResponse[]> {
try {
const presets = await this.prisma.schedulePresets.findMany({
where: { employee_id },