2022-02-22 06:30:26 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
|
2020-01-20 06:33:24 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('sendCheckingPresence', {
|
2022-03-01 10:09:10 +00:00
|
|
|
description: 'Sends a RocketChat message to a connected user or department channel',
|
2020-01-20 06:33:24 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2022-04-07 11:08:53 +00:00
|
|
|
arg: 'workerId',
|
2022-02-22 06:30:26 +00:00
|
|
|
type: 'number',
|
2020-01-20 06:33:24 +00:00
|
|
|
required: true,
|
2022-02-22 06:30:26 +00:00
|
|
|
description: 'The recipient user id'
|
2021-03-15 11:26:11 +00:00
|
|
|
},
|
|
|
|
{
|
2020-01-20 06:33:24 +00:00
|
|
|
arg: 'message',
|
2022-02-22 06:30:26 +00:00
|
|
|
type: 'string',
|
2020-01-20 06:33:24 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The message'
|
|
|
|
}],
|
|
|
|
returns: {
|
2022-02-22 06:30:26 +00:00
|
|
|
type: 'object',
|
2020-01-20 06:33:24 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/sendCheckingPresence`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-14 17:21:04 +00:00
|
|
|
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
|
2020-10-21 11:25:52 +00:00
|
|
|
if (!recipientId) return false;
|
2020-02-28 08:23:02 +00:00
|
|
|
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2021-05-14 17:21:04 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2020-01-20 06:33:24 +00:00
|
|
|
const models = Self.app.models;
|
2020-02-28 07:24:01 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2022-02-22 06:30:26 +00:00
|
|
|
const recipient = await models.Account.findById(recipientId, null, myOptions);
|
2020-02-28 07:24:01 +00:00
|
|
|
|
2022-02-22 06:30:26 +00:00
|
|
|
// Prevent sending messages to yourself
|
2020-10-21 11:25:52 +00:00
|
|
|
if (recipientId == userId) return false;
|
|
|
|
|
2022-02-22 06:30:26 +00:00
|
|
|
if (!recipient)
|
2020-10-21 11:25:52 +00:00
|
|
|
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2022-02-23 11:07:05 +00:00
|
|
|
const {data} = await Self.getUserStatus(recipient.name);
|
2022-02-22 06:30:26 +00:00
|
|
|
if (data) {
|
2022-04-12 11:01:34 +00:00
|
|
|
if (data.status === 'offline' || data.status === 'busy') {
|
2022-02-22 06:30:26 +00:00
|
|
|
// Send message to department room
|
|
|
|
const workerDepartment = await models.WorkerDepartment.findById(recipientId, {
|
|
|
|
include: {
|
|
|
|
relation: 'department'
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
const department = workerDepartment && workerDepartment.department();
|
|
|
|
const channelName = department && department.chatName;
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2022-02-22 06:30:26 +00:00
|
|
|
if (channelName)
|
|
|
|
return Self.send(ctx, `#${channelName}`, `@${recipient.name} ➔ ${message}`);
|
2022-04-12 12:44:32 +00:00
|
|
|
else
|
|
|
|
return Self.send(ctx, `@${recipient.name}`, message);
|
2022-02-22 06:30:26 +00:00
|
|
|
} else
|
|
|
|
return Self.send(ctx, `@${recipient.name}`, message);
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
|
|
|
};
|
2022-02-22 06:30:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current user status on Rocketchat
|
|
|
|
*
|
|
|
|
* @param {string} username - The recipient user name
|
|
|
|
* @return {Promise} - The request promise
|
|
|
|
*/
|
2022-02-23 11:07:05 +00:00
|
|
|
Self.getUserStatus = async function getUserStatus(username) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
return resolve({
|
|
|
|
data: {
|
|
|
|
status: 'online'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-22 06:30:26 +00:00
|
|
|
const login = await Self.getServiceAuth();
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
params: {username},
|
|
|
|
headers: {
|
|
|
|
'X-Auth-Token': login.auth.token,
|
|
|
|
'X-User-Id': login.auth.userId
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return axios.get(`${login.api}/users.getStatus`, options);
|
2022-02-23 11:07:05 +00:00
|
|
|
};
|
2020-01-20 06:33:24 +00:00
|
|
|
};
|