From 8e63816616d12d095f66ececabc64bea549189d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Sat, 4 Jan 2025 18:53:46 +0100 Subject: [PATCH] refactor: improve code formatting and error messages in ClassService --- src/modules/class/class.service.ts | 64 +++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/modules/class/class.service.ts b/src/modules/class/class.service.ts index d7e1d6b..fd0bcaa 100644 --- a/src/modules/class/class.service.ts +++ b/src/modules/class/class.service.ts @@ -8,7 +8,7 @@ import * as moment from "moment"; @Injectable() export class ClassService { - constructor(private readonly prisma: PrismaService) { } + constructor(private readonly prisma: PrismaService) {} async create(createClassDto: CreateClassDto) { return await this.prisma.class.create({ data: createClassDto }); @@ -20,7 +20,7 @@ export class ClassService { cursor, where, orderBy, - include + include, }: { skip?: number; take?: number; @@ -35,7 +35,7 @@ export class ClassService { cursor, where, orderBy, - include + include, }); } @@ -68,12 +68,11 @@ export class ClassService { include: { Students: true }, }); - if (!Class) - throw new HttpException("Class not found", 404); + if (!Class) throw new HttpException("Class not found", 404); const studentIdsToAdd = studentIds.filter( (studentId) => - !Class.Students.some((student) => student.id === studentId) + !Class.Students.some((student) => student.id === studentId), ); if (studentIdsToAdd.length === 0) return Class; @@ -82,7 +81,9 @@ export class ClassService { where: { id: classId }, data: { Students: { - connect: studentIdsToAdd.map((studentId) => ({ id: studentId })), + connect: studentIdsToAdd.map((studentId) => ({ + id: studentId, + })), }, }, }); @@ -97,18 +98,41 @@ export class ClassService { async createRoom(classId: string, createRoomClassDto: CreateRoomClassDto) { // Check if end time is greater than start time - const invalidTime = createRoomClassDto.times.find( - (time) => moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm")) + const invalidTime = createRoomClassDto.times.find((time) => + moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm")), ); if (invalidTime) - throw new HttpException("Invalid time", 400); + throw new HttpException( + "The end time must be greater than the start time", + 400, + ); - // Check if date is in the past - const invalidDate = moment(createRoomClassDto.date).isBefore(moment()); + 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(), + }); - if (invalidDate) - throw new HttpException("Invalid date", 400); + // 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, + ); + + if (moment().isAfter(firsTimeDateEnd)) + throw new HttpException( + "The end time must be greater than the current time", + 400, + ); return await this.prisma.room.create({ include: { Times: true }, @@ -132,10 +156,14 @@ export class ClassService { } async getRooms(classId: string) { - return await this.prisma.class.findUnique({ - where: { id: classId }, - include: { ClassRoom: { include: { Times: true, Presentator: true } } }, - }).then((class_) => class_.ClassRoom); + return await this.prisma.class + .findUnique({ + where: { id: classId }, + include: { + ClassRoom: { include: { Times: true, Presentator: true } }, + }, + }) + .then((class_) => class_.ClassRoom); } async getUserClasses(userId: string) {