Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

052 Dec 5: Iterating the Assessment Tracker and Compliance Tracker #330

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 73 additions & 14 deletions Servers/controllers/assessment.ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
getAssessmentByIdQuery,
updateAssessmentByIdQuery,
} from "../utils/assessment.utils";
import { createNewQuestionQuery, RequestWithFile } from "../utils/question.utils";
import { createNewTopicQuery } from "../utils/topic.utils";
import { createNewSubtopicQuery } from "../utils/subtopic.utils";

export async function getAllAssessments(
req: Request,
Expand Down Expand Up @@ -74,21 +77,29 @@ export async function getAssessmentById(
}

export async function createAssessment(
req: Request,
req: RequestWithFile,
res: Response
): Promise<any> {
try {
const newAssessment: {
projectId: number;
} = req.body;

if (!newAssessment.projectId) {
return res.status(400).json(
STATUS_CODE[400]({
message: "projectId is required",
})
);
}
title: string;
subTopics: {
id: number;
title: string;
questions: {
id: number;
question: string;
hint: string;
priorityLevel: string;
answerType: string;
inputType: string;
isRequired: boolean;
evidenceFileRequired: boolean;
evidenceFile: string;
}[];
}[];
}[] = req.body;

if (MOCKDATA_ON === true) {
const createdAssessment = createMockAssessment(newAssessment);
Expand All @@ -99,13 +110,61 @@ export async function createAssessment(

return res.status(503).json(STATUS_CODE[503]({}));
} else {
const createdAssessment = await createNewAssessmentQuery(newAssessment);
let flag = true;
mainLoop: for (const topicGroup of newAssessment) {
if (!topicGroup.projectId) {
flag = false;
break mainLoop;
}
const assessment = await createNewAssessmentQuery({
projectId: topicGroup.projectId,
});
const assessmentId = assessment.id;
const newTopic = await createNewTopicQuery({
assessmentId,
title: topicGroup.title,
});
if (!newTopic) {
flag = false;
break mainLoop;
}
const newTopicId = newTopic.id;
for (const topic of topicGroup.subTopics) {
const newSubTopic = await createNewSubtopicQuery({
topicId: newTopicId,
name: topic.title,
});
if (!newSubTopic) {
flag = false;
break mainLoop;
}
const newSubTopicId = newSubTopic.topicId;
for (const question of topic.questions) {
const newQuestion = await createNewQuestionQuery(
{
subtopicId: newSubTopicId,
questionText: question.question,
answerType: question.answerType,
evidenceFileRequired: question.evidenceFileRequired,
hint: question.hint,
isRequired: question.isRequired,
priorityLevel: question.priorityLevel,
},
req.files!
);
if (!newQuestion) {
flag = false;
break mainLoop;
}
}
}
}

if (createdAssessment) {
return res.status(201).json(STATUS_CODE[201](createdAssessment));
if (flag) {
return res.status(201).json(STATUS_CODE[201]({}));
}

return res.status(503).json(STATUS_CODE[503]({}));
return res.status(204).json(STATUS_CODE[204]({}));
Comment on lines +163 to +167
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Return an appropriate error status code instead of 204 when creation fails

Returning a 204 No Content status when the assessment creation fails may be misleading. A 400 (Bad Request) or 500 (Internal Server Error) status code would better indicate that the operation did not succeed.

Apply this diff to return a more suitable error status code:

-if (flag) {
-  return res.status(201).json(STATUS_CODE[201]({}));
-}
-
-return res.status(204).json(STATUS_CODE[204]({}));
+if (flag) {
+  return res.status(201).json(STATUS_CODE[201]({}));
+} else {
+  return res.status(400).json(STATUS_CODE[400]({ message: "Assessment creation failed" }));
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (flag) {
return res.status(201).json(STATUS_CODE[201]({}));
}
return res.status(503).json(STATUS_CODE[503]({}));
return res.status(204).json(STATUS_CODE[204]({}));
if (flag) {
return res.status(201).json(STATUS_CODE[201]({}));
} else {
return res.status(400).json(STATUS_CODE[400]({ message: "Assessment creation failed" }));
}

}
} catch (error) {
return res.status(500).json(STATUS_CODE[500]((error as Error).message));
Expand Down
4 changes: 3 additions & 1 deletion Servers/routes/assessment.route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import express from "express";
const router = express.Router();
const multer = require("multer");
const upload = multer({ Storage: multer.memoryStorage() });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the option name 'Storage' to 'storage' in multer configuration

The configuration option for multer should be storage (lowercase 's'), not Storage. Using the incorrect option name will prevent multer from working properly.

Apply this diff to fix the typo:

-const upload = multer({ Storage: multer.memoryStorage() });
+const upload = multer({ storage: multer.memoryStorage() });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const upload = multer({ Storage: multer.memoryStorage() });
const upload = multer({ storage: multer.memoryStorage() });


import {
createAssessment,
Expand All @@ -16,7 +18,7 @@ router.get("/", /*authenticateJWT, */ getAllAssessments);
router.get("/:id", /*authenticateJWT, */ getAssessmentById);

// POST, PUT, DELETE requests
router.post("/", /*authenticateJWT, */ createAssessment);
router.post("/", /*authenticateJWT, */ upload.any("files"), createAssessment);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Adjust multer middleware usage: upload.any() does not accept field names

The upload.any() method from multer does not accept any arguments. It accepts files from any field name. If you intend to accept files from a specific field, consider using upload.array('files') or upload.single('file') as appropriate.

Apply this diff to use the correct multer middleware method:

-router.post("/", /*authenticateJWT, */ upload.any("files"), createAssessment);
+router.post("/", /*authenticateJWT, */ upload.array("files"), createAssessment);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
router.post("/", /*authenticateJWT, */ upload.any("files"), createAssessment);
router.post("/", /*authenticateJWT, */ upload.array("files"), createAssessment);

router.put("/:id", /*authenticateJWT, */ updateAssessmentById);
router.delete("/:id", /*authenticateJWT, */ deleteAssessmentById);

Expand Down
4 changes: 1 addition & 3 deletions Servers/routes/question.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import express from "express";
const router = express.Router();
const multer = require("multer");
const upload = multer({ Storage: multer.memoryStorage() });

import {
createQuestion,
Expand All @@ -18,7 +16,7 @@ router.get("/", /*authenticateJWT,*/ getAllQuestions);
router.get("/:id", /*authenticateJWT,*/ getQuestionById);

// POST, PUT, DELETE requests
router.post("/", /*authenticateJWT,*/ upload.any("files"), createQuestion);
router.post("/", /*authenticateJWT,*/ createQuestion);
router.put("/:id", /*authenticateJWT,*/ updateQuestionById);
router.delete("/:id", /*authenticateJWT,*/ deleteQuestionById);

Expand Down