clean(shifts): did some cleaning

This commit is contained in:
Matthieu Haineault 2025-12-04 11:05:38 -05:00
parent 4c933d3564
commit 9b4517a26d
6 changed files with 61 additions and 43 deletions

View File

@ -1,11 +1,12 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
import { SchedulePresetsController } from "src/time-and-attendance/schedule-presets/controller/schedule-presets.controller"; import { SchedulePresetsController } from "src/time-and-attendance/schedule-presets/controller/schedule-presets.controller";
import { SchedulePresetsGetService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-get.service"; import { SchedulePresetsGetService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-get.service";
import { SchedulePresetsCreateService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-create.service"; import { SchedulePresetsCreateService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-create.service";
import { SchedulePresetUpdateService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-update.service"; import { SchedulePresetUpdateService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-update.service";
import { SchedulePresetDeleteService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-delete.service"; import { SchedulePresetDeleteService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-delete.service";
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";

View File

@ -1,17 +1,21 @@
import { Body, Controller, Delete, Param, Patch, Post } from "@nestjs/common"; import { Body, Controller, Delete, Param, Patch, Post } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory"; import { Modules as ModulesEnum } from ".prisma/client";
import { ShiftDto } from "src/time-and-attendance/shifts/dtos/shift-create.dto"; import { ShiftDto } from "src/time-and-attendance/shifts/dtos/shift-create.dto";
import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service"; import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service";
import { ShiftsUpdateDeleteService } from "src/time-and-attendance/shifts/services/shifts-update-delete.service"; import { ShiftsUpdateService } from "src/time-and-attendance/shifts/services/shifts-update-delete.service";
import { ShiftsDeleteService } from "src/time-and-attendance/shifts/services/shifts-delete.service";
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators"; import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
import { Modules as ModulesEnum } from ".prisma/client"; import { Result } from "src/common/errors/result-error.factory";
import { Access } from "src/common/decorators/module-access.decorators"; import { Access } from "src/common/decorators/module-access.decorators";
@Controller('shift') @Controller('shift')
export class ShiftController { export class ShiftController {
constructor( constructor(
private readonly create_service: ShiftsCreateService, private readonly create_service: ShiftsCreateService,
private readonly update_delete_service: ShiftsUpdateDeleteService, private readonly update_service: ShiftsUpdateService,
private readonly delete_service: ShiftsDeleteService,
) { } ) { }
@Post('create') @Post('create')
@ -23,13 +27,13 @@ export class ShiftController {
@Patch('update') @Patch('update')
@ModuleAccessAllowed(ModulesEnum.timesheets) @ModuleAccessAllowed(ModulesEnum.timesheets)
updateBatch(@Access('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> { updateBatch(@Access('email') email: string, @Body() dtos: ShiftDto[]): Promise<Result<boolean, string>> {
return this.update_delete_service.updateOneOrManyShifts(dtos, email); return this.update_service.updateOneOrManyShifts(dtos, email);
} }
@Delete(':shift_id') @Delete(':shift_id')
@ModuleAccessAllowed(ModulesEnum.timesheets) @ModuleAccessAllowed(ModulesEnum.timesheets)
remove(@Param('shift_id') shift_id: number): Promise<Result<number, string>> { remove(@Param('shift_id') shift_id: number): Promise<Result<number, string>> {
return this.update_delete_service.deleteShift(shift_id); return this.delete_service.deleteShift(shift_id);
} }
} }

View File

@ -0,0 +1,29 @@
import { Injectable } from "@nestjs/common";
import { Result } from "src/common/errors/result-error.factory";
import { PrismaService } from "src/prisma/prisma.service";
@Injectable()
export class ShiftsDeleteService {
constructor(private readonly prisma: PrismaService) { }
//_________________________________________________________________
// DELETE
//_________________________________________________________________
//finds shifts using shit_ids
//blocs deletion if approved
async deleteShift(shift_id: number): Promise<Result<number, string>> {
try {
return await this.prisma.$transaction(async (tx) => {
const shift = await tx.shifts.findUnique({
where: { id: shift_id },
select: { id: true, date: true, timesheet_id: true },
});
if (!shift) return { success: false, error: `SHIFT_NOT_FOUND` };
await tx.shifts.delete({ where: { id: shift_id } });
return { success: true, data: shift.id };
});
} catch (error) {
return { success: false, error: `SHIFT_NOT_FOUND` }
}
}
}

View File

@ -1,15 +1,17 @@
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
import { PrismaService } from "src/prisma/prisma.service";
import { shift_select } from "src/time-and-attendance/utils/selects.utils";
import { Injectable } from "@nestjs/common";
import { Normalized } from "src/time-and-attendance/utils/type.utils";
import { Result } from "src/common/errors/result-error.factory";
import { EmployeeTimesheetResolver } from "src/common/mappers/timesheet.mapper";
import { toDateFromString, toStringFromHHmm, toStringFromDate, toDateFromHHmm, overlaps } from "src/common/utils/date-utils"; import { toDateFromString, toStringFromHHmm, toStringFromDate, toDateFromHHmm, overlaps } from "src/common/utils/date-utils";
import { Injectable } from "@nestjs/common";
import { PrismaService } from "src/prisma/prisma.service";
import { EmployeeTimesheetResolver } from "src/common/mappers/timesheet.mapper";
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
import { Result } from "src/common/errors/result-error.factory";
import { shift_select } from "src/time-and-attendance/utils/selects.utils";
import { Normalized } from "src/time-and-attendance/utils/type.utils";
import { ShiftDto } from "src/time-and-attendance/shifts/dtos/shift-create.dto"; import { ShiftDto } from "src/time-and-attendance/shifts/dtos/shift-create.dto";
@Injectable() @Injectable()
export class ShiftsUpdateDeleteService { export class ShiftsUpdateService {
constructor( constructor(
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly typeResolver: BankCodesResolver, private readonly typeResolver: BankCodesResolver,
@ -113,28 +115,6 @@ export class ShiftsUpdateDeleteService {
} }
} }
//_________________________________________________________________
// DELETE
//_________________________________________________________________
//finds shifts using shit_ids
//blocs deletion if approved
async deleteShift(shift_id: number): Promise<Result<number, string>> {
try {
return await this.prisma.$transaction(async (tx) => {
const shift = await tx.shifts.findUnique({
where: { id: shift_id },
select: { id: true, date: true, timesheet_id: true },
});
if (!shift) return { success: false, error: `SHIFT_NOT_FOUND` };
await tx.shifts.delete({ where: { id: shift_id } });
return { success: true, data: shift.id };
});
} catch (error) {
return { success: false, error: `SHIFT_NOT_FOUND` }
}
}
//_________________________________________________________________ //_________________________________________________________________
// helpers // helpers
//_________________________________________________________________ //_________________________________________________________________

View File

@ -1,12 +1,14 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ShiftController } from 'src/time-and-attendance/shifts/controllers/shift.controller'; import { ShiftController } from 'src/time-and-attendance/shifts/controllers/shift.controller';
import { ShiftsCreateService } from 'src/time-and-attendance/shifts/services/shifts-create.service'; import { ShiftsCreateService } from 'src/time-and-attendance/shifts/services/shifts-create.service';
import { ShiftsUpdateDeleteService } from 'src/time-and-attendance/shifts/services/shifts-update-delete.service'; import { ShiftsDeleteService } from 'src/time-and-attendance/shifts/services/shifts-delete.service';
import { ShiftsUpdateService } from 'src/time-and-attendance/shifts/services/shifts-update-delete.service';
@Module({ @Module({
controllers: [ShiftController], controllers: [ShiftController],
providers: [ ShiftsCreateService, ShiftsUpdateDeleteService ], providers: [ShiftsCreateService, ShiftsUpdateService, ShiftsDeleteService],
exports: [ ShiftsCreateService, ShiftsUpdateDeleteService ], exports: [ShiftsCreateService, ShiftsUpdateService, ShiftsDeleteService],
}) })
export class ShiftsModule { } export class ShiftsModule { }

View File

@ -27,7 +27,8 @@ import { CsvExportController } from "src/modules/exports/controllers/csv-exports
import { ShiftController } from "src/time-and-attendance/shifts/controllers/shift.controller"; import { ShiftController } from "src/time-and-attendance/shifts/controllers/shift.controller";
import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service"; import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service";
import { ShiftsGetService } from "src/time-and-attendance/shifts/services/shifts-get.service"; import { ShiftsGetService } from "src/time-and-attendance/shifts/services/shifts-get.service";
import { ShiftsUpdateDeleteService } from "src/time-and-attendance/shifts/services/shifts-update-delete.service"; import { ShiftsUpdateService } from "src/time-and-attendance/shifts/services/shifts-update-delete.service";
import { ShiftsDeleteService } from "src/time-and-attendance/shifts/services/shifts-delete.service";
import { SchedulePresetsGetService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-get.service"; import { SchedulePresetsGetService } from "src/time-and-attendance/schedule-presets/services/schedule-presets-get.service";
import { SchedulePresetsController } from "src/time-and-attendance/schedule-presets/controller/schedule-presets.controller"; import { SchedulePresetsController } from "src/time-and-attendance/schedule-presets/controller/schedule-presets.controller";
@ -59,7 +60,8 @@ import { SchedulePresetsCreateService } from "src/time-and-attendance/schedule-p
GetTimesheetsOverviewService, GetTimesheetsOverviewService,
ShiftsGetService, ShiftsGetService,
ShiftsCreateService, ShiftsCreateService,
ShiftsUpdateDeleteService, ShiftsUpdateService,
ShiftsDeleteService,
ExpenseUpsertService, ExpenseUpsertService,
SchedulePresetsGetService, SchedulePresetsGetService,
SchedulePresetDeleteService, SchedulePresetDeleteService,