const { splitSite } = require("../middlewares");
const db = require("../models");
const MessageToken = db.message_token;
exports.createMessageToken = (req, res) => {
  const site = splitSite.findSiteNumber(req);
  // Create a MessageToken
  const messageToken = new MessageToken({
    message_token: req.body.message_token || '',
    site
  });

  // Save MessageToken in the database
  messageToken
    .save(messageToken)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Message Token."
      });
    });
};
// Retrieve all MessageTokens from the database.
exports.getAllMessageTokens = (req, res) => {
  var condition = {};
  MessageToken.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving messageTokens."
      });
    });
};

// Update a MessageTOken by the id in the request
exports.updateMessageToken = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  MessageToken.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update MessageToken with id=${id}. Maybe MessageToken was not found!`
        });
      } else res.send({ success: true, message: "MessageToken was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating MessageToken with id=" + id
      });
    });
};

