Message from JavaScript discussions

October 2020

— It is used as a backend... product of Google

— 

Hi. Anyone konws how can I refactor this REST JSON web service in nodejs, to be easy to maintain, read and evolve in the future? 👇🏻👇🏻👇🏻👇🏻


- Tests
- Async vs Promise vs Callbacks
- Service vs Repository/DAO vs Handler
 
 
// MessageHandler.js
postMessagesHandler: function(req, res) {
   postMessagesList(req.body.stepId, req.body.messages, res);
}
 
async function postMessagesList(stepId, messages, res) {
   let cache = {};
   if (messages) {
       persistence()
           .findall(globals.modelNames.CommunicationMessage, {step_id: globals.generateUuidFromString(stepId)})
           //Find all current messages that have same step_id
           .then(oldMessages => {
               cache.oldMessages = JSON.parse(JSON.stringify(oldMessages));
               return persistence().del(globals.modelNames.CommunicationMessage, {findBy: {step_id: globals.generateUuidFromString(stepId)}})
           })
           //Delete all of them
           .then(result => {
               let promises = [];
               messages.forEach(message => {
                   promises.push(persistence().save(globals.modelNames.CommunicationMessage, message))
               });
               return promises;
           })
           //Save new messages
           .then(messages => {
               cache.newMessages = JSON.parse(JSON.stringify(messages));
               let messageMap = new Map();
               cache.newMessages.forEach(message => messageMap.put(message.id, message));
               let promises = [];
               cache.oldMessages.forEach(message => {
                   //If a message is gone
                   if (!messageMap.get(message.id)) {
                       //Delete children actions
                       promises.push(persistence().del(globals.modelNames.CommunicationAction, {findBy: {message_id: message.id}}));
                   }
               });
               return promises;
           })
           //Cascade delete actions (for deleted messages)
           .then(results => {
               if (cache.newMessages) {
                   res.send(cache.newMessages);
               } else {
                   res.status(httpStatus.NO_CONTENT).send("No content");
               }
           })
           // .then(results => {
           //     if (cache.newMessages) {
           //         res.send(cache.newMessages);
           //     } else {
           //         res.send("No content");
           //     }
           // })
           .catch(function (err) {
               console.error(err);
               res.status(httpStatus.INTERNAL_SERVER_ERROR).send(err.message);
           });
   } else {
       res.status(httpStatus.NO_CONTENT).send("No content");
   }
}

— 8

— Wait what happened? °°
i can't see the only messages and it says that i left the group

— Hmm you used if statement in this cod.can I know the question?

— Yes. Thanks a lot for replying... If u can solve it u will do a big help to me. I will send it for u.

Message permanent page

— Ok I will try to solve it

— Thanks...

— I will message you back when I'm done so please wait 🙂

— Ok.Thankssss alot...

— Ok bro

— I m new in programming