2020-01-20 06:33:24 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('sendCheckingPresence', {
|
2020-01-20 10:59:15 +00:00
|
|
|
description: 'Sends a RocketChat message to a working worker or department channel',
|
2020-01-20 06:33:24 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2020-10-21 12:51:07 +00:00
|
|
|
arg: 'workerId',
|
2020-01-20 06:33:24 +00:00
|
|
|
type: 'Number',
|
|
|
|
required: true,
|
|
|
|
description: 'The worker id of the destinatary'
|
2021-03-15 11:26:11 +00:00
|
|
|
},
|
|
|
|
{
|
2020-01-20 06:33:24 +00:00
|
|
|
arg: 'message',
|
|
|
|
type: 'String',
|
|
|
|
required: true,
|
|
|
|
description: 'The message'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/sendCheckingPresence`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-14 17:21:04 +00:00
|
|
|
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
|
2020-10-21 11:25:52 +00:00
|
|
|
if (!recipientId) return false;
|
2020-02-28 08:23:02 +00:00
|
|
|
|
2021-05-14 17:21:04 +00:00
|
|
|
let myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2020-01-20 06:33:24 +00:00
|
|
|
const models = Self.app.models;
|
2021-05-14 17:21:04 +00:00
|
|
|
const account = await models.Account.findById(recipientId, null, myOptions);
|
2020-02-28 07:24:01 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
2020-10-21 11:25:52 +00:00
|
|
|
if (recipientId == userId) return false;
|
|
|
|
|
2020-02-28 07:24:01 +00:00
|
|
|
if (!account)
|
2020-10-21 11:25:52 +00:00
|
|
|
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
2020-01-20 06:33:24 +00:00
|
|
|
|
|
|
|
const query = `SELECT worker_isWorking(?) isWorking`;
|
2021-05-14 17:21:04 +00:00
|
|
|
const [result] = await Self.rawSql(query, [recipientId], myOptions);
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2020-02-11 08:14:40 +00:00
|
|
|
if (!result.isWorking) {
|
2020-10-21 11:25:52 +00:00
|
|
|
const workerDepartment = await models.WorkerDepartment.findById(recipientId, {
|
2020-01-20 06:33:24 +00:00
|
|
|
include: {
|
|
|
|
relation: 'department'
|
|
|
|
}
|
2021-05-14 17:21:04 +00:00
|
|
|
}, myOptions);
|
2020-02-13 12:20:31 +00:00
|
|
|
const department = workerDepartment && workerDepartment.department();
|
2020-02-28 06:26:19 +00:00
|
|
|
const channelName = department && department.chatName;
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2020-02-11 08:14:40 +00:00
|
|
|
if (channelName)
|
2020-10-21 11:25:52 +00:00
|
|
|
return Self.send(ctx, `#${channelName}`, `@${account.name} ➔ ${message}`);
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
2020-01-20 10:59:15 +00:00
|
|
|
|
2020-02-11 08:14:40 +00:00
|
|
|
return Self.send(ctx, `@${account.name}`, message);
|
2020-01-20 06:33:24 +00:00
|
|
|
};
|
|
|
|
};
|