refactor: improve code formatting and error messages in ClassService
This commit is contained in:
parent
db88a24028
commit
8e63816616
@ -20,7 +20,7 @@ export class ClassService {
|
|||||||
cursor,
|
cursor,
|
||||||
where,
|
where,
|
||||||
orderBy,
|
orderBy,
|
||||||
include
|
include,
|
||||||
}: {
|
}: {
|
||||||
skip?: number;
|
skip?: number;
|
||||||
take?: number;
|
take?: number;
|
||||||
@ -35,7 +35,7 @@ export class ClassService {
|
|||||||
cursor,
|
cursor,
|
||||||
where,
|
where,
|
||||||
orderBy,
|
orderBy,
|
||||||
include
|
include,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,12 +68,11 @@ export class ClassService {
|
|||||||
include: { Students: true },
|
include: { Students: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!Class)
|
if (!Class) throw new HttpException("Class not found", 404);
|
||||||
throw new HttpException("Class not found", 404);
|
|
||||||
|
|
||||||
const studentIdsToAdd = studentIds.filter(
|
const studentIdsToAdd = studentIds.filter(
|
||||||
(studentId) =>
|
(studentId) =>
|
||||||
!Class.Students.some((student) => student.id === studentId)
|
!Class.Students.some((student) => student.id === studentId),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (studentIdsToAdd.length === 0) return Class;
|
if (studentIdsToAdd.length === 0) return Class;
|
||||||
@ -82,7 +81,9 @@ export class ClassService {
|
|||||||
where: { id: classId },
|
where: { id: classId },
|
||||||
data: {
|
data: {
|
||||||
Students: {
|
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) {
|
async createRoom(classId: string, createRoomClassDto: CreateRoomClassDto) {
|
||||||
// Check if end time is greater than start time
|
// Check if end time is greater than start time
|
||||||
const invalidTime = createRoomClassDto.times.find(
|
const invalidTime = createRoomClassDto.times.find((time) =>
|
||||||
(time) => moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm"))
|
moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm")),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (invalidTime)
|
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 date = moment(createRoomClassDto.date);
|
||||||
const invalidDate = moment(createRoomClassDto.date).isBefore(moment());
|
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)
|
// Check if start time is greater than end time
|
||||||
throw new HttpException("Invalid date", 400);
|
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({
|
return await this.prisma.room.create({
|
||||||
include: { Times: true },
|
include: { Times: true },
|
||||||
@ -132,10 +156,14 @@ export class ClassService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getRooms(classId: string) {
|
async getRooms(classId: string) {
|
||||||
return await this.prisma.class.findUnique({
|
return await this.prisma.class
|
||||||
|
.findUnique({
|
||||||
where: { id: classId },
|
where: { id: classId },
|
||||||
include: { ClassRoom: { include: { Times: true, Presentator: true } } },
|
include: {
|
||||||
}).then((class_) => class_.ClassRoom);
|
ClassRoom: { include: { Times: true, Presentator: true } },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((class_) => class_.ClassRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserClasses(userId: string) {
|
async getUserClasses(userId: string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user