salix/back/methods/chat/sendCheckingPresence.js

54 lines
1.6 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, workerId, message) => {
const models = Self.app.models;
const account = await models.Account.findById(workerId);
const query = `SELECT worker_isWorking(?) isWorking`;
const [result] = await Self.rawSql(query, [workerId]);
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.chatName;
if (channelName) {
message = `@${account.name} => ${message}`;
room = `#${channelName}`;
} else room = `@${account.name}`;
}
return Self.send(ctx, room, message);
};
};