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": {
"operationId": "SchedulePresetsController_createPreset",
"parameters": [
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"parameters": [],
"requestBody": {
"required": true,
"content": {
@ -535,19 +526,10 @@
]
}
},
"/schedule-presets/find/{employee_id}": {
"/schedule-presets/find-list": {
"get": {
"operationId": "SchedulePresetsController_findListById",
"parameters": [
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"parameters": [],
"responses": {
"200": {
"description": ""
@ -558,18 +540,10 @@
]
}
},
"/schedule-presets/apply-presets/{employee_id}": {
"/schedule-presets/apply-presets": {
"post": {
"operationId": "SchedulePresetsController_applyPresets",
"parameters": [
{
"name": "employee_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
},
{
"name": "preset",
"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 { 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";
@ -9,18 +9,18 @@ import { SchedulePresetsUpsertService } from "src/time-and-attendance/time-track
@Controller('schedule-presets')
export class SchedulePresetsController {
constructor(
private readonly upsertService: SchedulePresetsUpsertService,
private readonly upsertService: SchedulePresetsUpsertService,
private readonly getService: SchedulePresetsGetService,
private readonly applyPresetsService: SchedulePresetsApplyService,
private readonly getService: SchedulePresetsGetService,
){}
//used to create a schedule preset
@Post('create/:employee_id')
async createPreset(
@Param('employee_id', ParseIntPipe) employee_id: number,
@Post('create')
async createPreset( @Req() req,
@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
@ -42,23 +42,22 @@ export class SchedulePresetsController {
//used to show the list of available schedule presets
@Get('find/:employee_id')
async findListById(
@Param('employee_id', ParseIntPipe) employee_id: number,
) {
return this.getService.getSchedulePresets(employee_id);
@Get('find-list')
async findListById( @Req() req) {
const email = req.user?.email;
return this.getService.getSchedulePresets(email);
}
//used to apply a preset to a timesheet
@Post('/apply-presets/:employee_id')
async applyPresets(
@Param('employee_id') employee_id: number,
@Post('apply-presets')
async applyPresets( @Req() req,
@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(!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 { ApplyResult } from "src/time-and-attendance/utils/type.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()
export class SchedulePresetsApplyService {
constructor( private readonly prisma: PrismaService) {}
constructor( private readonly prisma: PrismaService, private readonly emailResolver: EmailToIdResolver) {}
async applyToTimesheet(
employee_id: number,
preset_name: string,
start_date_iso: string,
): Promise<ApplyResult> {
async applyToTimesheet( email: string, 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);
const preset = await this.prisma.schedulePresets.findFirst({
where: { employee_id, name: preset_name },
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 { Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { EmailToIdResolver } from "src/time-and-attendance/shared/utils/resolve-email-id.utils";
@Injectable()
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 {
const employee_id = await this.emailResolver.findIdByEmail(email);
const presets = await this.prisma.schedulePresets.findMany({
where: { employee_id },
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 { 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 { EmailToIdResolver } from "src/time-and-attendance/shared/utils/resolve-email-id.utils";
@Injectable()
export class SchedulePresetsUpsertService {
constructor(
private readonly prisma: PrismaService,
private readonly typeResolver : BankCodesResolver,
private readonly emailResolver: EmailToIdResolver,
){}
//_________________________________________________________________
// CREATE
//_________________________________________________________________
async createPreset( employee_id: number, dto: SchedulePresetsDto): Promise<CreatePresetResult> {
async createPreset( email: string, dto: SchedulePresetsDto): Promise<CreatePresetResult> {
try {
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)=> {
if(dto.is_default) {
await tx.schedulePresets.updateMany({