diff --git a/backend/.gitignore b/backend/.gitignore index 88dd195..2fe45bb 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,4 +1,4 @@ node_modules -public/* -!public/.gitkeep +src/public/* +!src/public/.gitkeep .env \ No newline at end of file diff --git a/backend/src/middleware/is-auth.js b/backend/src/middleware/is-auth.js index 1b19142..07d07d6 100644 --- a/backend/src/middleware/is-auth.js +++ b/backend/src/middleware/is-auth.js @@ -14,9 +14,7 @@ module.exports = (req, res, next) => { } if (!decodedToken) { - const error = new Error("Falha na autenticação"); - error.statusCode = 401; - next(error); + return res.status(401).json({ message: "Unauthorized" }); } next(); diff --git a/backend/src/routes/contacts.js b/backend/src/routes/contacts.js index d2ba031..ef4719c 100644 --- a/backend/src/routes/contacts.js +++ b/backend/src/routes/contacts.js @@ -6,7 +6,6 @@ const ContactController = require("../controllers/ContactController"); const routes = express.Router(); routes.get("/contacts", isAuth, ContactController.index); -// routes.post(ContactController.postCreateContact); routes.post("/contacts", isAuth, ContactController.store); diff --git a/backend/src/routes/messages.js b/backend/src/routes/messages.js index 564da57..dfb3811 100644 --- a/backend/src/routes/messages.js +++ b/backend/src/routes/messages.js @@ -6,6 +6,7 @@ const MessageController = require("../controllers/MessageController"); const routes = express.Router(); routes.get("/messages/:contactId", isAuth, MessageController.index); + routes.post("/messages/:contactId", isAuth, MessageController.store); module.exports = routes; diff --git a/backend/src/services/wbotMessageListener.js b/backend/src/services/wbotMessageListener.js index b80beab..4fda3a5 100644 --- a/backend/src/services/wbotMessageListener.js +++ b/backend/src/services/wbotMessageListener.js @@ -14,8 +14,7 @@ const wbotMessageListener = () => { wbot.on("message", async msg => { console.log(msg); let newMessage; - // console.log(msg); - if (msg.from === "status@broadcast") { + if (msg.from === "status@broadcast" || msg.type === "location") { return; } try { @@ -28,9 +27,7 @@ const wbotMessageListener = () => { if (contact) { await contact.update({ profilePicUrl: profilePicUrl }); - } - - if (!contact) { + } else { try { contact = await Contact.create({ name: msgContact.pushname || msgContact.number.toString(), @@ -81,24 +78,26 @@ const wbotMessageListener = () => { await contact.update({ lastMessage: msg.body }); } - io.to(contact.id) - .to("notification") - .emit("appMessage", { - action: "create", - message: { - ...newMessage.dataValues, - mediaUrl: `${ - newMessage.mediaUrl - ? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}` - : "" - }`, - }, - contact: { - ...contact.dataValues, - unreadMessages: 1, - lastMessage: newMessage.messageBody, - }, - }); + const serializedMessage = { + ...newMessage.dataValues, + mediaUrl: `${ + newMessage.mediaUrl + ? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}` + : "" + }`, + }; + + const serializaedContact = { + ...contact.dataValues, + unreadMessages: 1, + lastMessage: newMessage.messageBody, + }; + + io.to(contact.id).to("notification").emit("appMessage", { + action: "create", + message: serializedMessage, + contact: serializaedContact, + }); let chat = await msg.getChat(); chat.sendSeen();