Skip to content

Commit

Permalink
Merge pull request #442 from tcet-opensource/validation-for-timetable
Browse files Browse the repository at this point in the history
[Added]: Validation for timetable
  • Loading branch information
TejasNair9977 authored Nov 8, 2023
2 parents aabfee3 + 641e092 commit 7633aaf
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions controller/timetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
} from "#services/timetable";
import { logger } from "#util";

import { isEntityIdValid } from "#middleware/entityIdValidation";
import Faculty from "#models/faculty";
import Group from "#models/group";
import ActivityBlueprint from "#models/activityBlueprint";

async function addTimetable(req, res) {
const {
startDate,
Expand All @@ -18,22 +23,36 @@ async function addTimetable(req, res) {
teabreakStartTime,
teabreakDuration,
} = req.body;

const isClassInchargeValid = await isEntityIdValid(classIncharge, Faculty);
const isGroupValid = await isEntityIdValid(group, Group);
const isActivityBlueprintValid = await isEntityIdValid(
activityBlueprints,
ActivityBlueprint,
);

try {
const newTimetable = await createTimetable(
startDate,
endDate,
classIncharge,
group,
activityBlueprints,
lunchbreakStartTime,
lunchbreakDuration,
teabreakStartTime,
teabreakDuration,
);
res.json({
res: `added timetable for: ${newTimetable.startDate} to ${newTimetable.endDate}`,
id: newTimetable.id,
});
if (!isClassInchargeValid || !isGroupValid || !isActivityBlueprintValid) {
res.status(400).json({
error: `Invalid IDs: ClassIncharge: ${isClassInchargeValid}, Group: ${isGroupValid}, ActivityBluePrint: ${isActivityBlueprintValid}`,
});
} else {
const newTimetable = await createTimetable(
startDate,
endDate,
classIncharge,
group,
activityBlueprints,
lunchbreakStartTime,
lunchbreakDuration,
teabreakStartTime,
teabreakDuration,
);
res.json({
res: `added timetable for: ${newTimetable.startDate} to ${newTimetable.endDate}`,
id: newTimetable.id,
});
}
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
Expand Down

0 comments on commit 7633aaf

Please sign in to comment.