fix(expenses): fix date update and make sure the timesheet_id is updated when the date changes week
This commit is contained in:
parent
194a12d7ab
commit
2958403f08
|
|
@ -19,8 +19,9 @@ export class ExpenseController {
|
|||
}
|
||||
|
||||
@Patch('update')
|
||||
update(@Body() dto: ExpenseDto): Promise<Result<ExpenseDto, string>> {
|
||||
return this.upsert_service.updateExpense(dto);
|
||||
update(@Body() dto: ExpenseDto, @Req() req): Promise<Result<ExpenseDto, string>> {
|
||||
const email = req.user?.email;
|
||||
return this.upsert_service.updateExpense(dto, email);
|
||||
}
|
||||
|
||||
@Delete('delete/:expense_id')
|
||||
|
|
|
|||
|
|
@ -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<Result<ExpenseDto, string>> {
|
||||
async updateExpense(dto: ExpenseDto, email: string): Promise<Result<ExpenseDto, string>> {
|
||||
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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user