diff --git a/back/methods/chat/send.js b/back/methods/chat/send.js index ece0d4994..d62c84e04 100644 --- a/back/methods/chat/send.js +++ b/back/methods/chat/send.js @@ -42,7 +42,7 @@ module.exports = Self => { return true; } - }; - return false; + return false; + }; }; diff --git a/back/methods/chat/sendCheckingPresence.js b/back/methods/chat/sendCheckingPresence.js index be233c96f..569ce3475 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}`); - return models.Chat.create({ + await models.Chat.create({ senderFk: sender.id, recipient: `@${recipient.name}`, dated: new Date(), @@ -52,5 +52,7 @@ module.exports = Self => { status: 0, attempts: 0 }); + + return true; }; }; diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js index 1b4e5d68c..e7ab777af 100644 --- a/back/methods/chat/sendQueued.js +++ b/back/methods/chat/sendQueued.js @@ -30,14 +30,14 @@ module.exports = Self => { for (let chat of chats) { if (chat.checkUserStatus) { try { - await sendCheckingUserStatus(chat); + await Self.sendCheckingUserStatus(chat); await updateChat(chat, sentStatus); } catch (error) { await updateChat(chat, errorStatus); } } else { try { - await sendMessage(chat.senderFk, chat.recipient, chat.message); + await Self.sendMessage(chat.senderFk, chat.recipient, chat.message); await updateChat(chat, sentStatus); } catch (error) { await updateChat(chat, errorStatus); @@ -46,7 +46,7 @@ module.exports = Self => { } }; - async function sendCheckingUserStatus(chat) { + Self.sendCheckingUserStatus = async function sendCheckingUserStatus(chat) { const models = Self.app.models; const recipientName = chat.recipient.slice(1); @@ -56,7 +56,7 @@ module.exports = Self => { } }); - const {data} = await getUserStatus(recipient.name); + const {data} = await Self.getUserStatus(recipient.name); if (data) { if (data.status === 'offline' || data.status === 'busy') { // Send message to department room @@ -75,7 +75,8 @@ module.exports = Self => { } else return sendMessage(chat.senderFk, `@${recipient.name}`, chat.message); } - } + }; + /** * Update status and attempts of a chat * @@ -84,7 +85,7 @@ module.exports = Self => { * @param {string} message - The message to send * @return {Promise} - The request promise */ - async function sendMessage(senderFk, recipient, message) { + Self.sendMessage = async function sendMessage(senderFk, recipient, message) { if (process.env.NODE_ENV !== 'production') { return new Promise(resolve => { return resolve({ @@ -113,15 +114,15 @@ module.exports = Self => { 'alias': sender.nickname, 'text': message }, options); - } + }; /** - * Update status and attempts of a chat - * - * @param {object} chat - The chat - * @param {string} status - The new status - * @return {Promise} - The request promise - */ + * Update status and attempts of a chat + * + * @param {object} chat - The chat + * @param {string} status - The new status + * @return {Promise} - The request promise + */ async function updateChat(chat, status) { return chat.updateAttributes({ status: status, @@ -130,12 +131,12 @@ module.exports = Self => { } /** - * Returns the current user status on Rocketchat - * - * @param {string} username - The recipient user name - * @return {Promise} - The request promise - */ - async function getUserStatus(username) { + * Returns the current user status on Rocketchat + * + * @param {string} username - The recipient user name + * @return {Promise} - The request promise + */ + Self.getUserStatus = async function getUserStatus(username) { if (process.env.NODE_ENV !== 'production') { return new Promise(resolve => { return resolve({ @@ -157,5 +158,5 @@ module.exports = Self => { }; return axios.get(`${login.api}/users.getStatus`, options); - } + }; }; diff --git a/back/methods/chat/spec/send.spec.js b/back/methods/chat/spec/send.spec.js index 634e1d420..dd07a1342 100644 --- a/back/methods/chat/spec/send.spec.js +++ b/back/methods/chat/spec/send.spec.js @@ -1,14 +1,14 @@ const app = require('vn-loopback/server/server'); describe('Chat send()', () => { - it('should return a "Fake notification sent" as response', async() => { + it('should return true as response', async() => { let ctx = {req: {accessToken: {userId: 1}}}; let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something'); expect(response).toEqual(true); }); - it('should retrun false as response', async() => { + it('should return false as response', async() => { let ctx = {req: {accessToken: {userId: 18}}}; let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something'); diff --git a/back/methods/chat/spec/sendCheckingPresence.spec.js b/back/methods/chat/spec/sendCheckingPresence.spec.js index 712e7f947..5d1a1b3dd 100644 --- a/back/methods/chat/spec/sendCheckingPresence.spec.js +++ b/back/methods/chat/spec/sendCheckingPresence.spec.js @@ -1,58 +1,21 @@ const models = require('vn-loopback/server/server').models; describe('Chat sendCheckingPresence()', () => { - const today = new Date(); - today.setHours(6, 0); - const ctx = {req: {accessToken: {userId: 1}}}; - const chatModel = models.Chat; - const departmentId = 23; - const workerId = 1107; + it('should return true as response', async() => { + const workerId = 1107; - it(`should call to send() method with "@HankPym" as recipient argument`, async() => { - spyOn(chatModel, 'send').and.callThrough(); - spyOn(chatModel, 'getUserStatus').and.returnValue( - new Promise(resolve => { - return resolve({ - data: { - status: 'online' - } - }); - }) - ); + let ctx = {req: {accessToken: {userId: 1}}}; + let response = await models.Chat.sendCheckingPresence(ctx, workerId, 'I changed something'); - await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); - - expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something'); + expect(response).toEqual(true); }); - it(`should call to send() method with "#cooler" as recipient argument`, async() => { - spyOn(chatModel, 'send').and.callThrough(); - spyOn(chatModel, 'getUserStatus').and.returnValue( - new Promise(resolve => { - return resolve({ - data: { - status: 'offline' - } - }); - }) - ); + it('should return false as response', async() => { + const salesPersonId = 18; - const tx = await models.Claim.beginTransaction({}); + let ctx = {req: {accessToken: {userId: 18}}}; + let response = await models.Chat.sendCheckingPresence(ctx, salesPersonId, 'I changed something'); - try { - const options = {transaction: tx}; - - const department = await models.Department.findById(departmentId, null, options); - await department.updateAttribute('chatName', 'cooler'); - - await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); - - expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } + expect(response).toEqual(false); }); }); diff --git a/back/methods/chat/spec/sendQueued.spec.js b/back/methods/chat/spec/sendQueued.spec.js new file mode 100644 index 000000000..bbf5a73c7 --- /dev/null +++ b/back/methods/chat/spec/sendQueued.spec.js @@ -0,0 +1,41 @@ +const models = require('vn-loopback/server/server').models; + +describe('Chat sendCheckingPresence()', () => { + const today = new Date(); + today.setHours(6, 0); + const chatModel = models.Chat; + + it(`should call to sendCheckingUserStatus()`, async() => { + spyOn(chatModel, 'sendCheckingUserStatus').and.callThrough(); + + const chat = { + checkUserStatus: 1, + status: 0, + attempts: 0 + }; + + await chatModel.destroyAll(); + await chatModel.create(chat); + + await chatModel.sendQueued(); + + expect(chatModel.sendCheckingUserStatus).toHaveBeenCalled(); + }); + + it(`should call to sendMessage() method`, async() => { + spyOn(chatModel, 'sendMessage').and.callThrough(); + + const chat = { + checkUserStatus: 0, + status: 0, + attempts: 0 + }; + + await chatModel.destroyAll(); + await chatModel.create(chat); + + await chatModel.sendQueued(); + + expect(chatModel.sendMessage).toHaveBeenCalled(); + }); +});