diff --git a/src/time-and-attendance/schedule-presets/schedule-presets.controller.ts b/src/time-and-attendance/schedule-presets/schedule-presets.controller.ts index 7da9c20..5fd2cbf 100644 --- a/src/time-and-attendance/schedule-presets/schedule-presets.controller.ts +++ b/src/time-and-attendance/schedule-presets/schedule-presets.controller.ts @@ -50,11 +50,22 @@ export class SchedulePresetsController { @Post('apply-preset') @ModuleAccessAllowed(ModulesEnum.timesheets) async applyPresetToTimesheet( - @Access('email') email: string, @Body('timesheet_id') timesheet_id: number, + @Access('email') email: string, + @Body('timesheet_id') timesheet_id: number, ) { return await this.applyService.applyPresetToTimesheet(email, timesheet_id); } + @Post('apply-preset/:employeeEmail') + @ModuleAccessAllowed(ModulesEnum.timesheets_approval) + async applyPresetToTimesheetFromApproval( + @Access('email') email: string, + @Body('timesheet_id') timesheet_id: number, + @Param('employeeEmail') employee_email: string, + ) { + return await this.applyService.applyPresetToTimesheet(email, timesheet_id, employee_email); + } + @Post('apply-day-preset') @ModuleAccessAllowed(ModulesEnum.timesheets) async applyPresetToDay( @@ -65,4 +76,16 @@ export class SchedulePresetsController { ) { return await this.applyService.applyPresetToDay(email, timesheet_id, week_day_index, date); } + + @Post('apply-day-preset/:employeeEmail') + @ModuleAccessAllowed(ModulesEnum.timesheets_approval) + async applyPresetToDayFromApproval( + @Access('email') email: string, + @Body('timesheet_id') timesheet_id: number, + @Body('week_day_index') week_day_index: number, + @Body('date') date: string, + @Param('employeeEmail') employee_email: string, + ) { + return await this.applyService.applyPresetToDay(email, timesheet_id, week_day_index, date, employee_email); + } } \ No newline at end of file diff --git a/src/time-and-attendance/schedule-presets/services/schedule-presets-apply.service.ts b/src/time-and-attendance/schedule-presets/services/schedule-presets-apply.service.ts index 97ad01c..5b3a756 100644 --- a/src/time-and-attendance/schedule-presets/services/schedule-presets-apply.service.ts +++ b/src/time-and-attendance/schedule-presets/services/schedule-presets-apply.service.ts @@ -24,8 +24,9 @@ export class SchedulePresetsApplyService { private readonly payPeriodEventService: PayPeriodEventService, ) { } - async applyPresetToTimesheet(email: string, timesheet_id: number): Promise> { - const employee_id = await this.emailResolver.findIdByEmail(email); + async applyPresetToTimesheet(email: string, timesheet_id: number, employee_email?: string): Promise> { + const user_email = employee_email ?? email; + const employee_id = await this.emailResolver.findIdByEmail(user_email); if (!employee_id.success) return { success: false, error: 'EMPLOYEE_NOT_FOUND' }; const employee_default_schedule_preset = await this.prisma.employees.findFirst({ @@ -76,16 +77,27 @@ export class SchedulePresetsApplyService { created_shifts.push(shift.data); } - const response = await this.shiftService.createOneOrManyShifts(email, created_shifts); - if (response.success) + const response = await this.shiftService.createOneOrManyShifts(user_email, created_shifts); + if (response.success) { + // notify timesheet-approval observers of changes + if (!employee_email) { + this.payPeriodEventService.emit({ + employee_email: user_email, + event_type: 'preset', + action: 'create', + }) + } + return { success: true, data: true }; + } else return { success: false, error: 'There was an error applying presets for this week' }; } - async applyPresetToDay(email: string, timesheet_id: number, week_day_index: number, date: string): Promise> { - const employee_id = await this.emailResolver.findIdByEmail(email); + async applyPresetToDay(email: string, timesheet_id: number, week_day_index: number, date: string, employee_email?: string): Promise> { + const user_email = employee_email ?? email; + const employee_id = await this.emailResolver.findIdByEmail(user_email); if (!employee_id.success) return { success: false, error: 'EMPLOYEE_NOT_FOUND' }; const week_day = Object.keys(WEEKDAY_MAP)[week_day_index]; @@ -118,16 +130,18 @@ export class SchedulePresetsApplyService { if (!created_shift.success) return { success: false, error: 'SHIFT_CREATE_FAILED' } - + await this.shiftService.createShift(employee_id.data, created_shift.data); } // notify timesheet-approval observers of changes - this.payPeriodEventService.emit({ - employee_email: email, - event_type: 'preset', - action: 'create', - }) + if (!employee_email) { + this.payPeriodEventService.emit({ + employee_email: user_email, + event_type: 'preset', + action: 'create', + }) + } return { success: true, data: true }; } @@ -145,7 +159,7 @@ export class SchedulePresetsApplyService { is_approved: false, is_remote: preset.is_remote!, }; - + return { success: true, data: shift }; } }