fix(schedule_preset) : added a check to remove preset_id for employees using the newly deleted preset

This commit is contained in:
Matthieu Haineault 2025-12-11 12:02:34 -05:00
parent acc128e5ea
commit be957d8180
5 changed files with 216 additions and 88 deletions

View File

@ -544,10 +544,83 @@
] ]
} }
}, },
"/exports/csv": { "/exports/csv/{year}/{period_no}": {
"get": { "get": {
"operationId": "CsvExportController_exportCsv", "operationId": "CsvExportController_exportCsv",
"parameters": [], "parameters": [
{
"name": "approved",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "shifts",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "expenses",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "holiday",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "vacation",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "targo",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "solucom",
"required": true,
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "year",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
},
{
"name": "period_no",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "" "description": ""

View File

@ -42,6 +42,7 @@ export class EmployeesGetService {
external_payroll_id: true, external_payroll_id: true,
first_work_day: true, first_work_day: true,
last_work_day: true, last_work_day: true,
schedule_preset_id: true,
} }
}).then(rows => rows.map(r => ({ }).then(rows => rows.map(r => ({
@ -56,6 +57,7 @@ export class EmployeesGetService {
supervisor_full_name: `${r.supervisor?.user.first_name} ${r.supervisor?.user.last_name}`, supervisor_full_name: `${r.supervisor?.user.first_name} ${r.supervisor?.user.last_name}`,
first_work_day: toStringFromDate(r.first_work_day), first_work_day: toStringFromDate(r.first_work_day),
last_work_day: r.last_work_day ? toStringFromDate(r.last_work_day) : null, last_work_day: r.last_work_day ? toStringFromDate(r.last_work_day) : null,
preset_id: r.schedule_preset_id ?? undefined,
}))); })));
return { success: true, data: employee_list }; return { success: true, data: employee_list };
}; };
@ -81,6 +83,7 @@ export class EmployeesGetService {
job_title: true, job_title: true,
external_payroll_id: true, external_payroll_id: true,
is_supervisor: true, is_supervisor: true,
schedule_preset_id: true,
supervisor: { supervisor: {
select: { select: {
id: true, user: { id: true, user: {
@ -110,6 +113,7 @@ export class EmployeesGetService {
phone_number: existing_profile.user.phone_number, phone_number: existing_profile.user.phone_number,
residence: existing_profile.user.phone_number, residence: existing_profile.user.phone_number,
first_work_day: toStringFromDate(existing_profile.first_work_day), first_work_day: toStringFromDate(existing_profile.first_work_day),
preset_id: existing_profile.schedule_preset_id ?? undefined,
}, },
}; };
}; };

View File

@ -1,4 +1,4 @@
import { Controller, Get, Header, Query} from "@nestjs/common"; import { Controller, Get, Header, Param, Query } from "@nestjs/common";
import { CsvExportService } from "./csv-exports.service"; import { CsvExportService } from "./csv-exports.service";
import { ExportCsvOptionsDto } from "./export-csv-options.dto"; import { ExportCsvOptionsDto } from "./export-csv-options.dto";
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators"; import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
@ -8,25 +8,35 @@ import { Modules as ModulesEnum } from ".prisma/client";
export class CsvExportController { export class CsvExportController {
constructor(private readonly csvService: CsvExportService) { } constructor(private readonly csvService: CsvExportService) { }
@Get('csv') @Get('csv/:year/:period_no')
@ModuleAccessAllowed(ModulesEnum.employee_management) @ModuleAccessAllowed(ModulesEnum.employee_management)
@Header('Content-Type', 'text/csv; charset=utf-8') @Header('Content-Type', 'text/csv; charset=utf-8')
@Header('Content-Disposition', 'attachment; filename="export.csv"') @Header('Content-Disposition', 'attachment; filename="export.csv"')
async exportCsv(@Query() query: ExportCsvOptionsDto ): Promise<Buffer> { async exportCsv(
@Query('approved') approved: boolean,
@Query('shifts') shifts: boolean,
@Query('expenses') expenses: boolean,
@Query('holiday') holiday: boolean,
@Query('vacation') vacation: boolean,
@Query('targo') targo: boolean,
@Query('solucom') solucom: boolean,
@Param('year') year: number,
@Param('period_no') period_no: number,
): Promise<Buffer> {
const rows = await this.csvService.collectTransaction( const rows = await this.csvService.collectTransaction(
query.year, year,
query.period_no, period_no,
{ {
approved: query.approved ?? true, approved: approved ?? false,
types: { types: {
shifts: query.shifts ?? true, shifts: shifts ?? false,
expenses: query.expenses ?? true, expenses: expenses ?? false,
holiday: query.holiday ?? true, holiday: holiday ?? false,
vacation: query.vacation ?? true, vacation: vacation ?? false,
}, },
companies: { companies: {
targo: query.targo ?? true, targo: targo ?? false,
solucom: query.solucom ?? true, solucom: solucom ?? false,
}, },
} }
); );

View File

@ -35,8 +35,9 @@ export class CsvExportService {
} }
const approved_filter = filters.approved ? { is_approved: true } : {}; const approved_filter = filters.approved ? { is_approved: true } : {};
const holiday_code = await this.resolveHolidayTypeCode('HOLIDAY');
const {holiday_code, vacation_code} = this.resolveLeaveCodes(); const vacation_code = await this.resolveVacationTypeCode('VACATION');
const code_filter = [holiday_code, vacation_code];
//Prisma queries //Prisma queries
const promises: Array<Promise<any[]>> = []; const promises: Array<Promise<any[]>> = [];
@ -46,7 +47,7 @@ export class CsvExportService {
where: { where: {
date: { gte: start, lte: end }, date: { gte: start, lte: end },
...approved_filter, ...approved_filter,
bank_code: { bank_code: { notIn: [ holiday_code, vacation_code ] } }, bank_code: { bank_code: { notIn: code_filter } },
timesheet: { employee: { company_code: { in: company_codes } } }, timesheet: { employee: { company_code: { in: company_codes } } },
}, },
select: { select: {
@ -54,13 +55,17 @@ export class CsvExportService {
start_time: true, start_time: true,
end_time: true, end_time: true,
bank_code: { select: { bank_code: true } }, bank_code: { select: { bank_code: true } },
timesheet: { select: { timesheet: {
employee: { select: { select: {
employee: {
select: {
company_code: true, company_code: true,
external_payroll_id: true, external_payroll_id: true,
user: { select: { first_name: true, last_name: true } }, user: { select: { first_name: true, last_name: true } },
}}, }
}}, },
}
},
}, },
})); }));
} else { } else {
@ -80,13 +85,17 @@ export class CsvExportService {
start_time: true, start_time: true,
end_time: true, end_time: true,
bank_code: { select: { bank_code: true } }, bank_code: { select: { bank_code: true } },
timesheet: { select: { timesheet: {
employee: { select: { select: {
employee: {
select: {
company_code: true, company_code: true,
external_payroll_id: true, external_payroll_id: true,
user: { select: { first_name: true, last_name: true } }, user: { select: { first_name: true, last_name: true } },
} }, }
} }, },
}
},
}, },
})); }));
} else { } else {
@ -106,13 +115,17 @@ export class CsvExportService {
start_time: true, start_time: true,
end_time: true, end_time: true,
bank_code: { select: { bank_code: true } }, bank_code: { select: { bank_code: true } },
timesheet: { select: { timesheet: {
employee: { select: { select: {
employee: {
select: {
company_code: true, company_code: true,
external_payroll_id: true, external_payroll_id: true,
user: { select: { first_name: true, last_name: true } }, user: { select: { first_name: true, last_name: true } },
} }, }
} }, },
}
},
}, },
})); }));
} else { } else {
@ -130,13 +143,17 @@ export class CsvExportService {
date: true, date: true,
amount: true, amount: true,
bank_code: { select: { bank_code: true } }, bank_code: { select: { bank_code: true } },
timesheet: { select: { timesheet: {
employee: { select: { select: {
employee: {
select: {
company_code: true, company_code: true,
external_payroll_id: true, external_payroll_id: true,
user: { select: { first_name: true, last_name: true } }, user: { select: { first_name: true, last_name: true } },
}}, }
}}, },
}
},
}, },
})); }));
} else { } else {
@ -198,14 +215,27 @@ export class CsvExportService {
return rows; return rows;
} }
resolveLeaveCodes(): { holiday_code: string; vacation_code: string; } { resolveHolidayTypeCode = async (holiday: string): Promise<string> => {
const holiday_code = process.env.HOLIDAY_CODE?.trim(); const holiday_code = await this.prisma.bankCodes.findFirst({
where: { type: holiday },
select: {
bank_code: true,
},
});
if (!holiday_code) throw new BadRequestException('Missing Holiday bank code'); if (!holiday_code) throw new BadRequestException('Missing Holiday bank code');
const vacation_code = process.env.VACATION_CODE?.trim(); return holiday_code.bank_code;
if(!vacation_code) throw new BadRequestException('Missing Vacation bank code'); }
return { holiday_code, vacation_code}; resolveVacationTypeCode = async (vacation: string): Promise<string> => {
const vacation_code = await this.prisma.bankCodes.findFirst({
where: { type: vacation },
select: {
bank_code: true,
},
});
if (!vacation_code) throw new BadRequestException('Missing Vacation bank code');
return vacation_code.bank_code;
} }
resolveCompanyCodes(companies: { targo: boolean; solucom: boolean; }): number[] { resolveCompanyCodes(companies: { targo: boolean; solucom: boolean; }): number[] {

View File

@ -14,6 +14,17 @@ export class SchedulePresetDeleteService {
}); });
if (!preset) return { success: false, error: `SCHEDULE_PRESET_NOT_FOUND` }; if (!preset) return { success: false, error: `SCHEDULE_PRESET_NOT_FOUND` };
const employee_with_preset = await this.prisma.employees.findMany({
where: { schedule_preset_id: preset.id },
select: {
schedule_preset_id: true,
},
});
if(employee_with_preset.length > 0) {
for(const employee of employee_with_preset) {
employee.schedule_preset_id = null;
}
}
await this.prisma.$transaction(async (tx) => { await this.prisma.$transaction(async (tx) => {
await tx.schedulePresetShifts.deleteMany({ where: { preset_id: preset_id } }); await tx.schedulePresetShifts.deleteMany({ where: { preset_id: preset_id } });
await tx.schedulePresets.delete({ where: { id: preset_id } }); await tx.schedulePresets.delete({ where: { id: preset_id } });