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: [{
|
|
|
|
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, workerId, message) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const account = await models.Account.findById(workerId);
|
2020-02-28 07:24:01 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
if (!account)
|
|
|
|
throw new Error(`Could not send message to worker id ${workerId} from user ${userId}`);
|
2020-01-20 06:33:24 +00:00
|
|
|
|
|
|
|
const query = `SELECT worker_isWorking(?) isWorking`;
|
2020-01-20 10:59:15 +00:00
|
|
|
const [result] = await Self.rawSql(query, [workerId]);
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2020-02-11 08:14:40 +00:00
|
|
|
if (!result.isWorking) {
|
2020-01-20 06:33:24 +00:00
|
|
|
const workerDepartment = await models.WorkerDepartment.findById(workerId, {
|
|
|
|
include: {
|
|
|
|
relation: 'department'
|
|
|
|
}
|
|
|
|
});
|
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)
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|