Skip to content

Commit

Permalink
Merge pull request #645 from OneCommunityGlobal/xiaow_hotfix_and_refa…
Browse files Browse the repository at this point in the history
…ctor_of_ownermessage

XiaoW_Hotfix and Refactor ownerMessage related backend handler
  • Loading branch information
one-community authored Dec 11, 2023
2 parents a185364 + e51d0b4 commit 80c5f81
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 147 deletions.
88 changes: 43 additions & 45 deletions src/controllers/ownerMessageController.js
Original file line number Diff line number Diff line change
@@ -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,
};
};

Expand Down
64 changes: 0 additions & 64 deletions src/controllers/ownerStandardMessageController.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/models/ownerMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
9 changes: 0 additions & 9 deletions src/models/ownerStandardMessage.js

This file was deleted.

11 changes: 4 additions & 7 deletions src/routes/ownerMessageRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
18 changes: 0 additions & 18 deletions src/routes/ownerStandardMessageRouter.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 80c5f81

Please sign in to comment.