Added back unit tests
gitea/salix/2015-chat_sendCheckingPresence This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-01-20 11:59:15 +01:00
parent 3226081081
commit 3ff1fb7d2d
8 changed files with 121 additions and 20 deletions

View File

@ -1,7 +1,6 @@
const request = require('request-promise-native');
module.exports = Self => {
Self.remoteMethodCtx('sendCheckingPresence', {
description: 'Send a RocketChat message',
description: 'Sends a RocketChat message to a working worker or department channel',
accessType: 'WRITE',
accepts: [{
arg: 'workerId',
@ -29,24 +28,26 @@ module.exports = Self => {
const account = await models.Account.findById(workerId);
const query = `SELECT worker_isWorking(?) isWorking`;
const result = await Self.rawSql(query, [workerId]);
const [result] = await Self.rawSql(query, [workerId]);
if (result.isWorking) {
const username = `@${account.name}`;
return await Self.send(ctx, username, message);
} else {
let room;
if (result.isWorking)
room = `@${account.name}`;
else {
const workerDepartment = await models.WorkerDepartment.findById(workerId, {
include: {
relation: 'department'
}
});
const department = workerDepartment.department;
const channelName = department.chatChannelName;
const room = `#${channelName}`;
const department = workerDepartment.department();
const channelName = department.chatName;
room = `#${channelName}`;
return await Self.send(ctx, room, message);
if (channelName)
room = `#${channelName}`;
else room = `@${account.name}`;
}
return Self.send(ctx, room, message);
};
};

View File

@ -1,9 +1,9 @@
const app = require('vn-loopback/server/server');
describe('chat sendMessage()', () => {
describe('chat send()', () => {
it('should return a "Fake notification sent" as response', async() => {
let ctx = {req: {accessToken: {userId: 1}}};
let response = await app.models.Chat.sendMessage(ctx, '@salesPerson', 'I changed something');
let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something');
expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent');
@ -11,7 +11,7 @@ describe('chat sendMessage()', () => {
it('should not return a response', async() => {
let ctx = {req: {accessToken: {userId: 18}}};
let response = await app.models.Chat.sendMessage(ctx, '@salesPerson', 'I changed something');
let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something');
expect(response).toBeUndefined();
});

View File

@ -0,0 +1,65 @@
const app = require('vn-loopback/server/server');
describe('chat sendCheckingPresence()', () => {
const departmentId = 23;
const workerId = 107;
let timeEntry;
afterAll(async done => {
const department = await app.models.Department.findById(departmentId);
await department.updateAttribute('chatName', null);
await app.models.WorkerTimeControl.destroyById(timeEntry.id);
done();
});
it(`should call to send() method with the worker username when no department channel is specified
and then return a "Fake notification sent" as response`, async() => {
const ctx = {req: {accessToken: {userId: 1}}};
const chatModel = app.models.Chat;
spyOn(chatModel, 'send').and.callThrough();
const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something');
});
it(`should call to send() method with the worker department channel if is specified
and then return a "Fake notification sent" as response`, async() => {
const ctx = {req: {accessToken: {userId: 1}}};
const chatModel = app.models.Chat;
spyOn(chatModel, 'send').and.callThrough();
const department = await app.models.Department.findById(departmentId);
await department.updateAttribute('chatName', 'cooler');
const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', 'I changed something');
});
it(`should call to send() method with the worker username when the worker is working`, async() => {
const ctx = {req: {accessToken: {userId: 1}}};
const chatModel = app.models.Chat;
spyOn(chatModel, 'send').and.callThrough();
const today = new Date();
today.setHours(6, 0);
timeEntry = await app.models.WorkerTimeControl.create({
userFk: workerId,
timed: today,
manual: false,
direction: 'in'
});
const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
expect(response.statusCode).toEqual(200);
expect(response.message).toEqual('Fake notification sent');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something');
});
});

View File

@ -1,2 +1,2 @@
ALTER TABLE `vn`.`department`
ADD COLUMN `chatChannelName` VARCHAR(45) NULL AFTER `path`;
ADD COLUMN `chatName` VARCHAR(45) NULL AFTER `path`;

View File

@ -0,0 +1,32 @@
USE `vn`;
DROP function IF EXISTS `worker_isWorking`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` FUNCTION `worker_isWorking`(vWorkerFk INT) RETURNS tinyint(1)
READS SQL DATA
BEGIN
/**
* Comprueba si el trabajador está trabajando en el momento de la consulta
* @return Devuelve TRUE en caso de que este trabajando. Si se encuentra en un descanso devolverá FALSE
*/
DECLARE vLastIn DATETIME ;
SELECT MAX(timed) INTO vLastIn
FROM vn.workerTimeControl
WHERE userFk = vWorkerFk AND
direction = 'in';
IF (SELECT MOD(COUNT(*),2)
FROM vn.workerTimeControl
WHERE userFk = vWorkerFk AND
timed >= vLastIn
) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END$$
DELIMITER ;

View File

@ -155,10 +155,10 @@ module.exports = function(Self) {
const result = await realMethod.call(this, data, options);
if (cb) cb(null, result);
else return result;
} catch (err) {
let myErr = replaceErr(err, replaceErrFunc);
if (cb)
cb(myErr);
if (cb) cb(myErr);
else
throw myErr;
}

View File

@ -94,7 +94,7 @@ module.exports = Self => {
id: id,
url: `${origin}/#!/ticket/${id}/summary`
});
await models.Chat.sendMessage(ctx, `@${salesPersonUser}`, message);
await models.Chat.send(ctx, `@${salesPersonUser}`, message);
}
return ticket.updateAttribute('isDeleted', true);

View File

@ -25,6 +25,9 @@
},
"sons": {
"type": "Number"
},
"chatName": {
"type": "String"
}
}
}