salix/back/methods/chat/sendCheckingPresence.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-01-20 06:33:24 +00:00
const request = require('request-promise-native');
module.exports = Self => {
Self.remoteMethodCtx('sendCheckingPresence', {
description: 'Send a RocketChat message',
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]);
if (result.isWorking) {
const username = `@${account.name}`;
return await Self.send(ctx, username, message);
} else {
const workerDepartment = await models.WorkerDepartment.findById(workerId, {
include: {
relation: 'department'
}
});
const department = workerDepartment.department;
const channelName = department.chatChannelName;
const room = `#${channelName}`;
return await Self.send(ctx, room, message);
}
};
};