diff --git a/src/time-and-attendance/expenses/controllers/expense.controller.ts b/src/time-and-attendance/expenses/controllers/expense.controller.ts index 3a6c138..9f32e46 100644 --- a/src/time-and-attendance/expenses/controllers/expense.controller.ts +++ b/src/time-and-attendance/expenses/controllers/expense.controller.ts @@ -19,8 +19,9 @@ export class ExpenseController { } @Patch('update') - update(@Body() dto: ExpenseDto): Promise> { - return this.upsert_service.updateExpense(dto); + update(@Body() dto: ExpenseDto, @Req() req): Promise> { + const email = req.user?.email; + return this.upsert_service.updateExpense(dto, email); } @Delete('delete/:expense_id') diff --git a/src/time-and-attendance/expenses/services/expense-upsert.service.ts b/src/time-and-attendance/expenses/services/expense-upsert.service.ts index c7b4db8..422e418 100644 --- a/src/time-and-attendance/expenses/services/expense-upsert.service.ts +++ b/src/time-and-attendance/expenses/services/expense-upsert.service.ts @@ -33,7 +33,7 @@ export class ExpenseUpsertService { //finds the timesheet using expense.date by finding the sunday const start_date = weekStartSunday(normed_expense.data.date); - + const timesheet = await this.prisma.timesheets.findFirst({ where: { start_date, employee_id: employee_id.data }, select: { id: true, employee_id: true }, @@ -72,18 +72,25 @@ export class ExpenseUpsertService { //_________________________________________________________________ // UPDATE //_________________________________________________________________ - async updateExpense(dto: ExpenseDto): Promise> { + async updateExpense(dto: ExpenseDto, email: string): Promise> { try { + //fetch employee_id using req.user.email + const employee_id = await this.emailResolver.findIdByEmail(email); + if (!employee_id.success) return { success: false, error: employee_id.error }; //normalize string , date format and parse numbers const normed_expense = await this.normalizeAndParseExpenseDto(dto); if (!normed_expense.success) return { success: false, error: normed_expense.error } - const timesheet = await this.prisma.timesheets.findUnique({ - where: { id: dto.timesheet_id }, + //added timesheet_id modification check according to the new date + const new_timesheet_start_date = weekStartSunday(toDateFromString(dto.date)); + + const timesheet = await this.prisma.timesheets.findFirst({ + where: { start_date: new_timesheet_start_date, employee_id: employee_id.data }, select: timesheet_select, }); if (!timesheet) return { success: false, error: `Timesheet ${dto.timesheet_id} not found` } + //checks for modifications const data: Prisma.ExpensesUpdateInput = { ...normed_expense.data,