added notes to the frontend
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const asyncHandler = require("express-async-handler");
|
||||
|
||||
const User = require("../models/userModel");
|
||||
const Note = require("../models/noteModel");
|
||||
const Ticket = require("../models/ticketModel");
|
||||
|
||||
// @desc Get notes for a ticket
|
||||
// @route GET /api/tickets/:ticketId/notes
|
||||
// @access Private
|
||||
const getNotes = asyncHandler(async (req, res) => {
|
||||
const user = await User.findById(req.user.id);
|
||||
|
||||
if (!user) {
|
||||
res.status(401);
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
const ticket = await Ticket.findById(req.params.ticketId)
|
||||
if (!ticket) {
|
||||
res.status(404);
|
||||
throw new Error("Ticket not found");
|
||||
}
|
||||
if (ticket.user.toString() !== req.user.id) {
|
||||
res.status(401);
|
||||
throw new Error("Not authorized");
|
||||
}
|
||||
|
||||
const notes = await Note.find({ ticket: req.params.ticketId });
|
||||
|
||||
res.status(201).json(notes);
|
||||
});
|
||||
// @desc Creata a note
|
||||
// @route POST /api/tickets/:ticketId/notes
|
||||
// @access Private
|
||||
const addNote = asyncHandler(async (req, res) => {
|
||||
const user = await User.findById(req.user.id);
|
||||
|
||||
if (!user) {
|
||||
res.status(401);
|
||||
throw new Error("User not found");
|
||||
}
|
||||
const ticket = await Ticket.findById(req.params.ticketId);
|
||||
if (!ticket) {
|
||||
res.status(404);
|
||||
throw new Error("Ticket not found");
|
||||
}
|
||||
if (ticket.user.toString() !== req.user.id) {
|
||||
res.status(401);
|
||||
throw new Error("Not authorized");
|
||||
}
|
||||
const note = await Note.create({
|
||||
text: req.body.text,
|
||||
ticket: req.params.ticketId,
|
||||
isStaff:false,
|
||||
user: req.user.id
|
||||
});
|
||||
res.status(201).json(note);
|
||||
});
|
||||
|
||||
module.exports = {addNote, getNotes };
|
||||
@@ -0,0 +1,32 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const noteSchema = mongoose.Schema(
|
||||
{
|
||||
user: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
required: true,
|
||||
ref: "User",
|
||||
},
|
||||
ticket: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
required: true,
|
||||
ref: "Ticket",
|
||||
},
|
||||
text: {
|
||||
type: String,
|
||||
required: [true, "Please add some text"],
|
||||
},
|
||||
isStaff: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
staffId: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = mongoose.model("Note", noteSchema);
|
||||
@@ -0,0 +1,8 @@
|
||||
const express = require("express");
|
||||
const router = express.Router({ mergeParams: true });
|
||||
const protect = require("../middleware/authMiddleware");
|
||||
const { getNotes, addNote } = require("../controllers/noteController");
|
||||
|
||||
router.route("/").get(protect, getNotes).post(protect, addNote);
|
||||
|
||||
module.exports = router;
|
||||
@@ -10,6 +10,8 @@ const {
|
||||
|
||||
const protect = require("../middleware/authMiddleware");
|
||||
|
||||
const noteRouter = require('./noteRoutes')
|
||||
router.use('/:ticketId/notes',noteRouter)
|
||||
|
||||
router.route("/").get(protect, getTickets).post(protect, createTicket);
|
||||
router
|
||||
|
||||
Reference in New Issue
Block a user