4858-chat_realTime #1313

Merged
vicent merged 11 commits from 4858-chat_realTime into dev 2023-03-24 10:12:25 +00:00
4 changed files with 68 additions and 20 deletions
Showing only changes of commit d85717ca04 - Show all commits

View File

@ -36,7 +36,7 @@ module.exports = Self => {
dated: new Date(), dated: new Date(),
checkUserStatus: 0, checkUserStatus: 0,
message: message, message: message,
status: 0, status: 'pending',
attempts: 0 attempts: 0
}); });

View File

@ -27,11 +27,17 @@ module.exports = Self => {
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => { Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
if (!recipientId) return false; if (!recipientId) return false;
let tx;
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
vicent marked this conversation as resolved Outdated

aci faltaria un try catch i el rollback

aci faltaria un try catch i el rollback
Outdated
Review

Quitada transaccion

Quitada transaccion
const models = Self.app.models; const models = Self.app.models;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const sender = await models.Account.findById(userId); const sender = await models.Account.findById(userId);
@ -49,12 +55,37 @@ module.exports = Self => {
dated: new Date(), dated: new Date(),
checkUserStatus: 1, checkUserStatus: 1,
message: message, message: message,
status: 0, status: 'sending',
attempts: 0 attempts: 0
}); }, myOptions);
console.log(chat);
await models.Chat.sendQueued(chat.id); try {
await Self.sendCheckingUserStatus(chat);
await updateChat(chat, 'sent', myOptions);
} catch (error) {
await updateChat(chat, 'error', error, myOptions);
}
if (tx) await tx.commit();
return true; return true;
}; };
/**
* Update status and attempts of a chat
*
* @param {object} chat - The chat
* @param {string} status - The new status
* @param {string} error - The error
* @param {object} options - Query options
* @return {Promise} - The request promise
*/
async function updateChat(chat, status, error, options) {
return chat.updateAttributes({
status: status,
attempts: ++chat.attempts,
error: error
}, options);
}
}; };

View File

@ -3,11 +3,6 @@ module.exports = Self => {
Self.remoteMethodCtx('sendQueued', { Self.remoteMethodCtx('sendQueued', {
description: 'Send a RocketChat message', description: 'Send a RocketChat message',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
description: 'The message id'
}],
returns: { returns: {
type: 'object', type: 'object',
root: true root: true
@ -18,20 +13,25 @@ module.exports = Self => {
} }
}); });
Self.sendQueued = async id => { Self.sendQueued = async() => {
const models = Self.app.models; const models = Self.app.models;
const maxAttempts = 3; const maxAttempts = 3;
const sentStatus = 1; const sentStatus = 'sent';
const errorStatus = 2; const sendingStatus = 'sending';
let filter = { const errorStatus = 'error';
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: id ? {id} : filter where: {
status: {
neq: {
and: [
sentStatus,
sendingStatus
]
}
},
attempts: {lt: maxAttempts}
}
}); });
for (let chat of chats) { for (let chat of chats) {
@ -137,6 +137,7 @@ module.exports = Self => {
* @param {string} error - The error * @param {string} error - The error
* @return {Promise} - The request promise * @return {Promise} - The request promise
*/ */
async function updateChat(chat, status, error) { async function updateChat(chat, status, error) {
return chat.updateAttributes({ return chat.updateAttributes({
status: status, status: status,

View File

@ -0,0 +1,16 @@
ALTER TABLE `vn`.`chat` ADD statusNew enum('pending','sent','error','sending') DEFAULT 'pending' NOT NULL;
UPDATE `vn`.`chat`
SET statusNew = 'pending'
WHERE status = 0;
UPDATE `vn`.`chat`
SET statusNew = 'sent'
WHERE status = 1;
UPDATE `vn`.`chat`
SET statusNew = 'error'
WHERE status = 2;
ALTER TABLE `vn`.`chat` CHANGE status status__ tinyint(1) DEFAULT NULL NULL;
ALTER TABLE `vn`.`chat` CHANGE statusNew status enum('pending','sent','error','sending') CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'pending' NOT NULL;