Untitled

 avatar
unknown
plain_text
a year ago
3.8 kB
3
Indexable
const express = require("express");
const Requests = require("../mongoose/models/requests.js");
//setting up the request router
const requestRouter = express.Router();

/**
 * url -> /requests
 * method -> POST
 * purpose -> create a new document inside requests collection
 * response -> 
 *          If success -> 
 *              status code -> 201
 *              body -> { 
                            "type": "Created", 
                            "message": "Request registered successfully with request id {_id}" 
                        }
                The {_id} should be filled with the _id generated for the document
 *          If there is any active requests(i.e) with the status of new or in-progress for the user (use the email that comes with the request body to find if there are any active requests for the user) ->
                status code -> 403
                body -> { 
                            "type": "Forbidden", 
                            "message": "You cannot raise multiple requests at a time" 
                        }
**/

requestRouter.post("/requests", async (req, res) => {
  const { name, email, type, issue_description } = req.body;
  const existingData = await Requests.find({ email }).lean();

  console.log(existingData);
  if (name && email && type && issue_description) {
    if (
      existingData.status === "new" ||
      existingData.status === "in-progress"
    ) {
      return res.status(403).send({
        type: "Forbidden",
        message: "You cannot raise multiple requests at a time",
      });
    }

    if (
      existingData.status !== "new" &&
      existingData.status !== "in-progress"
    ) {
      const newData = new Requests({ name, email, type, issue_description });

      await newData.save();
      return res.status(201).send({
        type: "Created",
        message: `Request registered successfully with request id ${newData.id}`,
      });
    }
  }
});

/**
 * url -> /requests
 * method -> GET
 * purpose -> fetching data from the requests collection
 * response ->
 *          If success ->
 *              status code -> 200
 *              body -> fetched data (refer to problem statement for sample data)
 **/

requestRouter.get("/requests", async (req, res) => {
  const data = await Requests.find({});

  return res.status(200).send({ type: "Ok", requests: data });
});

/**
 * url -> /requests/:id
 * method -> PATCH
 * purpose -> This should update the document in the requests collections that has _id equal to the id that comes with the request
 * response -> 
 *          If the status of the request is new or in-progress, then the status and the comment of the document should be updated -> 
 *              status code -> 200
 *              body -> { 
                            "type": "Created",
                            "message": "Request status updates successfully"
                        }
 *          If the status of the request is resolved or rejected -> 
                status code -> 403
                body -> { 
                            "type": "Forbidden",
                            "message": "Cannot perform this operation on this request"
                        }
**/
requestRouter.patch("/reuests/:id", async (req, res) => {
  const data = req.body;

  if (
    req.body.status === "resolved" ||
    req.body.status === "rejected" ||
    req.body.status === "in-progress"
  ) {
    await Requests.findByIdAndUpdate(req.params.id, { data });
    return res.status(200).send({
      type: "Ok",
      message: "Request status updates successfully",
    });
  }

  if (req.body.status === "new") {
    return res.status(403).send({
      type: "Forbidden",
      message: "Cannot perform this operation on this request",
    });
  }
});

//exporting the router
module.exports = requestRouter;
Editor is loading...
Leave a Comment