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

Task #356/report an issue section for customers #357

Merged
merged 10 commits into from
Apr 9, 2024
Merged
4 changes: 2 additions & 2 deletions src/backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const mongoose = require('mongoose');
require('dotenv').config();
const routes = require('./routes');
const passport = require('./config/passport');

const cache = require('express-cache-headers');
const cors = require('cors');
const app = express();

Expand All @@ -22,7 +22,7 @@ const app = express();
// }

// Initializing middlewares

app.use(cache(30));
app.use(session({
secret:process.env.SESSION_SECRET,
resave: false,
Expand Down
266 changes: 158 additions & 108 deletions src/backend/controllers/issueController.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,177 @@
const Issue = require( '../models/issueModel');
const Issue = require("../models/issueModel");
const { authenticate } = require("../config/passport");

exports.issue_list = [
async (req,res)=>{
try{
const issues = await Issue.find({}).populate({
path: 'replies',
populate: {
path: 'sender',
model: 'User'
}
}).populate('sender').exec();
res.status(200).json(issues||[]);
}catch(error){
res.status(500).json({ error: 'Internal Server Error' });
}
authenticate,
async (req, res) => {
try {
const issues = await Issue.find({})
.populate({
path: "replies",
populate: {
path: "sender",
model: "User",
},
})
.populate("sender")
.exec();
res.status(200).json(issues || []);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
},
];

exports.issue_detail = async (req,res)=>{
try{
const id = req.params.issueId;
const issue = await Issue.findById(id).populate({
path: 'replies',
populate: {
path: 'sender',
model: 'User'
}
}).populate('sender').exec();
if(issue !== null)
return res.status(200).json(issue);
else
return res.status(404).json({error:"Not found"});

}catch(error){
res.status(500).json({ error: 'Internal Server Error' });
exports.issue_detail = [
authenticate,
async (req, res) => {
try {
const id = req.params.issueId;
const issue = await Issue.findById(id)
.populate({
path: "replies",
populate: {
path: "sender",
model: "User",
},
})
.populate("sender")
.exec();
if (issue !== null) return res.status(200).json(issue);
else return res.status(404).json({ error: "Not found" });
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
}

exports.issue_list_user = async (req,res)=>{
try{
const id = req.params.userId;
const userIssues = await Issue.find({sender:id}).populate({
path: 'replies',
populate: {
path: 'sender',
model: 'User'
}
}).populate('sender').exec();
return res.status(200).json(userIssues||[]);

},
];

}catch(error){
res.status(500).json({ error: 'Internal Server Error' });
exports.issue_list_user = [
authenticate,
async (req, res) => {
try {
const id = req.params.userId;
const userIssues = await Issue.find({ sender: id })
.populate({
path: "replies",
populate: {
path: "sender",
model: "User",
},
})
.populate("sender")
.exec();
return res.status(200).json(userIssues || []);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
}
},
];

exports.issue_create = [
async (req,res)=>{
try{
const {
sender,subject,description
} = req.body;
if(!sender||!subject||!description){
return res.status(400).json({error:"Some of the required fields are empty"});
}
const issue= new Issue({
seen:false,
sender:sender,
description:description,
status:"open",
subject:subject,
replies:[]
});
issue.save();
res.status(200).json(issue);

}catch(error){
res.status(500).json({ error: 'Internal Server Error' });
}
authenticate,
async (req, res) => {
try {
const { sender, subject, description } = req.body;
if (!sender || !subject || !description) {
return res
.status(400)
.json({ error: "Some of the required fields are empty" });
}
const issue = new Issue({
seen: false,
sender: sender,
description: description,
status: "open",
subject: subject,
replies: [],
});
issue.save();
res.status(200).json(issue);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
]
},
];

exports.issue_delete = [
authenticate,
async (req, res) => {
try{
const issueId = req.params.issueId;
const deletedIssue = await Issue.findByIdAndDelete(issueId);

if(!deletedIssue)
res.status(404).json({error:"Issue doesn't exist"});
else
return res.status(200).json({message:("successfully deleted issue " +issueId)})
} catch(error){
res.status(500).json({ error: 'Internal Server Error' });
authenticate,
async (req, res) => {
try {
const issueId = req.params.issueId;
const deletedIssue = await Issue.findByIdAndDelete(issueId);

}
if (!deletedIssue) res.status(404).json({ error: "Issue doesn't exist" });
else
return res
.status(200)
.json({ message: "successfully deleted issue " + issueId });
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
];
},
];

exports.issue_reply = [
authenticate,
async (req, res) => {
try {
const issueId = req.params.issueId;
const { sender, body } = req.body;

if (!sender || !body)
return res.status(400).json({ error: "Some fields are not specified" });
let issue = await Issue.findById(issueId);


exports.issue_reply = async (req, res) => {
try {
const issueId = req.params.issueId;
const { sender, body } = req.body;

if (!sender || !body)
return res.status(400).json({ error: "Some fields are not specified" });

const issue = await Issue.findById(issueId);

if (!issue)
return res.status(404).json({ error: "Issue not found" });

issue.replies.push({ sender, body });
await issue.save(); // Save the updated issue

return res.status(200).json(issue);
} catch (error) {
console.error('Error adding reply:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
};

if (!issue) return res.status(404).json({ error: "Issue not found" });
issue.seen = false;
issue.replies.push({ sender, body });
await issue.save(); // Save the updated issue
issue = await Issue.findById(issueId)
.populate({
path: "replies",
populate: {
path: "sender",
model: "User",
},
})
.populate("sender")
.exec();

return res.status(200).json(issue);
} catch (error) {
console.error("Error adding reply:", error);
return res.status(500).json({ error: "Internal Server Error" });
}
},
];

exports.issue_seen = [
authenticate,
async (req, res) => {
try {
const {userId}=req.body;
const issueId = req.params.issueId;
const issue = await Issue.findById(issueId)
.populate({
path: "replies",
populate: {
path: "sender",
model: "User",
},
})
.populate("sender")
.exec();
if (!issue) return res.status(404).json({ error: "Issue not found" });
issue.replies.forEach((reply) => {
if(reply.sender._id!==userId) reply.seen = true;
});
issue.seen=true;
await issue.save();
res.status(200).json(issue);
} catch (error) {
console.error("Error marking replies as seen:", error);
return res.status(500).json({ error: "Internal Server Error" });
}
},
];
6 changes: 6 additions & 0 deletions src/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dotenv": "^16.4.5",
"express": "~4.16.1",
"express-async-handler": "^1.2.0",
"express-cache-headers": "^0.1.4",
"express-validator": "^7.0.1",
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/backend/routes/issueRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ router.get('/user/:userId',issue_controller.issue_list_user);
router.post('/',issue_controller.issue_create);
router.delete('/:issueId',issue_controller.issue_delete);
router.put('/reply/:issueId',issue_controller.issue_reply);
router.put('/seen/:issueId',issue_controller.issue_seen);
module.exports = router;
Loading
Loading