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 #349/implement issue schema and backend endpoints #350

Merged
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 src/backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ app.use('/api/reservations',routes.reservationsRoute);
app.use('/api/branches',routes.branchRoute);
app.use('/api/transactions',routes.transactionsRoute);
app.use('/api/reviews',routes.reviewsRoute);
app.use('/api/issues',routes.issueRoute);



Expand Down
109 changes: 109 additions & 0 deletions src/backend/controllers/issueController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const Issue = require( '../models/issueModel');
const { authenticate } = require("../config/passport");

exports.issue_list = [
async (req,res)=>{
try{
const issues =await Issue.find({}).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).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}).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' });
}
}
]

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' });

}
}
];



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({ message: "Reply successfully added" });
} catch (error) {
console.error('Error adding reply:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
};

2 changes: 1 addition & 1 deletion src/backend/controllers/reservationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports.create_reservation = asyncHandler(async (req, res) => {
pickupDate:pickupDate.substring(0,10),
returnDate:returnDate.substring(0,10),
});

if (!emailSent)
return res
.status(500)
Expand Down
54 changes: 54 additions & 0 deletions src/backend/models/issueModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const issueSchema = new Schema({
sender: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User', // Reference to the sender (user)
required: true
},
subject: {
type: String,
required: true
},
description: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: ['open', 'closed'], // Status of the issue
default: 'open'
},
seen: {
type: Boolean,
default: false
},
replies: [{
sender: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User', // Reference to the admin who replied
required: true
},
body: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
},
seen: {
type: Boolean,
default: false
},
}]
});

module.exports = mongoose.model('Issue', issueSchema);


4 changes: 3 additions & 1 deletion src/backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const authRoute = require('./authRoute');
const branchRoute = require('./branchRoute');
const transactionsRoute = require('./transactionsRoute');
const reviewsRoute =require('./reviewRoute');
const issueRoute = require('./issueRoute');


module.exports = {
Expand All @@ -14,5 +15,6 @@ module.exports = {
reservationsRoute,
branchRoute,
transactionsRoute,
reviewsRoute
reviewsRoute,
issueRoute
};
12 changes: 12 additions & 0 deletions src/backend/routes/issueRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const router = express.Router();

const issue_controller = require('../controllers/issueController');

router.get('/',issue_controller.issue_list);
router.get('/:issueId',issue_controller.issue_detail);
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);
module.exports = router;
Loading