fix(shift): fix minor issue with naming property (changed to shift_id from id)
This commit is contained in:
parent
be00798961
commit
88d4f2fe27
|
|
@ -19,6 +19,8 @@ import { writeFileSync } from 'fs';
|
||||||
import * as session from 'express-session';
|
import * as session from 'express-session';
|
||||||
import * as passport from 'passport';
|
import * as passport from 'passport';
|
||||||
|
|
||||||
|
const SESSION_TOKEN_DURATION_MINUTES = 180
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
|
|
||||||
|
|
@ -37,7 +39,7 @@ async function bootstrap() {
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
rolling: true,
|
rolling: true,
|
||||||
cookie: {
|
cookie: {
|
||||||
maxAge: 30 * 60 * 1000,
|
maxAge: SESSION_TOKEN_DURATION_MINUTES * 60 * 1000, // property maxAge requires milliseconds
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -7,5 +7,5 @@ export class UpdateShiftDto extends PartialType(
|
||||||
OmitType(ShiftDto, ['is_approved', 'timesheet_id'] as const),
|
OmitType(ShiftDto, ['is_approved', 'timesheet_id'] as const),
|
||||||
) {
|
) {
|
||||||
@IsInt()
|
@IsInt()
|
||||||
id!: number;
|
shift_id!: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -239,8 +239,9 @@ export class ShiftsUpsertService {
|
||||||
if (!Array.isArray(dtos) || dtos.length === 0) return [];
|
if (!Array.isArray(dtos) || dtos.length === 0) return [];
|
||||||
|
|
||||||
const updates: UpdateShiftPayload[] = await Promise.all(dtos.map((item) => {
|
const updates: UpdateShiftPayload[] = await Promise.all(dtos.map((item) => {
|
||||||
const { id, ...rest } = item;
|
const { shift_id, ...rest } = item;
|
||||||
if (!Number.isInteger(id)) throw new ConflictException({ error_code: 'INVALID_SHIFT'});
|
console.log('id received: ', shift_id);
|
||||||
|
if (!Number.isInteger(shift_id)) throw new ConflictException({ error_code: 'INVALID_SHIFT'});
|
||||||
|
|
||||||
const changes: UpdateShiftChanges = {};
|
const changes: UpdateShiftChanges = {};
|
||||||
if (rest.date !== undefined) changes.date = rest.date;
|
if (rest.date !== undefined) changes.date = rest.date;
|
||||||
|
|
@ -250,11 +251,11 @@ export class ShiftsUpsertService {
|
||||||
if (rest.is_remote !== undefined) changes.is_remote = rest.is_remote;
|
if (rest.is_remote !== undefined) changes.is_remote = rest.is_remote;
|
||||||
if (rest.comment !== undefined) changes.comment = rest.comment;
|
if (rest.comment !== undefined) changes.comment = rest.comment;
|
||||||
|
|
||||||
return { id, dto: changes };
|
return { shift_id, dto: changes };
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const shift_ids = updates.map(update_shift => update_shift.id);
|
const shift_ids = updates.map(update_shift => update_shift.shift_id);
|
||||||
const rows = await tx.shifts.findMany({
|
const rows = await tx.shifts.findMany({
|
||||||
where: { id: { in: shift_ids } },
|
where: { id: { in: shift_ids } },
|
||||||
select: shift_select,
|
select: shift_select,
|
||||||
|
|
@ -262,21 +263,21 @@ export class ShiftsUpsertService {
|
||||||
const regroup_id = new Map(rows.map(r => [r.id, r]));
|
const regroup_id = new Map(rows.map(r => [r.id, r]));
|
||||||
|
|
||||||
for (const update of updates) {
|
for (const update of updates) {
|
||||||
const existing = regroup_id.get(update.id);
|
const existing = regroup_id.get(update.shift_id);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
return updates.map(exist => exist.id === update.id
|
return updates.map(exist => exist.shift_id === update.shift_id
|
||||||
? ({ ok: false, id: update.id, error: new NotFoundException(`Shift with id: ${update.id} not found`) } as UpdateShiftResult)
|
? ({ ok: false, id: update.shift_id, error: new NotFoundException(`Shift with id: ${update.shift_id} not found`) } as UpdateShiftResult)
|
||||||
: ({ ok: false, id: exist.id, error: new BadRequestException('Batch aborted due to missing shift') }));
|
: ({ ok: false, id: exist.shift_id, error: new BadRequestException('Batch aborted due to missing shift') }));
|
||||||
}
|
}
|
||||||
if (existing.is_approved) {
|
if (existing.is_approved) {
|
||||||
return updates.map(exist => exist.id === update.id
|
return updates.map(exist => exist.shift_id === update.shift_id
|
||||||
? ({ ok: false, id: update.id, error: new BadRequestException('Approved shift cannot be updated') } as UpdateShiftResult)
|
? ({ ok: false, id: update.shift_id, error: new BadRequestException('Approved shift cannot be updated') } as UpdateShiftResult)
|
||||||
: ({ ok: false, id: exist.id, error: new BadRequestException('Batch aborted due to approved shift in update set') }));
|
: ({ ok: false, id: exist.shift_id, error: new BadRequestException('Batch aborted due to approved shift in update set') }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const planned_updates = updates.map(update => {
|
const planned_updates = updates.map(update => {
|
||||||
const exist_shift = regroup_id.get(update.id)!;
|
const exist_shift = regroup_id.get(update.shift_id)!;
|
||||||
const date_string = update.dto.date ?? toStringFromDate(exist_shift.date);
|
const date_string = update.dto.date ?? toStringFromDate(exist_shift.date);
|
||||||
const start_string = update.dto.start_time ?? toStringFromHHmm(exist_shift.start_time);
|
const start_string = update.dto.start_time ?? toStringFromHHmm(exist_shift.start_time);
|
||||||
const end_string = update.dto.end_time ?? toStringFromHHmm(exist_shift.end_time);
|
const end_string = update.dto.end_time ?? toStringFromHHmm(exist_shift.end_time);
|
||||||
|
|
@ -325,9 +326,9 @@ export class ShiftsUpsertService {
|
||||||
);
|
);
|
||||||
if (conflict) {
|
if (conflict) {
|
||||||
return updates.map(exist =>
|
return updates.map(exist =>
|
||||||
exist.id === planned.exist_shift.id
|
exist.shift_id === planned.exist_shift.id
|
||||||
? ({
|
? ({
|
||||||
ok: false, id: exist.id, error:{
|
ok: false, id: exist.shift_id, error:{
|
||||||
error_code: 'SHIFT_OVERLAP',
|
error_code: 'SHIFT_OVERLAP',
|
||||||
conflicts: {
|
conflicts: {
|
||||||
start_time: toStringFromHHmm(conflict.start),
|
start_time: toStringFromHHmm(conflict.start),
|
||||||
|
|
@ -336,7 +337,7 @@ export class ShiftsUpsertService {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} as UpdateShiftResult)
|
} as UpdateShiftResult)
|
||||||
: ({ ok: false, id: exist.id, error: new BadRequestException('Batch aborted due to overlap in another update') })
|
: ({ ok: false, id: exist.shift_id, error: new BadRequestException('Batch aborted due to overlap in another update') })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -369,7 +370,7 @@ export class ShiftsUpsertService {
|
||||||
},
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
return updates.map(exist => ({ ok: false, id: exist.id, error: error }));
|
return updates.map(exist => ({ ok: false, id: exist.shift_id, error: error }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,8 @@ export type ShiftWithOvertimeDto = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CreateShiftResult = { ok: true; data: ShiftWithOvertimeDto } | { ok: false; error: any };
|
export type CreateShiftResult = { ok: true; data: ShiftWithOvertimeDto } | { ok: false; error: any };
|
||||||
export type UpdateShiftChanges = Omit<UpdateShiftDto, 'id'>;
|
export type UpdateShiftChanges = Omit<UpdateShiftDto, 'shift_id'>;
|
||||||
export type UpdateShiftPayload = { id: number; dto: UpdateShiftChanges };
|
export type UpdateShiftPayload = { shift_id: number; dto: UpdateShiftChanges };
|
||||||
export type UpdateShiftResult = { ok: true; id: number; data: ShiftWithOvertimeDto } | { ok: false; id: number; error: any };
|
export type UpdateShiftResult = { ok: true; id: number; data: ShiftWithOvertimeDto } | { ok: false; id: number; error: any };
|
||||||
export type DeleteShiftResult = { ok: true; id: number; overtime: WeekOvertimeSummary } | { ok: false; id: number; error: any };
|
export type DeleteShiftResult = { ok: true; id: number; overtime: WeekOvertimeSummary } | { ok: false; id: number; error: any };
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user