refactor(timesheet): used session data and removed email from query of the Get function

This commit is contained in:
Matthieu Haineault 2025-10-31 14:04:07 -04:00
parent 6c746aa3c2
commit bb60887a0d
3 changed files with 18 additions and 28 deletions

View File

@ -342,20 +342,12 @@
"get": { "get": {
"operationId": "TimesheetController_getTimesheetByIds", "operationId": "TimesheetController_getTimesheetByIds",
"parameters": [ "parameters": [
{
"name": "employee_email",
"required": true,
"in": "query",
"schema": {
"type": "string"
}
},
{ {
"name": "year", "name": "year",
"required": true, "required": true,
"in": "query", "in": "query",
"schema": { "schema": {
"type": "string" "type": "number"
} }
}, },
{ {
@ -363,7 +355,7 @@
"required": true, "required": true,
"in": "query", "in": "query",
"schema": { "schema": {
"type": "string" "type": "number"
} }
} }
], ],

View File

@ -1,26 +1,18 @@
import { Controller, Get, ParseIntPipe, Query, Req, UnauthorizedException} from "@nestjs/common";
import { GetTimesheetsOverviewService } from "src/time-and-attendance/modules/time-tracker/timesheets/services/timesheet-get-overview.service"; import { GetTimesheetsOverviewService } from "src/time-and-attendance/modules/time-tracker/timesheets/services/timesheet-get-overview.service";
import { BadRequestException, Controller, Get, Query} from "@nestjs/common";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
@Controller('timesheets') @Controller('timesheets')
export class TimesheetController { export class TimesheetController {
constructor( constructor( private readonly timesheetOverview: GetTimesheetsOverviewService ){}
private readonly timesheetOverview: GetTimesheetsOverviewService,
private readonly emailResolver: EmailToIdResolver,
){}
@Get() @Get()
async getTimesheetByIds( async getTimesheetByIds(
@Query('employee_email') employee_email: string, @Req() req,
@Query('year') year: string, @Query('year', ParseIntPipe) year: number,
@Query('period_number') period_number: string, @Query('period_number', ParseIntPipe) period_number: number,
) { ) {
if (!employee_email || !year || !period_number) { const email = req.user?.email;
throw new BadRequestException('Query params "employee_email", "year" and eriod_number" are required.'); if(!email) throw new UnauthorizedException('Unauthorized User');
} return this.timesheetOverview.getTimesheetsForEmployeeByPeriod(email, year, period_number);
const employee_id = await this.emailResolver.findIdByEmail(employee_email);
const pay_year = Number(year);
const period_num = Number(period_number);
return this.timesheetOverview.getTimesheetsForEmployeeByPeriod(employee_id, pay_year, period_num);
} }
} }

View File

@ -3,19 +3,25 @@ import { NUMBER_OF_TIMESHEETS_TO_RETURN } from "src/time-and-attendance/utils/co
import { Injectable, NotFoundException } from "@nestjs/common"; import { Injectable, NotFoundException } from "@nestjs/common";
import { TotalExpenses, TotalHours } from "src/time-and-attendance/utils/type.utils"; import { TotalExpenses, TotalHours } from "src/time-and-attendance/utils/type.utils";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
@Injectable() @Injectable()
export class GetTimesheetsOverviewService { export class GetTimesheetsOverviewService {
constructor(private readonly prisma: PrismaService) { } constructor(
private readonly prisma: PrismaService,
private readonly emailResolver : EmailToIdResolver,
) { }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// GET TIMESHEETS FOR A SELECTED EMPLOYEE // GET TIMESHEETS FOR A SELECTED EMPLOYEE
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
async getTimesheetsForEmployeeByPeriod(employee_id: number, pay_year: number, pay_period_no: number) { async getTimesheetsForEmployeeByPeriod(email: string, pay_year: number, pay_period_no: number) {
//find period using year and period_no //find period using year and period_no
const period = await this.prisma.payPeriods.findFirst({ where: { pay_year, pay_period_no } }); const period = await this.prisma.payPeriods.findFirst({ where: { pay_year, pay_period_no } });
if (!period) throw new NotFoundException(`Pay period ${pay_year}-${pay_period_no} not found`); if (!period) throw new NotFoundException(`Pay period ${pay_year}-${pay_period_no} not found`);
//fetch the employee_id using the email
const employee_id = await this.emailResolver.findIdByEmail(email);
//loads the timesheets related to the fetched pay-period //loads the timesheets related to the fetched pay-period
const timesheet_range = { employee_id, start_date: { gte: period.period_start, lte: period.period_end } }; const timesheet_range = { employee_id, start_date: { gte: period.period_start, lte: period.period_end } };
let rows = await this.loadTimesheets(timesheet_range); let rows = await this.loadTimesheets(timesheet_range);