botserver/packages/security.gbapp/services/SecService.ts

282 lines
7.5 KiB
TypeScript
Raw Normal View History

import { GBServer } from '../../../src/app.js';
import { ConversationReference } from 'botbuilder';
2022-04-26 15:13:19 -03:00
import { GBLog, GBMinInstance, GBService, IGBInstance } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework';
import { GuaribasUser } from '../models/index.js';
import { FindOptions } from 'sequelize';
2018-04-21 02:59:30 -03:00
/**
* Security service layer.
*/
2018-04-21 02:59:30 -03:00
export class SecService extends GBService {
2022-11-19 23:34:58 -03:00
public async ensureUser (
2018-04-21 02:59:30 -03:00
instanceId: number,
userSystemId: string,
userName: string,
address: string,
channelName: string,
displayName: string,
email: string
2018-09-09 14:39:37 -03:00
): Promise<GuaribasUser> {
let user = await GuaribasUser.findOne({
where: {
userSystemId: userSystemId
}
});
if (!user) {
user = GuaribasUser.build();
}
user.instanceId = instanceId;
user.userSystemId = userSystemId;
user.userName = userName;
user.displayName = displayName;
user.email = email;
user.defaultChannel = channelName;
2020-12-31 15:36:19 -03:00
return await user.save();
2018-04-21 02:59:30 -03:00
}
/**
* Retrives a conversation reference from contact phone.
*/
2022-11-19 23:34:58 -03:00
public async getConversationReference (phone: string): Promise<ConversationReference> {
const options = <FindOptions>{ rejectOnEmpty: true, where: { phone: phone } };
const user = await GuaribasUser.findOne(options);
return JSON.parse(user.conversationReference);
}
/**
* Updates a conversation reference from contact phone.
*/
2022-11-19 23:34:58 -03:00
public async updateConversationReference (phone: string, conversationReference: string) {
2022-01-03 13:11:21 -03:00
const options = <FindOptions>{ where: { phone: phone } };
const user = await GuaribasUser.findOne(options);
user.conversationReference = conversationReference;
await user.save();
}
2022-11-19 23:34:58 -03:00
public async updateConversationReferenceById (userId: number, conversationReference: string) {
2022-01-03 13:11:21 -03:00
const options = <FindOptions>{ where: { userId: userId } };
const user = await GuaribasUser.findOne(options);
user.conversationReference = conversationReference;
await user.save();
}
2022-11-19 23:34:58 -03:00
public async updateUserLocale (userId: number, locale: any): Promise<GuaribasUser> {
2020-12-31 15:36:19 -03:00
const user = await GuaribasUser.findOne({
where: {
userId: userId
}
});
user.locale = locale;
2020-12-31 15:36:19 -03:00
return await user.save();
}
2022-11-19 23:34:58 -03:00
public async updateUserHearOnDialog (userId: number, dialogName: string): Promise<GuaribasUser> {
2020-12-31 15:36:19 -03:00
const user = await GuaribasUser.findOne({
where: {
userId: userId
}
});
user.hearOnDialog = dialogName;
2020-12-31 15:36:19 -03:00
return await user.save();
}
2022-11-19 23:34:58 -03:00
public async updateUserInstance (userSystemId: string, instanceId: number): Promise<GuaribasUser> {
2020-12-31 15:36:19 -03:00
const user = await GuaribasUser.findOne({
where: {
userSystemId: userSystemId
}
});
user.instanceId = instanceId;
return await user.save();
}
/**
* Finds and update user agent information to a next available person.
*/
2022-11-19 23:34:58 -03:00
public async updateHumanAgent (
userSystemId: string,
instanceId: number,
agentSystemId: string
): Promise<GuaribasUser> {
2022-11-19 23:34:58 -03:00
const user = await GuaribasUser.findOne({
where: {
userSystemId: userSystemId,
instanceId: instanceId
}
});
if (agentSystemId === null && user.agentSystemId !== undefined) {
const agent = await GuaribasUser.findOne({
where: {
userSystemId: user.agentSystemId
}
});
if (agent !== null && agent !== undefined) {
agent.agentMode = 'bot';
agent.agentSystemId = null;
await agent.save();
}
user.agentMode = 'bot';
user.agentSystemId = null;
} else {
user.agentMode = 'human';
user.agentSystemId = agentSystemId;
const agent = await GuaribasUser.findOne({
where: {
userSystemId: agentSystemId
}
});
agent.instanceId = user.instanceId;
agent.agentMode = 'self';
agent.agentSystemId = null;
await agent.save();
}
await user.save();
2020-12-31 15:36:19 -03:00
return user;
}
2022-11-19 23:34:58 -03:00
public async isAgentSystemId (systemId: string): Promise<Boolean> {
2020-12-31 15:36:19 -03:00
const user = await GuaribasUser.findOne({
where: {
userSystemId: systemId
}
});
if (user === null) {
2020-12-31 15:36:19 -03:00
throw new Error(`TRANSFER_TO phones must talk first to the bot before becoming an agent.`);
}
return user.agentMode === 'self';
}
2022-11-19 23:34:58 -03:00
public async assignHumanAgent (
min: GBMinInstance,
userSystemId: string,
agentSystemId: string = null
): Promise<string> {
if (!agentSystemId) {
2022-11-19 23:34:58 -03:00
let list = min.core.getParam<string>(min.instance, 'Transfer To', process.env.TRANSFER_TO);
if (list) {
2022-11-19 23:34:58 -03:00
list = list.split(';');
}
await CollectionUtil.asyncForEach(list, async item => {
if (
item !== undefined &&
agentSystemId === undefined &&
2022-11-19 23:34:58 -03:00
item !== userSystemId &&
!(await this.isAgentSystemId(item))
) {
agentSystemId = item;
}
});
}
GBLog.info(`Selected agentId: ${agentSystemId}`);
2022-04-26 15:13:19 -03:00
await this.updateHumanAgent(userSystemId, min.instance.instanceId, agentSystemId);
GBLog.info(`Updated agentId to: ${agentSystemId}`);
return agentSystemId;
}
public async getUserFromId (instanceId: number, userId: string): Promise<GuaribasUser> {
return await GuaribasUser.findOne({
where: {
instanceId: instanceId,
userId: userId
}
});
}
2022-11-19 23:34:58 -03:00
public async getUserFromSystemId (systemId: string): Promise<GuaribasUser> {
return await GuaribasUser.findOne({
where: {
userSystemId: systemId
}
});
}
2022-11-19 23:34:58 -03:00
public async getUserFromAgentSystemId (systemId: string): Promise<GuaribasUser> {
return await GuaribasUser.findOne({
where: {
agentSystemId: systemId
}
});
}
2022-11-19 23:34:58 -03:00
public async getAllUsers (instanceId: number): Promise<GuaribasUser[]> {
return await GuaribasUser.findAll({
where: {
instanceId: instanceId
}
});
}
/**
* Get a dynamic param from user. Dynamic params are defined in .gbdialog SET
* variables and other semantics during conversation.
*
* @param name Name of param to get from instance.
* @param defaultValue Value returned when no param is defined.
*/
2023-01-31 14:07:23 -03:00
public getParam<T> (user: GuaribasUser, name: string, defaultValue?: T): any {
let value = null;
if (user.params) {
const params = JSON.parse(user.params);
value = params ? params[name] : defaultValue;
}
if (typeof defaultValue === 'boolean') {
return new Boolean(value ? value.toString().toLowerCase() === 'true' : defaultValue);
}
if (typeof defaultValue === 'string') {
return value ? value : defaultValue;
}
if (typeof defaultValue === 'number') {
return new Number(value ? value : defaultValue ? defaultValue : 0);
}
if (user['dataValues'] && !value) {
value = user['dataValues'][name];
if (value === null) {
switch(name)
{
case 'language':
value = 'en';
break;
}
}
}
return value;
}
/**
* Saves user instance object to the storage handling
* multi-column JSON based store 'params' field.
*/
public async setParam (userId: number, name: string, value:any) {
const options = { where: {} };
options.where = { botId: userId };
let user = await GuaribasUser.findOne(options);
// tslint:disable-next-line:prefer-object-spread
let obj = JSON.parse(user.params);
obj['name'] = value;
user.params = JSON.stringify(obj);
return await user.save();
}
}