fix(export csv): added a filter to check if item is approved

This commit is contained in:
Matthieu Haineault 2025-08-21 16:27:17 -04:00
parent 9a150715b0
commit bfe145854f

View File

@ -18,54 +18,67 @@ export interface CsvRow {
export class CsvExportService {
constructor(private readonly prisma: PrismaService) {}
async collectTransaction(period_id: number, companies: ExportCompany[]): Promise<CsvRow[]> {
const companyCodes = companies.map(c => c === ExportCompany.TARGO ? 1 : 2);
async collectTransaction( period_id: number, companies: ExportCompany[], approved: boolean = true):
Promise<CsvRow[]> {
const company_codes = companies.map(c => c === ExportCompany.TARGO ? 1 : 2);
const period = await this.prisma.payPeriods.findFirst({
where: { pay_period_no: period_id },
});
if(!period) {
throw new NotFoundException(`Pay period ${period_id} not found`);
}
if(!period) throw new NotFoundException(`Pay period ${period_id} not found`);
const startDate = period.period_start;
const endDate = period.period_end;
const start_date = period.period_start;
const end_date = period.period_end;
const approved_filter = approved ? { is_approved: true } : {};
//fetching shifts
const shifts = await this.prisma.shifts.findMany({
where: { date: { gte: startDate, lte: endDate },
timesheet: { employee: { company_code: { in: companyCodes} } },
where: {
date: { gte: start_date, lte: end_date },
...approved_filter,
timesheet: {
employee: { company_code: { in: company_codes} } },
},
include: { bank_code: true,
timesheet: { include: {employee: { include: { user:true,
supervisor: { include: { user:true } },
}},
}},
include: {
bank_code: true,
timesheet: { include: {
employee: { include: {
user:true,
supervisor: { include: {
user:true } } } } } },
},
});
//fetching expenses
const expenses = await this.prisma.expenses.findMany({
where: { date: { gte: startDate, lte: endDate },
timesheet: { employee: { company_code: { in: companyCodes} } },
where: {
date: { gte: start_date, lte: end_date },
...approved_filter,
timesheet: { employee: { company_code: { in: company_codes} } },
},
include: { bank_code: true,
timesheet: { include: { employee: { include: { user: true,
supervisor: { include: { user:true } },
} },
} },
timesheet: { include: {
employee: { include: {
user: true,
supervisor: { include: {
user:true } } } } } },
},
});
//fetching leave requests
const leaves = await this.prisma.leaveRequests.findMany({
where : { start_date_time: { gte: startDate, lte: endDate },
employee: { company_code: { in: companyCodes } },
where : {
start_date_time: { gte: start_date, lte: end_date },
employee: { company_code: { in: company_codes } },
},
include: { bank_code: true,
employee: { include: { user: true,
supervisor: { include: { user: true } },
}},
include: {
bank_code: true,
employee: { include: {
user: true,
supervisor: { include: {
user: true } } } },
},
});
@ -74,7 +87,7 @@ export class CsvExportService {
//Shifts Mapping
for (const shift of shifts) {
const emp = shift.timesheet.employee;
const week_number = this.computeWeekNumber(startDate, shift.date);
const week_number = this.computeWeekNumber(start_date, shift.date);
const hours = this.computeHours(shift.start_time, shift.end_time);
rows.push({
@ -85,7 +98,7 @@ export class CsvExportService {
quantity_hours: hours,
amount: undefined,
week_number,
pay_date: this.formatDate(endDate),
pay_date: this.formatDate(end_date),
holiday_date: undefined,
});
}
@ -93,7 +106,7 @@ export class CsvExportService {
//Expenses Mapping
for (const e of expenses) {
const emp = e.timesheet.employee;
const week_number = this.computeWeekNumber(startDate, e.date);
const week_number = this.computeWeekNumber(start_date, e.date);
rows.push({
company_code: emp.company_code,
@ -103,7 +116,7 @@ export class CsvExportService {
quantity_hours: undefined,
amount: Number(e.amount),
week_number,
pay_date: this.formatDate(endDate),
pay_date: this.formatDate(end_date),
holiday_date: undefined,
});
}
@ -115,7 +128,7 @@ export class CsvExportService {
const start = l.start_date_time;
const end = l.end_date_time ?? start;
const week_number = this.computeWeekNumber(startDate, start);
const week_number = this.computeWeekNumber(start_date, start);
const hours = this.computeHours(start, end);
rows.push({
@ -126,7 +139,7 @@ export class CsvExportService {
quantity_hours: hours,
amount: undefined,
week_number,
pay_date: this.formatDate(endDate),
pay_date: this.formatDate(end_date),
holiday_date: undefined,
});
}
@ -146,7 +159,7 @@ export class CsvExportService {
generateCsv(rows: CsvRow[]): Buffer {
const header = [
'company_code',
'external_payrol_id',
'external_payroll_id',
'full_name',
'bank_code',
'quantity_hours',