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')
|
@Patch('update')
|
||||||
update(@Body() dto: ExpenseDto): Promise<Result<ExpenseDto, string>> {
|
update(@Body() dto: ExpenseDto, @Req() req): Promise<Result<ExpenseDto, string>> {
|
||||||
return this.upsert_service.updateExpense(dto);
|
const email = req.user?.email;
|
||||||
|
return this.upsert_service.updateExpense(dto, email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('delete/:expense_id')
|
@Delete('delete/:expense_id')
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ export class ExpenseUpsertService {
|
||||||
|
|
||||||
//finds the timesheet using expense.date by finding the sunday
|
//finds the timesheet using expense.date by finding the sunday
|
||||||
const start_date = weekStartSunday(normed_expense.data.date);
|
const start_date = weekStartSunday(normed_expense.data.date);
|
||||||
|
|
||||||
const timesheet = await this.prisma.timesheets.findFirst({
|
const timesheet = await this.prisma.timesheets.findFirst({
|
||||||
where: { start_date, employee_id: employee_id.data },
|
where: { start_date, employee_id: employee_id.data },
|
||||||
select: { id: true, employee_id: true },
|
select: { id: true, employee_id: true },
|
||||||
|
|
@ -72,18 +72,25 @@ export class ExpenseUpsertService {
|
||||||
//_________________________________________________________________
|
//_________________________________________________________________
|
||||||
// UPDATE
|
// UPDATE
|
||||||
//_________________________________________________________________
|
//_________________________________________________________________
|
||||||
async updateExpense(dto: ExpenseDto): Promise<Result<ExpenseDto, string>> {
|
async updateExpense(dto: ExpenseDto, email: string): Promise<Result<ExpenseDto, string>> {
|
||||||
try {
|
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
|
//normalize string , date format and parse numbers
|
||||||
const normed_expense = await this.normalizeAndParseExpenseDto(dto);
|
const normed_expense = await this.normalizeAndParseExpenseDto(dto);
|
||||||
if (!normed_expense.success) return { success: false, error: normed_expense.error }
|
if (!normed_expense.success) return { success: false, error: normed_expense.error }
|
||||||
|
|
||||||
const timesheet = await this.prisma.timesheets.findUnique({
|
//added timesheet_id modification check according to the new date
|
||||||
where: { id: dto.timesheet_id },
|
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,
|
select: timesheet_select,
|
||||||
});
|
});
|
||||||
if (!timesheet) return { success: false, error: `Timesheet ${dto.timesheet_id} not found` }
|
if (!timesheet) return { success: false, error: `Timesheet ${dto.timesheet_id} not found` }
|
||||||
|
|
||||||
|
|
||||||
//checks for modifications
|
//checks for modifications
|
||||||
const data: Prisma.ExpensesUpdateInput = {
|
const data: Prisma.ExpensesUpdateInput = {
|
||||||
...normed_expense.data,
|
...normed_expense.data,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user