feat(presets): small ajustements
This commit is contained in:
parent
03d9fa2cf4
commit
6332a42fa7
|
|
@ -55,10 +55,10 @@ export class SchedulePresetsController {
|
||||||
@Post('apply-presets')
|
@Post('apply-presets')
|
||||||
async applyPresets(
|
async applyPresets(
|
||||||
@Req() req,
|
@Req() req,
|
||||||
@Body('preset') preset_name: string,
|
@Body('preset') preset_id: number,
|
||||||
@Body('start') start_date: string
|
@Body('start') start_date: string
|
||||||
) {
|
) {
|
||||||
const email = req.user?.email;
|
const email = req.user?.email;
|
||||||
return this.applyPresetsService.applyToTimesheet(email, preset_name, start_date);
|
return this.applyPresetsService.applyToTimesheet(email, preset_id, start_date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
import { ArrayMinSize, IsArray, IsBoolean, IsOptional, IsString } from "class-validator";
|
import { ArrayMinSize, IsArray, IsBoolean, IsInt, IsOptional, IsString } from "class-validator";
|
||||||
import { SchedulePresetShiftsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-preset-shifts.dto";
|
import { SchedulePresetShiftsDto } from "src/time-and-attendance/time-tracker/schedule-presets/dtos/create-schedule-preset-shifts.dto";
|
||||||
|
|
||||||
export class SchedulePresetsDto {
|
export class SchedulePresetsDto {
|
||||||
|
|
||||||
|
@IsInt()
|
||||||
|
id!: number;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
name!: string;
|
name!: string;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,18 +11,19 @@ import { EmailToIdResolver } from "src/time-and-attendance/utils/resolve-email-i
|
||||||
export class SchedulePresetsApplyService {
|
export class SchedulePresetsApplyService {
|
||||||
constructor( private readonly prisma: PrismaService, private readonly emailResolver: EmailToIdResolver) {}
|
constructor( private readonly prisma: PrismaService, private readonly emailResolver: EmailToIdResolver) {}
|
||||||
|
|
||||||
async applyToTimesheet( email: string, preset_name: string, start_date_iso: string ): Promise<ApplyResult> {
|
async applyToTimesheet( email: string, id: number, start_date_iso: string ): Promise<ApplyResult> {
|
||||||
if(!preset_name?.trim()) throw new BadRequestException('A preset_name is required');
|
if(!id) throw new BadRequestException(`Schedule preset with id: ${id} not found`);
|
||||||
if(!DATE_ISO_FORMAT.test(start_date_iso)) throw new BadRequestException('start_date must be of format :YYYY-MM-DD');
|
if(!DATE_ISO_FORMAT.test(start_date_iso)) throw new BadRequestException('start_date must be of format :YYYY-MM-DD');
|
||||||
|
|
||||||
const employee_id = await this.emailResolver.findIdByEmail(email);
|
const employee_id = await this.emailResolver.findIdByEmail(email);
|
||||||
|
|
||||||
const preset = await this.prisma.schedulePresets.findFirst({
|
const preset = await this.prisma.schedulePresets.findFirst({
|
||||||
where: { employee_id, name: preset_name },
|
where: { employee_id, id },
|
||||||
include: {
|
include: {
|
||||||
shifts: {
|
shifts: {
|
||||||
orderBy: [{ week_day: 'asc'}, { sort_order: 'asc'}],
|
orderBy: [{ week_day: 'asc'}, { sort_order: 'asc'}],
|
||||||
select: {
|
select: {
|
||||||
|
id: true,
|
||||||
week_day: true,
|
week_day: true,
|
||||||
sort_order: true,
|
sort_order: true,
|
||||||
start_time: true,
|
start_time: true,
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ export class SchedulePresetsUpsertService {
|
||||||
}
|
}
|
||||||
const created = await tx.schedulePresets.create({
|
const created = await tx.schedulePresets.create({
|
||||||
data: {
|
data: {
|
||||||
|
id: dto.id,
|
||||||
employee_id,
|
employee_id,
|
||||||
name: dto.name,
|
name: dto.name,
|
||||||
is_default: !!dto.is_default,
|
is_default: !!dto.is_default,
|
||||||
|
|
@ -108,14 +109,17 @@ export class SchedulePresetsUpsertService {
|
||||||
|
|
||||||
const saved = await this.prisma.schedulePresets.findUnique({
|
const saved = await this.prisma.schedulePresets.findUnique({
|
||||||
where: { id: existing.id },
|
where: { id: existing.id },
|
||||||
include: { shifts: {
|
include: {
|
||||||
|
shifts: {
|
||||||
orderBy: [{ week_day: 'asc' }, { sort_order: 'asc' }],
|
orderBy: [{ week_day: 'asc' }, { sort_order: 'asc' }],
|
||||||
include: { bank_code: { select: { type: true } } },
|
include: { bank_code: { select: { type: true } } },
|
||||||
}},
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (!saved) throw new NotFoundException(`Preset with id: ${existing.id} not found`);
|
if (!saved) throw new NotFoundException(`Preset with id: ${existing.id} not found`);
|
||||||
|
|
||||||
const response_dto: SchedulePresetsDto = {
|
const response_dto: SchedulePresetsDto = {
|
||||||
|
id: saved.id,
|
||||||
name: saved.name,
|
name: saved.name,
|
||||||
is_default: saved.is_default,
|
is_default: saved.is_default,
|
||||||
preset_shifts: saved.shifts.map((shift) => ({
|
preset_shifts: saved.shifts.map((shift) => ({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user