diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js index d31ff9686..0598688e1 100644 --- a/back/methods/chat/sendQueued.js +++ b/back/methods/chat/sendQueued.js @@ -3,17 +3,7 @@ module.exports = Self => { Self.remoteMethodCtx('sendQueued', { description: 'Send a RocketChat message', accessType: 'WRITE', - accepts: [{ - arg: 'to', - type: 'string', - required: true, - description: 'User (@) or channel (#) to send the message' - }, { - arg: 'message', - type: 'string', - required: true, - description: 'The message' - }], + accepts: [], returns: { type: 'object', root: true @@ -24,22 +14,35 @@ module.exports = Self => { } }); - Self.sendQueued = async(ctx, to, message) => { + Self.sendQueued = async ctx => { const models = Self.app.models; - const accessToken = ctx.req.accessToken; - const sender = await models.Account.findById(accessToken.userId); - const recipient = to.replace('@', ''); - if (sender.name != recipient) { - await sendMessage(sender, to, message); + const sentStatus = 1; + const chats = await models.Chat.find({ + where: { + status: {neq: sentStatus} + } + }); - return true; + for (let chat of chats) { + if (chat.checkUserStatus) + await sendCheckingPresence(ctx, chat); + else + await sendMessage(chat); } - - return false; }; - async function sendMessage(sender, channel, message) { + async function sendCheckingPresence(ctx, chat) { + const models = Self.app.models; + const recipient = await models.Account.findOne({ + where: { + name: chat.recipient + } + }); + await models.Chat.sendCheckingPresence(ctx, recipient.id, chat.message); + } + + async function sendMessage(chat) { if (process.env.NODE_ENV !== 'production') { return new Promise(resolve => { return resolve({ @@ -49,6 +52,12 @@ module.exports = Self => { }); } + const sender = await models.Account.findOne({ + where: { + name: chat.senderFk + } + }); + const login = await Self.getServiceAuth(); const avatar = `${login.host}/avatar/${sender.name}`; @@ -60,10 +69,10 @@ module.exports = Self => { }; return axios.post(`${login.api}/chat.postMessage`, { - 'channel': channel, + 'channel': `@${chat.recipient}`, 'avatar': avatar, 'alias': sender.nickname, - 'text': message + 'text': chat.message }, options); } }; diff --git a/back/models/chat.js b/back/models/chat.js index 7d8468aae..95a1e2c29 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -3,4 +3,5 @@ module.exports = Self => { require('../methods/chat/send')(Self); require('../methods/chat/sendCheckingPresence')(Self); require('../methods/chat/notifyIssues')(Self); + require('../methods/chat/sendQueued')(Self); }; diff --git a/back/models/chat.json b/back/models/chat.json index 697d8c181..8fc3a6304 100644 --- a/back/models/chat.json +++ b/back/models/chat.json @@ -1,6 +1,39 @@ { "name": "Chat", "base": "VnModel", + "options": { + "mysql": { + "table": "chat" + } + }, + "properties": { + "id": { + "id": true, + "type": "number", + "description": "Identifier" + }, + "senderFk": { + "type": "number" + }, + "recipient": { + "type": "string" + }, + "dated": { + "type": "date" + }, + "checkUserStatus": { + "type": "boolean" + }, + "message": { + "type": "string" + }, + "status": { + "type": "string" + }, + "attempts": { + "type": "number" + } + }, "acls": [{ "property": "validations", "accessType": "EXECUTE", diff --git a/db/changes/10470-family/00-chat.sql b/db/changes/10470-family/00-chat.sql index 0c03f2bcb..d4a8f068a 100644 --- a/db/changes/10470-family/00-chat.sql +++ b/db/changes/10470-family/00-chat.sql @@ -1,10 +1,13 @@ CREATE TABLE `vn`.`chat` ( - `id` int(11) DEFAULT NULL, - `senderFk` int(11) DEFAULT NULL, - `recipient` varchar(50) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `id` int(11) NOT NULL AUTO_INCREMENT, + `senderFk` int(10) unsigned DEFAULT NULL, + `recipient` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL, `dated` date DEFAULT NULL, `checkUserStatus` tinyint(1) DEFAULT NULL, - `message` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `message` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL, `status` tinyint(1) DEFAULT NULL, - `attempts` int(1) DEFAULT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci \ No newline at end of file + `attempts` int(1) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `chat_FK` (`senderFk`), + CONSTRAINT `chat_FK` FOREIGN KEY (`senderFk`) REFERENCES `account`.`user` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c69012f41..23b9be646 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2558,7 +2558,11 @@ INSERT INTO `vn`.`supplierAgencyTerm` (`agencyFk`, `supplierFk`, `minimumPackage (2, 1, 60, 0.00, 0.00, NULL, 0, 5.00, 33), (3, 2, 0, 15.00, 0.00, NULL, 0, 0.00, 0), (4, 2, 0, 20.00, 0.00, NULL, 0, 0.00, 0), - (5, 442, 0, 0.00, 3.05, NULL, 0, 0.00, 0); + (5, 442, 0, 0.00, 3.05, NULL, 0, 0.00, 0); + +INSERT INTO `vn`.`chat` (`senderFk`, `recipient`, `dated`, `checkUserStatus`, `message`, `status`, `attempts`) + VALUES + (1101, 'PetterParker', '2022-06-02', 1, 'First test message', 0, 0); INSERT INTO `vn`.`mobileAppVersionControl` (`appName`, `version`, `isVersionCritical`) VALUES