fix(schedule_preset) : added a check to remove preset_id for employees using the newly deleted preset
This commit is contained in:
parent
acc128e5ea
commit
be957d8180
|
|
@ -544,10 +544,83 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
"/exports/csv": {
|
||||
"/exports/csv/{year}/{period_no}": {
|
||||
"get": {
|
||||
"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": {
|
||||
"200": {
|
||||
"description": ""
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ export class EmployeesGetService {
|
|||
external_payroll_id: true,
|
||||
first_work_day: true,
|
||||
last_work_day: true,
|
||||
schedule_preset_id: true,
|
||||
|
||||
}
|
||||
}).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}`,
|
||||
first_work_day: toStringFromDate(r.first_work_day),
|
||||
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 };
|
||||
};
|
||||
|
|
@ -81,6 +83,7 @@ export class EmployeesGetService {
|
|||
job_title: true,
|
||||
external_payroll_id: true,
|
||||
is_supervisor: true,
|
||||
schedule_preset_id: true,
|
||||
supervisor: {
|
||||
select: {
|
||||
id: true, user: {
|
||||
|
|
@ -110,6 +113,7 @@ export class EmployeesGetService {
|
|||
phone_number: existing_profile.user.phone_number,
|
||||
residence: existing_profile.user.phone_number,
|
||||
first_work_day: toStringFromDate(existing_profile.first_work_day),
|
||||
preset_id: existing_profile.schedule_preset_id ?? undefined,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 { ExportCsvOptionsDto } from "./export-csv-options.dto";
|
||||
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
|
||||
|
|
@ -8,25 +8,35 @@ import { Modules as ModulesEnum } from ".prisma/client";
|
|||
export class CsvExportController {
|
||||
constructor(private readonly csvService: CsvExportService) { }
|
||||
|
||||
@Get('csv')
|
||||
@Get('csv/:year/:period_no')
|
||||
@ModuleAccessAllowed(ModulesEnum.employee_management)
|
||||
@Header('Content-Type', 'text/csv; charset=utf-8')
|
||||
@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(
|
||||
query.year,
|
||||
query.period_no,
|
||||
year,
|
||||
period_no,
|
||||
{
|
||||
approved: query.approved ?? true,
|
||||
approved: approved ?? false,
|
||||
types: {
|
||||
shifts: query.shifts ?? true,
|
||||
expenses: query.expenses ?? true,
|
||||
holiday: query.holiday ?? true,
|
||||
vacation: query.vacation ?? true,
|
||||
shifts: shifts ?? false,
|
||||
expenses: expenses ?? false,
|
||||
holiday: holiday ?? false,
|
||||
vacation: vacation ?? false,
|
||||
},
|
||||
companies: {
|
||||
targo: query.targo ?? true,
|
||||
solucom: query.solucom ?? true,
|
||||
targo: targo ?? false,
|
||||
solucom: solucom ?? false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
|
|
|||
|
|
@ -35,8 +35,9 @@ export class CsvExportService {
|
|||
}
|
||||
|
||||
const approved_filter = filters.approved ? { is_approved: true } : {};
|
||||
|
||||
const {holiday_code, vacation_code} = this.resolveLeaveCodes();
|
||||
const holiday_code = await this.resolveHolidayTypeCode('HOLIDAY');
|
||||
const vacation_code = await this.resolveVacationTypeCode('VACATION');
|
||||
const code_filter = [holiday_code, vacation_code];
|
||||
|
||||
//Prisma queries
|
||||
const promises: Array<Promise<any[]>> = [];
|
||||
|
|
@ -46,7 +47,7 @@ export class CsvExportService {
|
|||
where: {
|
||||
date: { gte: start, lte: end },
|
||||
...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 } } },
|
||||
},
|
||||
select: {
|
||||
|
|
@ -54,13 +55,17 @@ export class CsvExportService {
|
|||
start_time: true,
|
||||
end_time: true,
|
||||
bank_code: { select: { bank_code: true } },
|
||||
timesheet: { select: {
|
||||
employee: { select: {
|
||||
timesheet: {
|
||||
select: {
|
||||
employee: {
|
||||
select: {
|
||||
company_code: true,
|
||||
external_payroll_id: true,
|
||||
user: { select: { first_name: true, last_name: true } },
|
||||
}},
|
||||
}},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
|
|
@ -80,13 +85,17 @@ export class CsvExportService {
|
|||
start_time: true,
|
||||
end_time: true,
|
||||
bank_code: { select: { bank_code: true } },
|
||||
timesheet: { select: {
|
||||
employee: { select: {
|
||||
timesheet: {
|
||||
select: {
|
||||
employee: {
|
||||
select: {
|
||||
company_code: true,
|
||||
external_payroll_id: true,
|
||||
user: { select: { first_name: true, last_name: true } },
|
||||
} },
|
||||
} },
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
|
|
@ -106,13 +115,17 @@ export class CsvExportService {
|
|||
start_time: true,
|
||||
end_time: true,
|
||||
bank_code: { select: { bank_code: true } },
|
||||
timesheet: { select: {
|
||||
employee: { select: {
|
||||
timesheet: {
|
||||
select: {
|
||||
employee: {
|
||||
select: {
|
||||
company_code: true,
|
||||
external_payroll_id: true,
|
||||
user: { select: { first_name: true, last_name: true } },
|
||||
} },
|
||||
} },
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
|
|
@ -130,13 +143,17 @@ export class CsvExportService {
|
|||
date: true,
|
||||
amount: true,
|
||||
bank_code: { select: { bank_code: true } },
|
||||
timesheet: { select: {
|
||||
employee: { select: {
|
||||
timesheet: {
|
||||
select: {
|
||||
employee: {
|
||||
select: {
|
||||
company_code: true,
|
||||
external_payroll_id: true,
|
||||
user: { select: { first_name: true, last_name: true } },
|
||||
}},
|
||||
}},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
}));
|
||||
} else {
|
||||
|
|
@ -198,14 +215,27 @@ export class CsvExportService {
|
|||
return rows;
|
||||
}
|
||||
|
||||
resolveLeaveCodes(): { holiday_code: string; vacation_code: string; } {
|
||||
const holiday_code = process.env.HOLIDAY_CODE?.trim();
|
||||
resolveHolidayTypeCode = async (holiday: string): Promise<string> => {
|
||||
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');
|
||||
|
||||
const vacation_code = process.env.VACATION_CODE?.trim();
|
||||
if(!vacation_code) throw new BadRequestException('Missing Vacation bank code');
|
||||
return holiday_code.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[] {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,17 @@ export class SchedulePresetDeleteService {
|
|||
});
|
||||
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 tx.schedulePresetShifts.deleteMany({ where: { preset_id: preset_id } });
|
||||
await tx.schedulePresets.delete({ where: { id: preset_id } });
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user