refs #4858
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-12-19 09:59:03 +01:00
parent c94c8a159e
commit fb3f3c64c9
2 changed files with 16 additions and 7 deletions

View File

@ -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;
};

View File

@ -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;
const chats = await models.Chat.find({
where: {
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: id ? {id} : filter
});
for (let chat of chats) {