From e3ccd70a19a6e4152205d85bf69f5c9a218481e2 Mon Sep 17 00:00:00 2001 From: swostikpati Date: Thu, 7 Dec 2023 12:21:26 -0500 Subject: [PATCH 1/4] fixed all lint errors in backend and added backend lint to cd --- back-end/.eslintrc.cjs | 2 + .../adminIssueViewDetailsHandler.js | 15 ++-- back-end/src/controllers/adminPostHandler.js | 23 +++-- .../src/controllers/createIssueHandler.js | 87 +++++++++---------- .../controllers/studentIssueUpdateHandler.js | 39 +++++---- .../src/controllers/studentIssuesHandler.js | 11 ++- back-end/src/middlewares/checkJWT.js | 8 +- .../updatePriorityForOpenIssues.js | 3 +- back-end/src/middlewares/updateResolved.js | 5 +- back-end/test/adminPostDetails.test.js | 77 ++++++++++------ back-end/test/createIssue.test.js | 25 +++--- back-end/test/studentIssueUpdate.test.js | 1 + package.json | 2 +- 13 files changed, 169 insertions(+), 129 deletions(-) diff --git a/back-end/.eslintrc.cjs b/back-end/.eslintrc.cjs index 3bf6f66..53da970 100644 --- a/back-end/.eslintrc.cjs +++ b/back-end/.eslintrc.cjs @@ -46,6 +46,8 @@ module.exports = { "no-console": ["off"], "space-before-function-paren": ["off"], + "quote-props": ["off"], + "no-prototype-builtins": ["off"], quotes: ["off"], indent: "off" } diff --git a/back-end/src/controllers/adminIssueViewDetailsHandler.js b/back-end/src/controllers/adminIssueViewDetailsHandler.js index d6bf9c7..84b4247 100644 --- a/back-end/src/controllers/adminIssueViewDetailsHandler.js +++ b/back-end/src/controllers/adminIssueViewDetailsHandler.js @@ -1,4 +1,4 @@ -import Issue from '../../models/issueModel.js'; +import Issue from "../../models/issueModel.js"; export async function adminIssueViewDetailsHandler(req, res) { const { paramName } = req.params; // Get the issue index from request params @@ -15,10 +15,15 @@ export async function adminIssueViewDetailsHandler(req, res) { try { // Query the database to find issues that match both department and index - const response = await Issue.find({ departments:department, index: paramName }); - // Check if no matching issues are found - if (!response || response.length === 0) { - return res.status(500).send("No issues found for the given department and index."); + const response = await Issue.find({ + departments: department, + index: paramName + }); + // Check if no matching issues are found + if (!response || response.length === 0) { + return res + .status(500) + .send("No issues found for the given department and index."); } res.json(response); // Send only the data that matches the specific issue index } catch (error) { diff --git a/back-end/src/controllers/adminPostHandler.js b/back-end/src/controllers/adminPostHandler.js index af221df..86f4884 100644 --- a/back-end/src/controllers/adminPostHandler.js +++ b/back-end/src/controllers/adminPostHandler.js @@ -1,4 +1,4 @@ -import Issue from '../../models/issueModel.js'; +import Issue from "../../models/issueModel.js"; export async function adminPostHandler(req, res) { const { paramName } = req.params; @@ -9,7 +9,10 @@ export async function adminPostHandler(req, res) { const departmentTags = req.body.issueDepartmentTags; const isProposed = req.body.isProposed; try { - const specificIssue = await Issue.findOne({ departments: department, index: paramName }); + const specificIssue = await Issue.findOne({ + departments: department, + index: paramName + }); if (newcomment !== undefined) { specificIssue.comments.unshift(newcomment); } @@ -23,13 +26,13 @@ export async function adminPostHandler(req, res) { specificIssue.departments = departmentTags; } if (req.files !== undefined) { - const newfilesattachments = req.files.map(file => file.filename); + const newfilesattachments = req.files.map((file) => file.filename); if (specificIssue.attachments[0] == null) { specificIssue.attachments = newfilesattachments; } else { - newfilesattachments.forEach(element => { + newfilesattachments.forEach((element) => { specificIssue.attachments.push(element); - }); + }); } } if (isProposed !== undefined) { @@ -37,18 +40,20 @@ export async function adminPostHandler(req, res) { } if (specificIssue.isProposed === true) { const currentDate = new Date(); - const day = String(currentDate.getDate()).padStart(2, '0'); - const month = String(currentDate.getMonth() + 1).padStart(2, '0'); + const day = String(currentDate.getDate()).padStart(2, "0"); + const month = String(currentDate.getMonth() + 1).padStart(2, "0"); const year = currentDate.getFullYear(); const formattedDate = `${day}/${month}/${year}`; const isProposedDate = formattedDate; specificIssue.isProposedDate = isProposedDate; } - + + /* eslint-disable no-unused-vars */ const updatedIssue = await specificIssue.save(); + /* eslint-enable no-unused-vars */ res.status(200).send("Success"); } catch (error) { - console.error('Error updating data:', error.message); + console.error("Error updating data:", error.message); res.status(500).send("An error occurred while updating the data."); } } diff --git a/back-end/src/controllers/createIssueHandler.js b/back-end/src/controllers/createIssueHandler.js index 53ac030..87c199b 100644 --- a/back-end/src/controllers/createIssueHandler.js +++ b/back-end/src/controllers/createIssueHandler.js @@ -1,50 +1,49 @@ -import Issue from '../../models/issueModel.js'; +/* eslint-disable no-unused-vars */ +import Issue from "../../models/issueModel.js"; export async function createIssueHandler(req, res) { - const { - dateCreated, - currentStatus, - currentPriority - } = req.body; + const { dateCreated, currentStatus, currentPriority } = req.body; - const currentDate = new Date(); - const day = String(currentDate.getDate()).padStart(2, '0'); - const month = String(currentDate.getMonth() + 1).padStart(2, '0'); - const year = currentDate.getFullYear(); - const formattedDate = `${day}/${month}/${year}`; - const issueDateCreated = dateCreated || formattedDate; - const issueTimeCreated = new Date().toLocaleTimeString('en-US', { - hour: '2-digit', - minute: '2-digit', - hour12: false, - timeZone: 'Asia/Dubai' - }); - const attachments = req.files.map(file => file.filename); - const lastIssue = await Issue.findOne().sort({ index: -1 }); - const newIndex = lastIssue ? lastIssue.index + 1 : 1; + const currentDate = new Date(); + const day = String(currentDate.getDate()).padStart(2, "0"); + const month = String(currentDate.getMonth() + 1).padStart(2, "0"); + const year = currentDate.getFullYear(); + const formattedDate = `${day}/${month}/${year}`; + const issueDateCreated = dateCreated || formattedDate; + const issueTimeCreated = new Date().toLocaleTimeString("en-US", { + hour: "2-digit", + minute: "2-digit", + hour12: false, + timeZone: "Asia/Dubai" + }); + const attachments = req.files.map((file) => file.filename); + const lastIssue = await Issue.findOne().sort({ index: -1 }); + const newIndex = lastIssue ? lastIssue.index + 1 : 1; - const newIssue = new Issue ({ - index: newIndex, - studentNetID: req.params.studentNetID, - studentName: req.body.studentName, - title: req.body.issueTitle, - description: req.body.issueDesc, - attachments: attachments, - departments: req.body.deptTagged.includes(',') ? req.body.deptTagged.split(',') : [req.body.deptTagged], - comments: [], - dateCreated: issueDateCreated, - timeCreated: issueTimeCreated, - currentStatus:'Open', - currentPriority: 'New', - isProposed: false, - isProposedDate: '', - }); + const newIssue = new Issue({ + index: newIndex, + studentNetID: req.params.studentNetID, + studentName: req.body.studentName, + title: req.body.issueTitle, + description: req.body.issueDesc, + attachments, + departments: req.body.deptTagged.includes(",") + ? req.body.deptTagged.split(",") + : [req.body.deptTagged], + comments: [], + dateCreated: issueDateCreated, + timeCreated: issueTimeCreated, + currentStatus: "Open", + currentPriority: "New", + isProposed: false, + isProposedDate: "" + }); - try { - await newIssue.save(); - res.status(200).send('Issue created successfully'); - } catch (error) { - console.error('Error creating issue:', error.message); - res.status(500).send("An error occurred while saving the data."); - } + try { + await newIssue.save(); + res.status(200).send("Issue created successfully"); + } catch (error) { + console.error("Error creating issue:", error.message); + res.status(500).send("An error occurred while saving the data."); + } } diff --git a/back-end/src/controllers/studentIssueUpdateHandler.js b/back-end/src/controllers/studentIssueUpdateHandler.js index 235ef35..866ffe0 100644 --- a/back-end/src/controllers/studentIssueUpdateHandler.js +++ b/back-end/src/controllers/studentIssueUpdateHandler.js @@ -1,20 +1,23 @@ -import Issue from '../../models/issueModel.js'; +import Issue from "../../models/issueModel.js"; // The function updates the issue related to this student export async function studentIssueUpdateHandler(req, res) { - const { paramName } = req.params; // Get the issue index from request params - const { studentNetID } = req.params; // Get the studentNetID from request params - const newcomment = req.body.comments; - const currentStatus = req.body.currentStatus; - const currentPriority = req.body.currentPriority; - const isProposed = req.body.isProposed; + const { paramName } = req.params; // Get the issue index from request params + const { studentNetID } = req.params; // Get the studentNetID from request params + const newcomment = req.body.comments; + const currentStatus = req.body.currentStatus; + const currentPriority = req.body.currentPriority; + const isProposed = req.body.isProposed; try { - const specificIssue = await Issue.findOne({ studentNetID: studentNetID, index: paramName }); + const specificIssue = await Issue.findOne({ + studentNetID, + index: paramName + }); if (newcomment !== undefined) { - specificIssue.comments.unshift(newcomment); + specificIssue.comments.unshift(newcomment); } if (currentStatus !== undefined) { - specificIssue.currentStatus = currentStatus; + specificIssue.currentStatus = currentStatus; } if (currentPriority !== undefined) { specificIssue.currentPriority = currentPriority; @@ -22,27 +25,27 @@ export async function studentIssueUpdateHandler(req, res) { if (isProposed !== undefined) { specificIssue.isProposed = isProposed; } - if (specificIssue.currentStatus === 'Resolved') { + if (specificIssue.currentStatus === "Resolved") { specificIssue.currentPriority = ""; } if (req.files !== undefined) { - const newfilesattachments = req.files.map(file => file.filename); + const newfilesattachments = req.files.map((file) => file.filename); if (specificIssue.attachments[0] == null) { specificIssue.attachments = newfilesattachments; } else { - newfilesattachments.forEach(element => { + newfilesattachments.forEach((element) => { specificIssue.attachments.push(element); - }); + }); } } const updatedIssue = await specificIssue.save(); // Send a response back to the client indicating success - res.json({ message: 'Issue updated successfully', updatedIssue }); + res.json({ message: "Issue updated successfully", updatedIssue }); } catch (error) { // Log the error and send an appropriate response - console.error('Error updating data:', error.message); - res.status(500).send('An error occurred while updating the data.'); + console.error("Error updating data:", error.message); + res.status(500).send("An error occurred while updating the data."); } -} \ No newline at end of file +} diff --git a/back-end/src/controllers/studentIssuesHandler.js b/back-end/src/controllers/studentIssuesHandler.js index a5bf16e..9059e0a 100644 --- a/back-end/src/controllers/studentIssuesHandler.js +++ b/back-end/src/controllers/studentIssuesHandler.js @@ -1,4 +1,5 @@ -import IssueModel from '../../models/issueModel.js'; +/* eslint-disable brace-style */ +import IssueModel from "../../models/issueModel.js"; import User from "../../models/UserModel.js"; export async function issueRetrievalHandler(req, res) { @@ -10,11 +11,13 @@ export async function issueRetrievalHandler(req, res) { netId: paramName }); // If user does not exist, return error - if (!user) { res.status(500).send("User does not exist.");} + if (!user) { + res.status(500).send("User does not exist."); + } // If user exists, return all issues for that user else { - const issues = await IssueModel.find({ "studentNetID": paramName }); - res.json(issues); + const issues = await IssueModel.find({ studentNetID: paramName }); + res.json(issues); } } catch (error) { console.error("Error retrieving data:", error.message); diff --git a/back-end/src/middlewares/checkJWT.js b/back-end/src/middlewares/checkJWT.js index c3c919f..57d16d9 100644 --- a/back-end/src/middlewares/checkJWT.js +++ b/back-end/src/middlewares/checkJWT.js @@ -5,11 +5,13 @@ export default function checkJWT(req, res, next) { if (err) { return res.status(500).json({ message: "Internal server error" }); } - + if (!user) { if (req.path !== "/") { // Send a 401 Unauthorized response with a message - return res.status(401).json({ authenticated: false, message: "User not authenticated" }); + return res + .status(401) + .json({ authenticated: false, message: "User not authenticated" }); } else { return next(); } @@ -18,4 +20,4 @@ export default function checkJWT(req, res, next) { next(); } })(req, res, next); -} \ No newline at end of file +} diff --git a/back-end/src/middlewares/updatePriorityForOpenIssues.js b/back-end/src/middlewares/updatePriorityForOpenIssues.js index bbb536f..5eb8bb3 100644 --- a/back-end/src/middlewares/updatePriorityForOpenIssues.js +++ b/back-end/src/middlewares/updatePriorityForOpenIssues.js @@ -12,7 +12,7 @@ export default async function updatePriorityForOpenIssues() { const openIssuesToUpdate = await IssueModel.find({ currentStatus: "Open", comments: { $size: 0 }, - dateCreated: { $lt: thresholdDate.format("DD/MM/YYYY") }, + dateCreated: { $lt: thresholdDate.format("DD/MM/YYYY") } }); // Update the priority of each eligible issue to "High Priority" @@ -23,6 +23,5 @@ export default async function updatePriorityForOpenIssues() { } catch (error) { // Handle errors console.error("Error updating issue priorities:", error.message); - res.status(500).send("An error occurred while updating issue priorities."); } } diff --git a/back-end/src/middlewares/updateResolved.js b/back-end/src/middlewares/updateResolved.js index 85007aa..0bfacca 100644 --- a/back-end/src/middlewares/updateResolved.js +++ b/back-end/src/middlewares/updateResolved.js @@ -12,12 +12,12 @@ export default async function updateResolved() { const openIssuesToUpdate = await IssueModel.find({ currentStatus: "Action Required", isProposed: true, - isProposedDate: { $lt: thresholdDate.format("DD/MM/YYYY") }, + isProposedDate: { $lt: thresholdDate.format("DD/MM/YYYY") } }); // Update the resolve status for each eligible issue to "Resolved" for (const issue of openIssuesToUpdate) { - issue.comments.unshift("Issue automatically resolved by the system.") + issue.comments.unshift("Issue automatically resolved by the system."); issue.currentPriority = ""; issue.currentStatus = "Resolved"; issue.isProposed = false; @@ -27,6 +27,5 @@ export default async function updateResolved() { } catch (error) { // Handle errors console.error("Error updating issue resolved status:", error.message); - res.status(500).send("An error occurred while updating issue resolved status."); } } diff --git a/back-end/test/adminPostDetails.test.js b/back-end/test/adminPostDetails.test.js index e65e43d..5b5c49d 100644 --- a/back-end/test/adminPostDetails.test.js +++ b/back-end/test/adminPostDetails.test.js @@ -14,7 +14,7 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { let department = "admin"; let currentStatus; let currentPriority; - + describe("POST /api/actions/admin/:department/:paramName", () => { it("should update the issue's comment for a valid department and issue index", async () => { const newComment = "This is a new comment."; @@ -29,15 +29,21 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { assert.equal(res.text, "Success"); // Query the database to find the updated issue - const updatedIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const updatedIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); // Check that the comment is added to the issue assert.include(updatedIssue.comments, newComment); }); it("should update the issue's status for a valid department and issue index", async () => { - const currentIssue = await IssueModel.findOne({ departments: department, index: paramName }); - currentStatus = currentIssue.currentStatus; + const currentIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); + currentStatus = currentIssue.currentStatus; const newStatus = "In Progress"; const res = await chai .request(server) @@ -49,14 +55,20 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { assert.equal(res.text, "Success"); // Query the database to find the updated issue - const updatedIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const updatedIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); // Check that the status is updated assert.equal(updatedIssue.currentStatus, newStatus); }); it("should update the issue's priority for a valid department and issue index", async () => { - const currentIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const currentIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); currentPriority = currentIssue.currentPriority; const newPriority = "New"; // Change this to your desired priority value const res = await chai @@ -69,14 +81,20 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { assert.equal(res.text, "Success"); // Query the database to find the updated issue - const updatedIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const updatedIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); // Check that the priority is updated assert.equal(updatedIssue.currentPriority, newPriority); }); it("should update the issue's department tags for a valid department and issue index", async () => { - const currentIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const currentIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); const newDepartmentTags = ["admin", "IT"]; const res = await chai .request(server) @@ -88,7 +106,10 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { assert.equal(res.text, "Success"); // Query the database to find the updated issue - const updatedIssue = await IssueModel.findOne({ departments: department, index: paramName }); + const updatedIssue = await IssueModel.findOne({ + departments: department, + index: paramName + }); // Check that the department tags are updated assert.deepEqual(updatedIssue.departments, newDepartmentTags); @@ -108,25 +129,25 @@ describe("Integration Tests for Admin Post Handler Endpoint", () => { paramName = 101; // Reset paramName to its original value }); - after(async () => { - try { - // Find the issue in the database by its index - const issue = await IssueModel.findOne({ index: paramName }); - // Remove the last comment from the comments array - issue.comments.pop(); - // Revert to the previous status - issue.currentStatus = currentStatus; - // Revert to the previous priority - issue.currentPriority = currentPriority; - // Revert to the previous department tags - issue.departments = ["admin"]; - // Save the updated issue back to the database - await issue.save(); - } catch (error) { - // Handle errors - console.error("Error:", error.message); - } + after(async () => { + try { + // Find the issue in the database by its index + const issue = await IssueModel.findOne({ index: paramName }); + // Remove the last comment from the comments array + issue.comments.pop(); + // Revert to the previous status + issue.currentStatus = currentStatus; + // Revert to the previous priority + issue.currentPriority = currentPriority; + // Revert to the previous department tags + issue.departments = ["admin"]; + // Save the updated issue back to the database + await issue.save(); + } catch (error) { + // Handle errors + console.error("Error:", error.message); + } }); }); }); -/* eslint-enable */ \ No newline at end of file +/* eslint-enable */ diff --git a/back-end/test/createIssue.test.js b/back-end/test/createIssue.test.js index 1c98e2a..61716e8 100644 --- a/back-end/test/createIssue.test.js +++ b/back-end/test/createIssue.test.js @@ -18,7 +18,7 @@ describe("Integration Tests for Create Issue Endpoint", () => { studentName: "student", issueTitle: "Test Issue", issueDesc: "This is a test issue.", - deptTagged: "IT", + deptTagged: "IT" }; const res = await chai @@ -37,7 +37,7 @@ describe("Integration Tests for Create Issue Endpoint", () => { const createdIssue = await IssueModel.findOne({ studentNetID: studentNetID, title: issueData.issueTitle, - departments: issueData.deptTagged, + departments: issueData.deptTagged }); // Extract the created issue's index from the database @@ -48,15 +48,15 @@ describe("Integration Tests for Create Issue Endpoint", () => { // Cleanup step to delete the created issue after testing after(async () => { - if (createdIssueIndex) { - // Find and delete the issue by its index - try { - await IssueModel.deleteOne({ index: createdIssueIndex }); - } catch (error) { - console.error(`Error deleting issue: ${error}`); - } + if (createdIssueIndex) { + // Find and delete the issue by its index + try { + await IssueModel.deleteOne({ index: createdIssueIndex }); + } catch (error) { + console.error(`Error deleting issue: ${error}`); } - }); + } + }); it("should handle errors gracefully for missing fields", async () => { const studentNetID = "student"; @@ -64,7 +64,7 @@ describe("Integration Tests for Create Issue Endpoint", () => { studentName: "student", issueTitle: "Test Issue", issueDesc: "", - deptTagged: "", + deptTagged: "" }; // Send the request with missing fields @@ -81,4 +81,5 @@ describe("Integration Tests for Create Issue Endpoint", () => { }); }); }); -/* eslint-enable */ \ No newline at end of file + +/* eslint-enable */ diff --git a/back-end/test/studentIssueUpdate.test.js b/back-end/test/studentIssueUpdate.test.js index 66d9a7c..b593677 100644 --- a/back-end/test/studentIssueUpdate.test.js +++ b/back-end/test/studentIssueUpdate.test.js @@ -1,3 +1,4 @@ +/* eslint-disable */ import chai, { assert } from "chai"; import chaiHttp from "chai-http"; import server from "../app.js"; diff --git a/package.json b/package.json index ac3a598..ae13ef6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "server": "cd back-end && npm run dev", "client": "cd front-end && npm start", "dev": "concurrently \"npm run server\" \"npm run client\"", - "lint": "cd front-end && npm run lint", + "lint": "cd front-end && npm run lint && cd ../back-end && npm run lint", "test": "cd back-end && npm test", "install-deps": "npm install && cd back-end && npm install && cd ../front-end && npm install" } From c7ef8a3d4292d26c2cc4a3bba102033efdac0fad Mon Sep 17 00:00:00 2001 From: swostikpati Date: Thu, 7 Dec 2023 12:35:03 -0500 Subject: [PATCH 2/4] fixed small lint error --- back-end/.eslintrc.cjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/back-end/.eslintrc.cjs b/back-end/.eslintrc.cjs index 53da970..baf3d82 100644 --- a/back-end/.eslintrc.cjs +++ b/back-end/.eslintrc.cjs @@ -39,7 +39,7 @@ module.exports = { "no-useless-call": ["error"], "no-use-before-define": ["warn"], - camelcase: ["warn", { properties: "never" }], + camelcase: ["off"], "func-call-spacing": ["off"], "no-lonely-if": ["off"], "array-bracket-spacing": ["warn"], @@ -49,6 +49,7 @@ module.exports = { "quote-props": ["off"], "no-prototype-builtins": ["off"], quotes: ["off"], - indent: "off" + indent: "off", + "object-shorthand": ["off"] } }; From 490062ca328574dd7c0545b93da8339335411be1 Mon Sep 17 00:00:00 2001 From: swostikpati Date: Thu, 7 Dec 2023 12:49:00 -0500 Subject: [PATCH 3/4] bug solving test --- back-end/.eslintignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 back-end/.eslintignore diff --git a/back-end/.eslintignore b/back-end/.eslintignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/back-end/.eslintignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file From 4f2284cd0b12c438bd7f12f08b91be059aa35733 Mon Sep 17 00:00:00 2001 From: swostikpati Date: Thu, 7 Dec 2023 13:37:29 -0500 Subject: [PATCH 4/4] attempt 3 --- back-end/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back-end/package.json b/back-end/package.json index f661811..df564b6 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -7,7 +7,7 @@ "test": "mocha test/**/*.test.js", "start": "node server.js", "dev": "nodemon server.js", - "lint": "eslint **/*.js", + "lint": "eslint src/**/*.js config/**/*.js models/**/*.js database/**/*.js", "coverage": "c8 mocha test/**/*.test.js" }, "keywords": [],