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); } }; };