refactor: streamline time validation logic in ClassService

This commit is contained in:
Rémi 2025-01-04 19:02:41 +01:00
parent 8e63816616
commit 341faa3bb0

View File

@ -109,28 +109,26 @@ export class ClassService {
);
const date = moment(createRoomClassDto.date);
const firstTimeDateStart = moment(date).set({
hour: moment(createRoomClassDto.times[0].start, "HH:mm").hours(),
minute: moment(
createRoomClassDto.times[0].start,
"HH:mm",
).minutes(),
});
const firsTimeDateEnd = moment(date).set({
hour: moment(createRoomClassDto.times[0].end, "HH:mm").hours(),
minute: moment(createRoomClassDto.times[0].end, "HH:mm").minutes(),
});
// Check if start time is greater than end time
if (firstTimeDateStart.isAfter(firsTimeDateEnd))
throw new HttpException(
"The end time must be greater than the start time",
400,
);
const firstTimeStart = createRoomClassDto.times.reduce((prev, current) =>
moment(current.start, "HH:mm").isBefore(moment(prev.start, "HH:mm"))
? current
: prev,
);
const lastTimeEnd = createRoomClassDto.times.reduce((prev, current) =>
moment(current.end, "HH:mm").isAfter(moment(prev.end, "HH:mm"))
? current
: prev,
);
if (moment().isAfter(firsTimeDateEnd))
if (
moment().isBefore(
moment(firstTimeStart.start, "HH:mm").subtract(5, "minutes"),
) ||
moment().isAfter(moment(lastTimeEnd.end, "HH:mm"))
)
throw new HttpException(
"The end time must be greater than the current time",
"Can't create a room for a class that has already ended",
400,
);