diff --git a/back-end/src/controllers/studentIssueUpdateHandler.js b/back-end/src/controllers/studentIssueUpdateHandler.js index 36f42ab..c03102b 100644 --- a/back-end/src/controllers/studentIssueUpdateHandler.js +++ b/back-end/src/controllers/studentIssueUpdateHandler.js @@ -1,76 +1,27 @@ -import { publicpath } from "../../app.js"; -import { promises as fs } from 'fs'; -import axios from 'axios'; +import Issue from '../../models/issueModel.js'; // The function updates the issue related to this student export async function studentIssueUpdateHandler(req, res) { - const { studentNetID, - paramName } = req.params; - const issueindex = req.body.issueindex; const newcomment = req.body.comments; const currentStatus = req.body.currentStatus; - const filePath = publicpath + "/json/mockapi.json"; - const fileContent = await fs.readFile(filePath, 'utf8'); - const jsonData = JSON.parse(fileContent); - - - // const specificIssue = jsonData.filter( - // (item) => String(item.index) === String(issueindex) - // ); - - // if (newcomment !== undefined) { - // specificIssue[0].comments.unshift(newcomment); - // } - // if (currentStatus !== undefined) { - // specificIssue[0].currentStatus = currentStatus; - // } - - // fs.writeFile(filePath, JSON.stringify(jsonData, null, '\t'), (err) => { - // if (err) { - // console.error(err); - // } else { - // console.log('File written successfully.'); - // } - // }); - - // const updateData = req.body; // This should contain the data you want to update try { - // Make a POST request to the backend API to update the student issue - // const response = await axios.post( - // `${process.env.BACKEND_URL}/api/actions/student/${studentNetID}`, - // updateData - // ); - - const response = await axios.post( - `${process.env.BACKEND_URL}/json/mockapi.json`, - updateData // will be replaced with db call - ); - const specificIssue = jsonData.filter( - (item) => String(item.index) === String(paramName) - ); + const specificIssue = await Issue.findOne({ index: issueindex }); if (newcomment !== undefined) { - specificIssue[0].comments.unshift(newcomment); + specificIssue.comments.unshift(newcomment); } if (currentStatus !== undefined) { - specificIssue[0].currentStatus = currentStatus; + specificIssue.currentStatus = currentStatus; } - fs.writeFile(filePath, JSON.stringify(jsonData, null, '\t'), (err) => { - if (err) { - console.error(err); - } else { - console.log('File written successfully.'); - } - }); - + const updatedIssue = await specificIssue.save(); // Send a response back to the client indicating success - res.json({ message: 'Issue updated successfully', updatedIssue: response.data }); + res.json({ message: 'Issue updated successfully', updatedIssue: updatedIssue }); } catch (error) { // Log the error and send an appropriate response console.error('Error updating data:', error.message); diff --git a/back-end/src/controllers/studentIssueViewDetailsHandler.js b/back-end/src/controllers/studentIssueViewDetailsHandler.js index 5c2e08d..b996bbf 100644 --- a/back-end/src/controllers/studentIssueViewDetailsHandler.js +++ b/back-end/src/controllers/studentIssueViewDetailsHandler.js @@ -1,4 +1,4 @@ -import axios from "axios"; +import Issue from '../../models/issueModel.js'; // The function retrieves all the issues related to this student export async function studentIssueViewDetailsHandler(req, res) { @@ -14,27 +14,20 @@ export async function studentIssueViewDetailsHandler(req, res) { } try { - // Assuming the data you want is at the response.data property - const response = await axios.get( - `${process.env.BACKEND_URL}/json/mockapi.json` // will be replaced with db call - ); + + const response = await Issue.find({ index: paramName }); // Check if any data is returned for the student - if (!response.data || response.data.length === 0) { + if (!response || response.length === 0) { return res.status(500).send("No issues found for the given studentNetID."); } - // Assuming response.data is an array of items and each item has a index - const filteredData = response.data.filter( - (item) => String(item.index) === String(paramName) - ); - // Check if the specific issue index exists - if (filteredData.length === 0) { + if (response.length === 0) { return res.status(500).send("Issue with the given index not found."); } - res.json(filteredData); // Send only the data that matches the specific issue index + res.json(response); // Send only the data that matches the specific issue index } catch (error) { // Log the error and send an appropriate response console.error("Error retrieving data:", error.message); diff --git a/front-end/src/components/student/StudentIssueOverlay/DesktopIssueDetails.js b/front-end/src/components/student/StudentIssueOverlay/DesktopIssueDetails.js index aa13fcd..2b095e4 100644 --- a/front-end/src/components/student/StudentIssueOverlay/DesktopIssueDetails.js +++ b/front-end/src/components/student/StudentIssueOverlay/DesktopIssueDetails.js @@ -114,15 +114,15 @@ const DesktopIssueDetails = ({ index }) => { // every time the `index` changes. }, [index, changeOccured]); - const reopenIssue = () => { + const reopenIssue = async () => { // setIssue({ ...issue, currentStatus: 'Open' }); - postReopen(); + await postReopen(); setChangeOccured(!changeOccured); setIssue({ ...issue, currentStatus: issue.currentStatus }); }; - const acceptResolution = () => { - postMarkAsResolved(); + const acceptResolution = async () => { + await postMarkAsResolved(); setChangeOccured(!changeOccured); setIssue({ ...issue, currentStatus: issue.currentStatus }); // Since the issue status is already 'Resolved', no need to change it.