targo-backend/src/time-and-attendance/exports/csv-exports.controller.ts
2025-12-12 13:26:09 -05:00

52 lines
1.8 KiB
TypeScript

import { Controller, Get, Param, Query, Res } from "@nestjs/common";
import { CsvExportService } from "./csv-exports.service";
import { ModuleAccessAllowed } from "src/common/decorators/modules-guard.decorators";
import { Modules as ModulesEnum } from ".prisma/client";
import { Response } from "express";
@Controller('exports')
export class CsvExportController {
constructor(private readonly csvService: CsvExportService) { }
@Get('csv/:year/:period_no')
@ModuleAccessAllowed(ModulesEnum.employee_management)
async exportCsv(
@Query('approved') approved: string,
@Query('shifts') shifts: string,
@Query('expenses') expenses: string,
@Query('holiday') holiday: string,
@Query('vacation') vacation: string,
@Query('targo') targo: string,
@Query('solucom') solucom: string,
@Param('year') year: number,
@Param('period_no') period_no: number,
@Res() response: Response,
): Promise<void> {
console.log({ targo, solucom });
const rows = await this.csvService.collectTransaction(
year,
period_no,
{
approved: approved === 'true',
types: {
shifts: shifts === 'true',
expenses: expenses === 'true',
holiday: holiday === 'true',
vacation: vacation === 'true',
},
companies: {
targo: targo === 'true',
solucom: solucom === 'true',
},
}
);
const csv_buffer = await this.csvService.generateCsv(rows);
response.set({
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': 'attachment; filename="export.csv"',
});
response.send(csv_buffer);
}
}