refactor(expenses): added start_date to find the right timesheet using expense.date

This commit is contained in:
Matthieu Haineault 2025-10-31 12:37:11 -04:00
parent e5484da39a
commit 6c746aa3c2

View File

@ -1,6 +1,6 @@
import { CreateExpenseResult, UpdateExpensePayload, UpdateExpenseResult, DeleteExpenseResult, NormalizedExpense } from "src/time-and-attendance/utils/type.utils";
import { toDateFromString, toStringFromDate } from "src/time-and-attendance/utils/date-time.utils";
import { toDateFromString, toStringFromDate, weekStartSunday } from "src/time-and-attendance/utils/date-time.utils";
import { Injectable, NotFoundException, Req } from "@nestjs/common";
import { expense_select } from "src/time-and-attendance/utils/selects.utils";
import { PrismaService } from "src/prisma/prisma.service";
@ -27,13 +27,16 @@ export class ExpenseUpsertService {
//normalize strings and dates
const normed_expense = this.normalizeExpenseDto(dto);
//finds the timesheet using expense.date
const start_date = weekStartSunday(normed_expense.date);
//parse numbers
const parsed_amount = this.parseOptionalNumber(dto.amount, "amount");
const parsed_mileage = this.parseOptionalNumber(dto.mileage, "mileage");
const parsed_attachment = this.parseOptionalNumber(dto.attachment, "attachment");
const timesheet = await this.prisma.timesheets.findFirst({
where: { id: dto.timesheet_id, employee_id: employee_id },
where: { start_date, employee_id },
select: { id: true, employee_id: true },
});
if(!timesheet) throw new NotFoundException(`Timesheet with id ${dto.timesheet_id} not found`);