This commit is contained in:
parent
c94c8a159e
commit
fb3f3c64c9
|
@ -43,7 +43,7 @@ module.exports = Self => {
|
||||||
if (!recipient)
|
if (!recipient)
|
||||||
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
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,
|
senderFk: sender.id,
|
||||||
recipient: `@${recipient.name}`,
|
recipient: `@${recipient.name}`,
|
||||||
dated: new Date(),
|
dated: new Date(),
|
||||||
|
@ -52,6 +52,8 @@ module.exports = Self => {
|
||||||
status: 0,
|
status: 0,
|
||||||
attempts: 0
|
attempts: 0
|
||||||
});
|
});
|
||||||
|
console.log(chat);
|
||||||
|
await models.Chat.sendQueued(chat.id);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,11 @@ module.exports = Self => {
|
||||||
Self.remoteMethodCtx('sendQueued', {
|
Self.remoteMethodCtx('sendQueued', {
|
||||||
description: 'Send a RocketChat message',
|
description: 'Send a RocketChat message',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [],
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
description: 'The message id'
|
||||||
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
root: true
|
root: true
|
||||||
|
@ -14,17 +18,20 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.sendQueued = async() => {
|
Self.sendQueued = async id => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const maxAttempts = 3;
|
const maxAttempts = 3;
|
||||||
const sentStatus = 1;
|
const sentStatus = 1;
|
||||||
const errorStatus = 2;
|
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({
|
const chats = await models.Chat.find({
|
||||||
where: {
|
where: id ? {id} : filter
|
||||||
status: {neq: sentStatus},
|
|
||||||
attempts: {lt: maxAttempts}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (let chat of chats) {
|
for (let chat of chats) {
|
||||||
|
|
Loading…
Reference in New Issue