2018-11-12 12:20:44 -02:00
|
|
|
const Fs = require('fs');
|
2019-03-09 16:59:31 -03:00
|
|
|
import urlJoin = require('url-join');
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
import { GBService, IGBInstance } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models';
|
2019-06-27 09:22:32 -03:00
|
|
|
import { ConversationReference } from 'botbuilder';
|
2020-01-26 17:43:50 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
/**
|
|
|
|
* Security service layer.
|
|
|
|
*/
|
2018-04-21 02:59:30 -03:00
|
|
|
export class SecService extends GBService {
|
2018-11-12 12:20:44 -02:00
|
|
|
public async importSecurityFile(localPath: string, instance: IGBInstance) {
|
2019-03-09 16:59:31 -03:00
|
|
|
const security = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'security.json'), 'utf8'));
|
2020-01-26 17:43:50 -03:00
|
|
|
await CollectionUtil.asyncForEach(security.groups, async group => {
|
2018-11-12 12:20:44 -02:00
|
|
|
const groupDb = GuaribasGroup.build({
|
2018-04-21 02:59:30 -03:00
|
|
|
instanceId: instance.instanceId,
|
|
|
|
displayName: group.displayName
|
2018-11-12 12:20:44 -02:00
|
|
|
});
|
2020-01-26 17:43:50 -03:00
|
|
|
const g1 = await groupDb.save();
|
|
|
|
await CollectionUtil.asyncForEach(group.users, async user => {
|
|
|
|
const userDb = GuaribasUser.build({
|
|
|
|
instanceId: instance.instanceId,
|
|
|
|
groupId: g1.groupId,
|
|
|
|
userName: user.userName
|
2018-11-12 12:20:44 -02:00
|
|
|
});
|
2020-01-26 17:43:50 -03:00
|
|
|
const user2 = await userDb.save();
|
|
|
|
const userGroup = GuaribasUserGroup.build();
|
|
|
|
userGroup.groupId = g1.groupId;
|
|
|
|
userGroup.userId = user2.userId;
|
|
|
|
await userGroup.save();
|
2018-11-12 12:20:44 -02:00
|
|
|
});
|
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
public async ensureUser(
|
2018-04-21 02:59:30 -03:00
|
|
|
instanceId: number,
|
|
|
|
userSystemId: string,
|
|
|
|
userName: string,
|
|
|
|
address: string,
|
|
|
|
channelName: string,
|
2020-05-02 21:28:13 -03:00
|
|
|
displayName: string
|
2018-09-09 14:39:37 -03:00
|
|
|
): Promise<GuaribasUser> {
|
2019-08-22 19:36:23 -03:00
|
|
|
let user = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
userSystemId: userSystemId
|
|
|
|
}
|
2019-03-08 06:37:13 -03:00
|
|
|
});
|
2019-08-22 19:36:23 -03:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
user = GuaribasUser.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
user.instanceId = instanceId;
|
|
|
|
user.userSystemId = userSystemId;
|
|
|
|
user.userName = userName;
|
|
|
|
user.displayName = displayName;
|
|
|
|
user.email = userName;
|
|
|
|
user.defaultChannel = channelName;
|
2020-01-27 16:19:09 -03:00
|
|
|
return await user.save();
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2019-06-27 09:22:32 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrives a conversation reference from contact phone.
|
|
|
|
*/
|
|
|
|
public async getConversationReference(phone: string): Promise<ConversationReference> {
|
|
|
|
const options = { where: { phone: phone } };
|
|
|
|
const user = await GuaribasUser.findOne(options);
|
|
|
|
|
|
|
|
return JSON.parse(user.conversationReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a conversation reference from contact phone.
|
|
|
|
*/
|
|
|
|
public async updateConversationReference(phone: string, conversationReference: string) {
|
|
|
|
const options = { where: { phone: phone } };
|
|
|
|
const user = await GuaribasUser.findOne(options);
|
|
|
|
|
|
|
|
user.conversationReference = conversationReference;
|
|
|
|
await user.save();
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
public async updateUserInstance(userSystemId: string, instanceId: number): Promise<GuaribasUser> {
|
2019-08-22 19:36:23 -03:00
|
|
|
let user = await GuaribasUser.findOne({
|
2020-01-26 17:43:50 -03:00
|
|
|
where: {
|
2019-08-22 19:36:23 -03:00
|
|
|
userSystemId: userSystemId
|
|
|
|
}
|
|
|
|
});
|
2020-05-02 21:28:13 -03:00
|
|
|
user.instanceId = instanceId;
|
|
|
|
|
|
|
|
return await user.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async updateCurrentAgent(
|
|
|
|
userSystemId: string,
|
|
|
|
instanceId: number,
|
|
|
|
agentSystemId: string
|
|
|
|
): Promise<GuaribasUser> {
|
|
|
|
const user = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
userSystemId: userSystemId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (agentSystemId === null) {
|
|
|
|
const agent = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
userSystemId: user.agentSystemId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (agent !== null && agent !== undefined) {
|
2020-10-18 13:24:19 -03:00
|
|
|
agent.agentMode = 'bot';
|
2020-05-02 21:28:13 -03:00
|
|
|
agent.agentSystemId = null;
|
|
|
|
await agent.save();
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
user.agentMode = 'bot';
|
2020-05-02 21:28:13 -03:00
|
|
|
user.agentSystemId = null;
|
|
|
|
} else {
|
2020-10-18 13:24:19 -03:00
|
|
|
user.agentMode = 'human';
|
2020-05-02 21:28:13 -03:00
|
|
|
user.agentSystemId = agentSystemId;
|
|
|
|
const agent = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
userSystemId: agentSystemId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
agent.instanceId = user.instanceId;
|
2020-10-18 13:24:19 -03:00
|
|
|
agent.agentMode = 'self';
|
2020-05-02 21:28:13 -03:00
|
|
|
agent.agentSystemId = null;
|
|
|
|
await agent.save();
|
|
|
|
}
|
|
|
|
|
2019-08-22 19:36:23 -03:00
|
|
|
await user.save();
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:28:13 -03:00
|
|
|
public async isAgentSystemId(systemId: string): Promise<Boolean> {
|
|
|
|
let user = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
userSystemId: systemId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
throw `TRANSFER_TO phones must talk first to the bot before becoming an agent.`;
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
return user.agentMode === 'self';
|
2020-05-02 21:28:13 -03:00
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
public async assignHumanAgent(userSystemId: string, instanceId: number): Promise<string> {
|
2020-05-02 21:28:13 -03:00
|
|
|
let agentSystemId;
|
|
|
|
const list = process.env.TRANSFER_TO.split(';');
|
|
|
|
await CollectionUtil.asyncForEach(list, async item => {
|
2020-10-18 13:24:19 -03:00
|
|
|
if (
|
|
|
|
!(await this.isAgentSystemId(item)) &&
|
|
|
|
item !== undefined &&
|
|
|
|
agentSystemId === undefined &&
|
|
|
|
item !== userSystemId
|
|
|
|
) {
|
|
|
|
// TODO: Optimize loop.
|
2020-05-02 21:28:13 -03:00
|
|
|
agentSystemId = item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.updateCurrentAgent(userSystemId, instanceId, agentSystemId);
|
|
|
|
|
|
|
|
return agentSystemId;
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
public async getUserFromSystemId(systemId: string): Promise<GuaribasUser> {
|
2019-08-22 19:36:23 -03:00
|
|
|
return await GuaribasUser.findOne({
|
|
|
|
where: {
|
2020-05-02 21:28:13 -03:00
|
|
|
userSystemId: systemId
|
2019-08-22 19:36:23 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
public async getUserFromAgentSystemId(systemId: string): Promise<GuaribasUser> {
|
2020-05-02 21:28:13 -03:00
|
|
|
return await GuaribasUser.findOne({
|
|
|
|
where: {
|
2020-10-18 13:24:19 -03:00
|
|
|
agentSystemId: systemId
|
2020-05-02 21:28:13 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-08-22 19:36:23 -03:00
|
|
|
|
2020-10-18 13:24:19 -03:00
|
|
|
public async getAllUsers(instanceId: number): Promise<GuaribasUser[]> {
|
|
|
|
return await GuaribasUser.findAll({
|
|
|
|
where: {
|
|
|
|
instanceId: instanceId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|