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';
|
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'));
|
2018-04-21 02:59:30 -03:00
|
|
|
security.groups.forEach(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
|
|
|
});
|
2019-03-09 16:59:31 -03:00
|
|
|
groupDb.save().then(g1 => {
|
2018-04-21 02:59:30 -03:00
|
|
|
group.users.forEach(user => {
|
2018-11-12 12:20:44 -02:00
|
|
|
const userDb = GuaribasUser.build({
|
2018-04-21 02:59:30 -03:00
|
|
|
instanceId: instance.instanceId,
|
2019-03-09 16:59:31 -03:00
|
|
|
groupId: g1.groupId,
|
2018-04-21 02:59:30 -03:00
|
|
|
userName: user.userName
|
2018-11-12 12:20:44 -02:00
|
|
|
});
|
2019-03-09 16:59:31 -03:00
|
|
|
userDb.save().then(user2 => {
|
2018-11-12 12:20:44 -02:00
|
|
|
const userGroup = GuaribasUserGroup.build();
|
2019-03-09 16:59:31 -03:00
|
|
|
userGroup.groupId = g1.groupId;
|
|
|
|
userGroup.userId = user2.userId;
|
2018-11-12 12:20:44 -02:00
|
|
|
userGroup.save();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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,
|
2019-08-22 19:36:23 -03:00
|
|
|
currentBotId: string,
|
2018-04-21 02:59:30 -03:00
|
|
|
userName: string,
|
|
|
|
address: string,
|
|
|
|
channelName: string,
|
2019-08-22 19:36:23 -03:00
|
|
|
displayName: string,
|
|
|
|
phone: 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: {
|
|
|
|
instanceId: instanceId,
|
|
|
|
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.currentBotId = currentBotId;
|
|
|
|
user.userName = userName;
|
|
|
|
user.displayName = displayName;
|
|
|
|
user.internalAddress = address;
|
|
|
|
user.email = userName;
|
|
|
|
user.phone = phone;
|
|
|
|
user.defaultChannel = channelName;
|
|
|
|
user.save();
|
|
|
|
return user;
|
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();
|
|
|
|
}
|
|
|
|
|
2019-08-22 19:36:23 -03:00
|
|
|
public async updateCurrentBotId(
|
|
|
|
instanceId: number,
|
|
|
|
userSystemId: string,
|
|
|
|
currentBotId: string
|
|
|
|
): Promise<GuaribasUser> {
|
|
|
|
let user = await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
instanceId: instanceId,
|
|
|
|
userSystemId: userSystemId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
user.currentBotId = currentBotId;
|
|
|
|
await user.save();
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getUserFromPhone(
|
|
|
|
phone: string
|
|
|
|
): Promise<GuaribasUser> {
|
|
|
|
return await GuaribasUser.findOne({
|
|
|
|
where: {
|
|
|
|
phone: phone
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|