refactor: streamline time validation logic in ClassService
This commit is contained in:
parent
8e63816616
commit
341faa3bb0
@ -109,28 +109,26 @@ export class ClassService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const date = moment(createRoomClassDto.date);
|
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
|
const firstTimeStart = createRoomClassDto.times.reduce((prev, current) =>
|
||||||
if (firstTimeDateStart.isAfter(firsTimeDateEnd))
|
moment(current.start, "HH:mm").isBefore(moment(prev.start, "HH:mm"))
|
||||||
throw new HttpException(
|
? current
|
||||||
"The end time must be greater than the start time",
|
: prev,
|
||||||
400,
|
);
|
||||||
|
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(
|
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,
|
400,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user