diff --git a/src/controllers/ownerMessageController.js b/src/controllers/ownerMessageController.js index b4a00431b..f09a32030 100644 --- a/src/controllers/ownerMessageController.js +++ b/src/controllers/ownerMessageController.js @@ -1,63 +1,61 @@ const ownerMessageController = function (OwnerMessage) { - const postOwnerMessage = function (req, res) { - if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to create messages!'); + const getOwnerMessage = async function (req, res) { + try { + const results = await OwnerMessage.find({}); + if (results.length === 0) { // first time initialization + const ownerMessage = new OwnerMessage(); + await ownerMessage.save(); + res.status(200).send({ ownerMessage }); + } else { + res.status(200).send({ ownerMessage: results[0] }); + } + } catch (error) { + res.status(404).send(error); } - const ownerMessage = new OwnerMessage(); - ownerMessage.message = req.body.newMessage; - ownerMessage.save().then(() => res.status(201).json({ - _serverMessage: 'Message succesfuly created!', - ownerMessage, - })).catch(err => res.status(500).send({ err })); - }; - - const getOwnerMessage = function (req, res) { - OwnerMessage.find() - .then(results => res.status(200).send(results)) - .catch(error => res.status(404).send(error)); }; - const deleteOwnerMessage = function (req, res) { + const updateOwnerMessage = async function (req, res) { if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to delete messages!'); + res.status(403).send('You are not authorized to create messages!'); + } + const { standardMessage, message } = req.body; + try { + const results = await OwnerMessage.find({}); + const ownerMessage = results[0]; + if (standardMessage) { + ownerMessage.standardMessage = standardMessage; + } else { + ownerMessage.message = message; + } + await ownerMessage.save(); + res.status(201).send({ + _serverMessage: 'Update successfully!', + ownerMessage: { standardMessage, message }, + }); + } catch (error) { + res.status(500).send(error); } - OwnerMessage.deleteMany({}) - .then((result) => { - result - .then(res.status(200).send({ _serverMessage: 'Message deleted!' })) - .catch((error) => { - res.status(400).send(error); - }); - }) - .catch((error) => { - res.status(400).send(error); - }); }; - const updateOwnerMessage = function (req, res) { + const deleteOwnerMessage = async function (req, res) { if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to update messages!'); + res.status(403).send('You are not authorized to delete messages!'); + } + try { + const results = await OwnerMessage.find({}); + const ownerMessage = results[0]; + ownerMessage.message = ''; + await ownerMessage.save(); + res.status(200).send({ _serverMessage: 'Delete successfully!', ownerMessage }); + } catch (error) { + res.status(500).send(error); } - const { id } = req.params; - - return OwnerMessage.findById(id, (error, ownerMessage) => { - if (error || ownerMessage === null) { - res.status(400).send('No ownerMessage found'); - return; - } - - ownerMessage.message = req.body.newMessage; - ownerMessage.save() - .then(results => res.status(201).send(results)) - .catch(errors => res.status(400).send(errors)); - }); }; return { - postOwnerMessage, getOwnerMessage, - deleteOwnerMessage, updateOwnerMessage, + deleteOwnerMessage, }; }; diff --git a/src/controllers/ownerStandardMessageController.js b/src/controllers/ownerStandardMessageController.js deleted file mode 100644 index efd933888..000000000 --- a/src/controllers/ownerStandardMessageController.js +++ /dev/null @@ -1,64 +0,0 @@ -const ownerStandardMessageController = function (OwnerStandardMessage) { - const postOwnerStandardMessage = function (req, res) { - if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to create messages!'); - } - const ownerStandardMessage = new OwnerStandardMessage(); - ownerStandardMessage.message = req.body.newStandardMessage; - ownerStandardMessage.save().then(() => res.status(201).json({ - _serverMessage: 'Message succesfuly created!', - ownerStandardMessage, - })).catch(err => res.status(500).send({ err })); - }; - - const getOwnerStandardMessage = function (req, res) { - OwnerStandardMessage.find() - .then(results => res.status(200).send(results)) - .catch(error => res.status(404).send(error)); - }; - - const deleteOwnerStandardMessage = function (req, res) { - if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to delete messages!'); - } - OwnerStandardMessage.deleteMany({}) - .then((result) => { - result - .then(res.status(200).send({ _serverMessage: 'Standard Message deleted!' })) - .catch((error) => { - res.status(400).send(error); - }); - }) - .catch((error) => { - res.status(400).send(error); - }); - }; - - const updateOwnerStandardMessage = function (req, res) { - if (req.body.requestor.role !== 'Owner') { - res.status(403).send('You are not authorized to update messages!'); - } - const { id } = req.params; - - return OwnerStandardMessage.findById(id, (error, ownerStandardMessage) => { - if (error || ownerStandardMessage === null) { - res.status(400).send('No ownerStandardMessage found'); - return; - } - - ownerStandardMessage.message = req.body.newStandardMessage; - ownerStandardMessage.save() - .then(results => res.status(201).send(results)) - .catch(errors => res.status(400).send(errors)); - }); - }; - - return { - postOwnerStandardMessage, - getOwnerStandardMessage, - deleteOwnerStandardMessage, - updateOwnerStandardMessage, - }; -}; - -module.exports = ownerStandardMessageController; diff --git a/src/models/ownerMessage.js b/src/models/ownerMessage.js index be953c3a3..a6314c929 100644 --- a/src/models/ownerMessage.js +++ b/src/models/ownerMessage.js @@ -3,7 +3,8 @@ const mongoose = require('mongoose'); const { Schema } = mongoose; const OwnerMessage = new Schema({ - message: { type: String }, + message: { type: String, default: '' }, + standardMessage: { type: String, default: '' }, }); module.exports = mongoose.model('ownerMessage', OwnerMessage, 'ownerMessage'); diff --git a/src/models/ownerStandardMessage.js b/src/models/ownerStandardMessage.js deleted file mode 100644 index d344a773f..000000000 --- a/src/models/ownerStandardMessage.js +++ /dev/null @@ -1,9 +0,0 @@ -const mongoose = require('mongoose'); - -const { Schema } = mongoose; - -const OwnerStandardMessage = new Schema({ - message: { type: String }, -}); - -module.exports = mongoose.model('ownerStandardMessage', OwnerStandardMessage, 'ownerStandardMessage'); diff --git a/src/routes/ownerMessageRouter.js b/src/routes/ownerMessageRouter.js index e436deed8..6f5716fe9 100644 --- a/src/routes/ownerMessageRouter.js +++ b/src/routes/ownerMessageRouter.js @@ -5,14 +5,11 @@ const routes = function (ownerMessage) { const OwnerMessageRouter = express.Router(); OwnerMessageRouter.route('/ownerMessage') - .post(controller.postOwnerMessage) - .get(controller.getOwnerMessage) - .delete(controller.deleteOwnerMessage); + .get(controller.getOwnerMessage) + .put(controller.updateOwnerMessage) + .delete(controller.deleteOwnerMessage); - OwnerMessageRouter.route('/ownerMessage/:id') - .put(controller.updateOwnerMessage); - -return OwnerMessageRouter; + return OwnerMessageRouter; }; module.exports = routes; diff --git a/src/routes/ownerStandardMessageRouter.js b/src/routes/ownerStandardMessageRouter.js deleted file mode 100644 index 08e629c3d..000000000 --- a/src/routes/ownerStandardMessageRouter.js +++ /dev/null @@ -1,18 +0,0 @@ -const express = require('express'); - -const routes = function (ownerStandardMessage) { - const controller = require('../controllers/ownerStandardMessageController')(ownerStandardMessage); - const OwnerStandardMessageRouter = express.Router(); - - OwnerStandardMessageRouter.route('/ownerStandardMessage') - .post(controller.postOwnerStandardMessage) - .get(controller.getOwnerStandardMessage) - .delete(controller.deleteOwnerStandardMessage); - - OwnerStandardMessageRouter.route('/ownerStandardMessage/:id') - .put(controller.updateOwnerStandardMessage); - -return OwnerStandardMessageRouter; -}; - -module.exports = routes; diff --git a/src/startup/routes.js b/src/startup/routes.js index 2d95ea639..ff5d01b2b 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -16,7 +16,6 @@ const inventoryItemType = require('../models/inventoryItemType'); const role = require('../models/role'); const rolePreset = require('../models/rolePreset'); const ownerMessage = require('../models/ownerMessage'); -const ownerStandardMessage = require('../models/ownerStandardMessage'); const profileInitialSetuptoken = require('../models/profileInitialSetupToken'); const reason = require('../models/reason'); const mouseoverText = require('../models/mouseoverText'); @@ -55,7 +54,6 @@ const taskEditSuggestionRouter = require('../routes/taskEditSuggestionRouter')(t const roleRouter = require('../routes/roleRouter')(role); const rolePresetRouter = require('../routes/rolePresetRouter')(rolePreset); const ownerMessageRouter = require('../routes/ownerMessageRouter')(ownerMessage); -const ownerStandardMessageRouter = require('../routes/ownerStandardMessageRouter')(ownerStandardMessage); const reasonRouter = require('../routes/reasonRouter')(reason, userProfile); const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverText); @@ -92,7 +90,6 @@ module.exports = function (app) { app.use('/api', roleRouter); app.use('/api', rolePresetRouter); app.use('/api', ownerMessageRouter); - app.use('/api', ownerStandardMessageRouter); app.use('/api', profileInitialSetupRouter); app.use('/api', reasonRouter); app.use('/api', informationRouter);