refactor(schedule-presets): modified route and params to use session data

This commit is contained in:
Matthieu Haineault 2025-11-03 11:19:21 -05:00
parent b1c6c50571
commit c59b50a829
5 changed files with 39 additions and 58 deletions

View File

@ -446,19 +446,10 @@
] ]
} }
}, },
"/schedule-presets/create/{employee_id}": { "/schedule-presets/create": {
"post": { "post": {
"operationId": "SchedulePresetsController_createPreset", "operationId": "SchedulePresetsController_createPreset",
"parameters": [ "parameters": [],
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"requestBody": { "requestBody": {
"required": true, "required": true,
"content": { "content": {
@ -535,19 +526,10 @@
] ]
} }
}, },
"/schedule-presets/find/{employee_id}": { "/schedule-presets/find-list": {
"get": { "get": {
"operationId": "SchedulePresetsController_findListById", "operationId": "SchedulePresetsController_findListById",
"parameters": [ "parameters": [],
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "" "description": ""
@ -558,18 +540,10 @@
] ]
} }
}, },
"/schedule-presets/apply-presets/{employee_id}": { "/schedule-presets/apply-presets": {
"post": { "post": {
"operationId": "SchedulePresetsController_applyPresets", "operationId": "SchedulePresetsController_applyPresets",
"parameters": [ "parameters": [
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
},
{ {
"name": "preset", "name": "preset",
"required": true, "required": true,

View File

@ -1,4 +1,4 @@
import { Controller, Param, Query, Body, Get, Post, BadRequestException, ParseIntPipe, Delete, Patch } from "@nestjs/common"; import { Controller, Param, Query, Body, Get, Post, BadRequestException, ParseIntPipe, Delete, Patch, Req } from "@nestjs/common";
import { SchedulePresetsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-presets.dto"; import { SchedulePresetsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-presets.dto";
import { SchedulePresetsUpdateDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/update-schedule-presets.dto"; import { SchedulePresetsUpdateDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/update-schedule-presets.dto";
import { SchedulePresetsApplyService } from "src/time-and-attendance/time-tracker/schedule-presets/services/schedule-presets-apply.service"; import { SchedulePresetsApplyService } from "src/time-and-attendance/time-tracker/schedule-presets/services/schedule-presets-apply.service";
@ -10,17 +10,17 @@ import { SchedulePresetsUpsertService } from "src/time-and-attendance/time-track
export class SchedulePresetsController { export class SchedulePresetsController {
constructor( constructor(
private readonly upsertService: SchedulePresetsUpsertService, private readonly upsertService: SchedulePresetsUpsertService,
private readonly applyPresetsService: SchedulePresetsApplyService,
private readonly getService: SchedulePresetsGetService, private readonly getService: SchedulePresetsGetService,
private readonly applyPresetsService: SchedulePresetsApplyService,
){} ){}
//used to create a schedule preset //used to create a schedule preset
@Post('create/:employee_id') @Post('create')
async createPreset( async createPreset( @Req() req,
@Param('employee_id', ParseIntPipe) employee_id: number,
@Body() dto: SchedulePresetsDto, @Body() dto: SchedulePresetsDto,
) { ) {
return await this.upsertService.createPreset(employee_id, dto); const email = req.user?.email;
return await this.upsertService.createPreset(email, dto);
} }
//used to update an already existing schedule preset //used to update an already existing schedule preset
@ -42,23 +42,22 @@ export class SchedulePresetsController {
//used to show the list of available schedule presets //used to show the list of available schedule presets
@Get('find/:employee_id') @Get('find-list')
async findListById( async findListById( @Req() req) {
@Param('employee_id', ParseIntPipe) employee_id: number, const email = req.user?.email;
) { return this.getService.getSchedulePresets(email);
return this.getService.getSchedulePresets(employee_id);
} }
//used to apply a preset to a timesheet //used to apply a preset to a timesheet
@Post('/apply-presets/:employee_id') @Post('apply-presets')
async applyPresets( async applyPresets( @Req() req,
@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,
) { ) {
const email = req.user?.email;
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(employee_id, preset_name, start_date); return this.applyPresetsService.applyToTimesheet(email, preset_name, start_date);
} }
} }

View File

@ -4,20 +4,19 @@ 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";
import { ApplyResult } from "src/time-and-attendance/utils/type.utils"; import { ApplyResult } from "src/time-and-attendance/utils/type.utils";
import { WEEKDAY } from "src/time-and-attendance/utils/mappers.utils"; import { WEEKDAY } from "src/time-and-attendance/utils/mappers.utils";
import { EmailToIdResolver } from "src/time-and-attendance/shared/utils/resolve-email-id.utils";
@Injectable() @Injectable()
export class SchedulePresetsApplyService { export class SchedulePresetsApplyService {
constructor( private readonly prisma: PrismaService) {} constructor( private readonly prisma: PrismaService, private readonly emailResolver: EmailToIdResolver) {}
async applyToTimesheet( async applyToTimesheet( email: string, preset_name: string, start_date_iso: string ): Promise<ApplyResult> {
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(!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);
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

@ -2,13 +2,18 @@ import { PresetResponse, ShiftResponse } from "src/time-and-attendance/utils/typ
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import { Injectable } from "@nestjs/common"; import { Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { EmailToIdResolver } from "src/time-and-attendance/shared/utils/resolve-email-id.utils";
@Injectable() @Injectable()
export class SchedulePresetsGetService { export class SchedulePresetsGetService {
constructor( private readonly prisma: PrismaService ){} constructor(
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
){}
async getSchedulePresets(employee_id: number): Promise<PresetResponse[]> { async getSchedulePresets(email: string): Promise<PresetResponse[]> {
try { try {
const employee_id = await this.emailResolver.findIdByEmail(email);
const presets = await this.prisma.schedulePresets.findMany({ const presets = await this.prisma.schedulePresets.findMany({
where: { employee_id }, where: { employee_id },
orderBy: [{is_default: 'desc' }, { name: 'asc' }], orderBy: [{is_default: 'desc' }, { name: 'asc' }],

View File

@ -5,20 +5,24 @@ import { toHHmmFromDate } from "src/time-and-attendance/utils/date-time.utils";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import { BankCodesResolver } from "src/time-and-attendance/shared/utils/resolve-bank-type-id.utils"; import { BankCodesResolver } from "src/time-and-attendance/shared/utils/resolve-bank-type-id.utils";
import { SchedulePresetsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-presets.dto"; import { SchedulePresetsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-presets.dto";
import { EmailToIdResolver } from "src/time-and-attendance/shared/utils/resolve-email-id.utils";
@Injectable() @Injectable()
export class SchedulePresetsUpsertService { export class SchedulePresetsUpsertService {
constructor( constructor(
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly typeResolver : BankCodesResolver, private readonly typeResolver : BankCodesResolver,
private readonly emailResolver: EmailToIdResolver,
){} ){}
//_________________________________________________________________ //_________________________________________________________________
// CREATE // CREATE
//_________________________________________________________________ //_________________________________________________________________
async createPreset( employee_id: number, dto: SchedulePresetsDto): Promise<CreatePresetResult> { async createPreset( email: string, dto: SchedulePresetsDto): Promise<CreatePresetResult> {
try { try {
const shifts_data = await this.resolveAndBuildPresetShifts(dto); const shifts_data = await this.resolveAndBuildPresetShifts(dto);
if(!shifts_data) throw new BadRequestException(`Employee with id: ${employee_id} or dto not found`); const employee_id = await this.emailResolver.findIdByEmail(email);
if(!shifts_data) throw new BadRequestException(`Employee with email: ${email} or dto not found`);
await this.prisma.$transaction(async (tx)=> { await this.prisma.$transaction(async (tx)=> {
if(dto.is_default) { if(dto.is_default) {
await tx.schedulePresets.updateMany({ await tx.schedulePresets.updateMany({