Skip to content

Commit 7633aaf

Browse files
Merge pull request #442 from tcet-opensource/validation-for-timetable
[Added]: Validation for timetable
2 parents aabfee3 + 641e092 commit 7633aaf

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

controller/timetable.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import {
66
} from "#services/timetable";
77
import { logger } from "#util";
88

9+
import { isEntityIdValid } from "#middleware/entityIdValidation";
10+
import Faculty from "#models/faculty";
11+
import Group from "#models/group";
12+
import ActivityBlueprint from "#models/activityBlueprint";
13+
914
async function addTimetable(req, res) {
1015
const {
1116
startDate,
@@ -18,22 +23,36 @@ async function addTimetable(req, res) {
1823
teabreakStartTime,
1924
teabreakDuration,
2025
} = req.body;
26+
27+
const isClassInchargeValid = await isEntityIdValid(classIncharge, Faculty);
28+
const isGroupValid = await isEntityIdValid(group, Group);
29+
const isActivityBlueprintValid = await isEntityIdValid(
30+
activityBlueprints,
31+
ActivityBlueprint,
32+
);
33+
2134
try {
22-
const newTimetable = await createTimetable(
23-
startDate,
24-
endDate,
25-
classIncharge,
26-
group,
27-
activityBlueprints,
28-
lunchbreakStartTime,
29-
lunchbreakDuration,
30-
teabreakStartTime,
31-
teabreakDuration,
32-
);
33-
res.json({
34-
res: `added timetable for: ${newTimetable.startDate} to ${newTimetable.endDate}`,
35-
id: newTimetable.id,
36-
});
35+
if (!isClassInchargeValid || !isGroupValid || !isActivityBlueprintValid) {
36+
res.status(400).json({
37+
error: `Invalid IDs: ClassIncharge: ${isClassInchargeValid}, Group: ${isGroupValid}, ActivityBluePrint: ${isActivityBlueprintValid}`,
38+
});
39+
} else {
40+
const newTimetable = await createTimetable(
41+
startDate,
42+
endDate,
43+
classIncharge,
44+
group,
45+
activityBlueprints,
46+
lunchbreakStartTime,
47+
lunchbreakDuration,
48+
teabreakStartTime,
49+
teabreakDuration,
50+
);
51+
res.json({
52+
res: `added timetable for: ${newTimetable.startDate} to ${newTimetable.endDate}`,
53+
id: newTimetable.id,
54+
});
55+
}
3756
} catch (error) {
3857
logger.error("Error while inserting", error);
3958
res.status(500);

0 commit comments

Comments
 (0)