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

Sprint/4/spike/337/lint backend #361

Merged
merged 5 commits into from
Dec 8, 2023
Merged
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
1 change: 1 addition & 0 deletions back-end/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
7 changes: 5 additions & 2 deletions back-end/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ 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"],

"no-console": ["off"],
"space-before-function-paren": ["off"],
"quote-props": ["off"],
"no-prototype-builtins": ["off"],
quotes: ["off"],
indent: "off"
indent: "off",
"object-shorthand": ["off"]
}
};
2 changes: 1 addition & 1 deletion back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
15 changes: 10 additions & 5 deletions back-end/src/controllers/adminIssueViewDetailsHandler.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
23 changes: 14 additions & 9 deletions back-end/src/controllers/adminPostHandler.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand All @@ -23,32 +26,34 @@ 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) {
specificIssue.isProposed = isProposed;
}
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.");
}
}
87 changes: 43 additions & 44 deletions back-end/src/controllers/createIssueHandler.js
Original file line number Diff line number Diff line change
@@ -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.");
}
}
39 changes: 21 additions & 18 deletions back-end/src/controllers/studentIssueUpdateHandler.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
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;
}
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.");
}
}
}
11 changes: 7 additions & 4 deletions back-end/src/controllers/studentIssuesHandler.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions back-end/src/middlewares/checkJWT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -18,4 +20,4 @@ export default function checkJWT(req, res, next) {
next();
}
})(req, res, next);
}
}
3 changes: 1 addition & 2 deletions back-end/src/middlewares/updatePriorityForOpenIssues.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.");
}
}
5 changes: 2 additions & 3 deletions back-end/src/middlewares/updateResolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
}
}
Loading
Loading