targo-backend/src/modules/shifts/controllers/shifts.controller.ts
2025-10-14 10:38:24 -04:00

87 lines
3.2 KiB
TypeScript

import { Body, Controller, Delete, Get, Header, Param, ParseBoolPipe, ParseIntPipe, Patch, Put, Query, } from "@nestjs/common";
import { RolesAllowed } from "src/common/decorators/roles.decorators";
import { Roles as RoleEnum } from '.prisma/client';
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { ShiftsCommandService } from "../services/shifts-command.service";
import { ShiftsQueryService } from "../services/shifts-query.service";
import { GetShiftsOverviewDto } from "../dtos/get-shift-overview.dto";
import { ShiftPayloadDto, UpsertShiftDto } from "../dtos/upsert-shift.dto";
import { OverviewRow } from "../types-and-interfaces/shifts-overview-row.interface";
import { UpsertAction } from "src/modules/shared/types/upsert-actions.types";
@ApiTags('Shifts')
@ApiBearerAuth('access-token')
// @UseGuards()
@Controller('shifts')
export class ShiftsController {
constructor(
private readonly shiftsService: ShiftsQueryService,
private readonly shiftsCommandService: ShiftsCommandService,
){}
@Put('upsert/:email')
async upsert_by_date(
@Param('email') email_param: string,
@Query('action') action: UpsertAction,
@Body() payload: UpsertShiftDto,
) {
return this.shiftsCommandService.upsertShifts(email_param, action, payload);
}
@Delete('delete/:email/:date')
async remove(
@Param('email') email: string,
@Param('date') date: string,
@Body() payload: UpsertShiftDto,
) {
return this.shiftsCommandService.deleteShift(email, date, payload);
}
@Patch('approval/:id')
//@RolesAllowed(RoleEnum.ACCOUNTING, RoleEnum.ADMIN, RoleEnum.HR, RoleEnum.SUPERVISOR)
async approve(@Param('id', ParseIntPipe) id: number, @Body('is_approved', ParseBoolPipe) isApproved: boolean) {
return this.shiftsCommandService.updateApproval(id, isApproved);
}
@Get('summary')
async getSummary( @Query() query: GetShiftsOverviewDto): Promise<OverviewRow[]> {
return this.shiftsService.getSummary(query.period_id);
}
@Get('export.csv')
@Header('Content-Type', 'text/csv; charset=utf-8')
@Header('Content-Disposition', 'attachment; filename="shifts-validation.csv"')
async exportCsv(@Query() query: GetShiftsOverviewDto): Promise<Buffer>{
const rows = await this.shiftsService.getSummary(query.period_id);
//CSV Headers
const header = [
'full_name',
'supervisor',
'total_regular_hrs',
'total_evening_hrs',
'total_overtime_hrs',
'total_expenses',
'total_mileage',
'is_validated'
].join(',') + '\n';
//CSV rows
const body = rows.map(r => {
const esc = (str: string) => `"${str.replace(/"/g, '""')}"`;
return [
esc(r.full_name),
esc(r.supervisor),
r.total_regular_hrs.toFixed(2),
r.total_evening_hrs.toFixed(2),
r.total_overtime_hrs.toFixed(2),
r.total_expenses.toFixed(2),
r.total_mileage.toFixed(2),
r.is_approved,
].join(',');
}).join('\n');
return Buffer.from('\uFEFF' + header + body, 'utf8');
}
}