noobling/anglicare-sprint-week

View on GitHub
app_server/out/controllers_requests.js.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: controllers/requests.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: controllers/requests.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>const mongoose = require('mongoose');

const Request = mongoose.model('Request');
const Service = mongoose.model('Service');

/**
 * Saves a youth person's request (mongo document) to the database.
 * Redirects to the bed vacancies page.
 * @param {Object} req Express request object.
 * @param {Object} res Express response object.
 */
module.exports.addRequest = (req, res) => {
  const request = new Request();

  request.firstName = req.body.fName;
  request.lastName = req.body.lName;
  request.gender = req.body.gender;
  request.age = req.body.age;
  request.hasChild = req.body.child === 'yes';
  request.isLongTerm = req.params.lengthOfStay === 'long_term';

  request.save((err, doc) => {
    if (err) {
      res.status(500).json({ message: err });
    } else {
      req.session.requestId = doc.id;
      req.session.coordinates = [req.body.long, req.body.lat];
      res.redirect('/locations/'.concat(req.params.lengthOfStay));
    }
  });
};

/**
 * Adds a youth person's phone number to their request (mongo document) and
 * adds that request to a service provider.
 * @param {Object} req Express request object.
 * @param {Object} res Express response object.
 */
module.exports.addPhoneToRequest = (req, res) => {
  if (req.session.requestId) {
    // Add phone number to request
    Request.findOneAndUpdate(
      { _id: req.session.requestId },
      {
        $set:
        {
          phoneNumber: req.body.number,
          email: req.body.email,
        },
      },
      { runValidators: true, new: true },
      (err) => {
        if (err) {
          console.log('[ERROR] RequestsController: '.concat(err));
          res.status(500).json({ message: 'Could not add phone number to request.' });
        }
      }
    );
    // Add request to service
    Service.findOneAndUpdate(
      { _id: req.body.serviceId },
      { $push: { requests: req.session.requestId } },
      { runValidators: true, new: true },
      (err) => {
        if (err) {
          console.log('[ERROR] RequestsController: '.concat(err));
          res.status(500).json({ message: 'Could not submit request to service provider.' });
        }
      }
    );
  } else {
    // TODO: Handle this case
    //    Tell the user to submit a new request?
    //    Use a 'token' system where the youth are provided a token to
    //    access the results of their request
    res.status(400).json({ message: 'Your session has expired. Please submit a new request.' });
  }
  res.status(201).end();
};
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#about">about</a></li><li><a href="global.html#addPhoneToRequest">addPhoneToRequest</a></li><li><a href="global.html#addRequest">addRequest</a></li><li><a href="global.html#addressSchema">addressSchema</a></li><li><a href="global.html#addService">addService</a></li><li><a href="global.html#ageSchema">ageSchema</a></li><li><a href="global.html#bedSchema">bedSchema</a></li><li><a href="global.html#dashboard">dashboard</a></li><li><a href="global.html#email">email</a></li><li><a href="global.html#findService">findService</a></li><li><a href="global.html#findServiceOfAvailability">findServiceOfAvailability</a></li><li><a href="global.html#gracefulShutdown">gracefulShutdown</a></li><li><a href="global.html#hoursSchema">hoursSchema</a></li><li><a href="global.html#index">index</a></li><li><a href="global.html#nexmo">nexmo</a></li><li><a href="global.html#notification">notification</a></li><li><a href="global.html#openCloseSchema">openCloseSchema</a></li><li><a href="global.html#profile">profile</a></li><li><a href="global.html#register">register</a></li><li><a href="global.html#requestSchema">requestSchema</a></li><li><a href="global.html#sendEmail">sendEmail</a></li><li><a href="global.html#sendSMS">sendSMS</a></li><li><a href="global.html#service">service</a></li><li><a href="global.html#serviceSchema">serviceSchema</a></li><li><a href="global.html#showLocation">showLocation</a></li><li><a href="global.html#showLocations">showLocations</a></li><li><a href="global.html#sms">sms</a></li><li><a href="global.html#tagSchema">tagSchema</a></li><li><a href="global.html#transporter">transporter</a></li><li><a href="global.html#userSchema">userSchema</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat Jan 27 2018 15:27:38 GMT+0800 (AWST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>