63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('sendCheckingPresence', {
|
|
description: 'Sends a RocketChat message to a working worker or department channel',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'workerId',
|
|
type: 'Number',
|
|
required: true,
|
|
description: 'The worker id of the destinatary'
|
|
},
|
|
{
|
|
arg: 'message',
|
|
type: 'String',
|
|
required: true,
|
|
description: 'The message'
|
|
}],
|
|
returns: {
|
|
type: 'Object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/sendCheckingPresence`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
|
|
if (!recipientId) return false;
|
|
|
|
let myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const models = Self.app.models;
|
|
const account = await models.Account.findById(recipientId, null, myOptions);
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
if (recipientId == userId) return false;
|
|
|
|
if (!account)
|
|
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
|
|
|
const query = `SELECT worker_isWorking(?) isWorking`;
|
|
const [result] = await Self.rawSql(query, [recipientId], myOptions);
|
|
|
|
if (!result.isWorking) {
|
|
const workerDepartment = await models.WorkerDepartment.findById(recipientId, {
|
|
include: {
|
|
relation: 'department'
|
|
}
|
|
}, myOptions);
|
|
const department = workerDepartment && workerDepartment.department();
|
|
const channelName = department && department.chatName;
|
|
|
|
if (channelName)
|
|
return Self.send(ctx, `#${channelName}`, `@${account.name} ➔ ${message}`);
|
|
}
|
|
|
|
return Self.send(ctx, `@${account.name}`, message);
|
|
};
|
|
};
|