diff --git a/back/methods/chat/sendCheckingPresence.js b/back/methods/chat/sendCheckingPresence.js index 3bc022429..5520abb7c 100644 --- a/back/methods/chat/sendCheckingPresence.js +++ b/back/methods/chat/sendCheckingPresence.js @@ -43,7 +43,7 @@ module.exports = Self => { if (!recipient) throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`); - await models.Chat.create({ + const chat = await models.Chat.create({ senderFk: sender.id, recipient: `@${recipient.name}`, dated: new Date(), @@ -52,6 +52,8 @@ module.exports = Self => { status: 0, attempts: 0 }); + console.log(chat); + await models.Chat.sendQueued(chat.id); return true; }; diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js index 66fbfcdc5..cbe963235 100644 --- a/back/methods/chat/sendQueued.js +++ b/back/methods/chat/sendQueued.js @@ -3,7 +3,11 @@ module.exports = Self => { Self.remoteMethodCtx('sendQueued', { description: 'Send a RocketChat message', accessType: 'WRITE', - accepts: [], + accepts: [{ + arg: 'id', + type: 'number', + description: 'The message id' + }], returns: { type: 'object', root: true @@ -14,17 +18,20 @@ module.exports = Self => { } }); - Self.sendQueued = async() => { + Self.sendQueued = async id => { const models = Self.app.models; const maxAttempts = 3; const sentStatus = 1; const errorStatus = 2; + let filter = { + status: {neq: sentStatus}, + attempts: {lt: maxAttempts} + }; + // NOTA: Igual se deberia transaccionar por si coincidiera que en el momento que se esta enviando directamente + // tambien se esta ejecutando la cola que no lo envie dos veces. const chats = await models.Chat.find({ - where: { - status: {neq: sentStatus}, - attempts: {lt: maxAttempts} - } + where: id ? {id} : filter }); for (let chat of chats) {