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": {
"operationId": "TimesheetController_getTimesheetByIds",
"parameters": [
{
"name": "employee_email",
"required": true,
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "year",
"required": true,
"in": "query",
"schema": {
"type": "string"
"type": "number"
}
},
{
@ -363,7 +355,7 @@
"required": true,
"in": "query",
"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 { BadRequestException, Controller, Get, Query} from "@nestjs/common";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
@Controller('timesheets')
export class TimesheetController {
constructor(
private readonly timesheetOverview: GetTimesheetsOverviewService,
private readonly emailResolver: EmailToIdResolver,
){}
constructor( private readonly timesheetOverview: GetTimesheetsOverviewService ){}
@Get()
async getTimesheetByIds(
@Query('employee_email') employee_email: string,
@Query('year') year: string,
@Query('period_number') period_number: string,
@Req() req,
@Query('year', ParseIntPipe) year: number,
@Query('period_number', ParseIntPipe) period_number: number,
) {
if (!employee_email || !year || !period_number) {
throw new BadRequestException('Query params "employee_email", "year" and eriod_number" are required.');
}
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);
const email = req.user?.email;
if(!email) throw new UnauthorizedException('Unauthorized User');
return this.timesheetOverview.getTimesheetsForEmployeeByPeriod(email, year, period_number);
}
}

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 { TotalExpenses, TotalHours } from "src/time-and-attendance/utils/type.utils";
import { PrismaService } from "src/prisma/prisma.service";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
@Injectable()
export class GetTimesheetsOverviewService {
constructor(private readonly prisma: PrismaService) { }
constructor(
private readonly prisma: PrismaService,
private readonly emailResolver : EmailToIdResolver,
) { }
//-----------------------------------------------------------------------------------
// 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
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`);
//fetch the employee_id using the email
const employee_id = await this.emailResolver.findIdByEmail(email);
//loads the timesheets related to the fetched pay-period
const timesheet_range = { employee_id, start_date: { gte: period.period_start, lte: period.period_end } };
let rows = await this.loadTimesheets(timesheet_range);