45 lines
2.0 KiB
TypeScript
45 lines
2.0 KiB
TypeScript
import { Controller, Get, Header, Query, UseGuards } from "@nestjs/common";
|
|
import { RolesGuard } from "src/common/guards/roles.guard";
|
|
import { Roles as RoleEnum } from '.prisma/client';
|
|
import { CsvExportService } from "../services/csv-exports.service";
|
|
import { ExportCompany, ExportCsvOptionsDto, ExportType } from "../dtos/export-csv-options.dto";
|
|
import { RolesAllowed } from "src/common/decorators/roles.decorators";
|
|
|
|
|
|
@Controller('exports')
|
|
@UseGuards(RolesGuard)
|
|
export class CsvExportController {
|
|
constructor(private readonly csvService: CsvExportService) {}
|
|
|
|
@Get('csv')
|
|
@Header('Content-Type', 'text/csv; charset=utf-8')
|
|
@Header('Content-Dispoition', 'attachment; filename="export.csv"')
|
|
//@RolesAllowed(RoleEnum.ADMIN, RoleEnum.ACCOUNTING, RoleEnum.HR)
|
|
async exportCsv(@Query() options: ExportCsvOptionsDto,
|
|
@Query('period') periodId: string ): Promise<Buffer> {
|
|
|
|
//sets default values
|
|
const companies = options.companies && options.companies.length ? options.companies :
|
|
[ ExportCompany.TARGO, ExportCompany.SOLUCOM];
|
|
const types = options.type && options.type.length ? options.type :
|
|
Object.values(ExportType);
|
|
|
|
//collects all
|
|
const all = await this.csvService.collectTransaction(Number(periodId), companies);
|
|
|
|
//filters by type
|
|
const filtered = all.filter(r => {
|
|
switch (r.bank_code.toLocaleLowerCase()) {
|
|
case 'holiday' : return types.includes(ExportType.HOLIDAY);
|
|
case 'vacation' : return types.includes(ExportType.VACATION);
|
|
case 'sick-leave': return types.includes(ExportType.SICK_LEAVE);
|
|
case 'expenses' : return types.includes(ExportType.EXPENSES);
|
|
default : return types.includes(ExportType.SHIFTS);
|
|
}
|
|
});
|
|
|
|
//generating the csv file
|
|
return this.csvService.generateCsv(filtered);
|
|
}
|
|
|
|
} |