Skip to content

Commit

Permalink
fix(core.gbapp): Fix in Bot Analytics.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 4, 2020
1 parent b268882 commit ca2de56
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/analytics.gblib/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export class AnalyticsService {
public async createMessage(
instanceId: number,
conversation: GuaribasConversation,
user: GuaribasUser,
userId: number,
content: string
): Promise<GuaribasConversationMessage> {

const message = GuaribasConversationMessage.build();
message.content = content;
message.instanceId = instanceId;
message.userId = user.userId;
message.userId = userId;
message.conversationId = conversation.conversationId;

return await message.save();
Expand Down
40 changes: 22 additions & 18 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { GBServer } from '../../../src/app';
import { Readable } from 'stream'
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
import { SecService } from '../../security.gblib/services/SecService';
import { AnalyticsService } from '../../analytics.gblib/services/AnalyticsService';
const urlJoin = require('url-join');
const PasswordGenerator = require("strict-password-generator").default;
const Nexmo = require('nexmo');
Expand Down Expand Up @@ -593,26 +594,29 @@ export class GBConversationalService {
}

public async sendText(min, step, text) {

let sec = new SecService();
const member = step.context.activity.from;
const user = await sec.ensureUser(min.instance.instanceId, member.id,
member.name, "", "web", member.name);
const minBoot = GBServer.globals.minBoot as any;
text = await min.conversationalService.translate(
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
text,
user.locale ? user.locale : 'pt'
);
const user = await min.userProfile.get(step.context, {});
if (user) {
const minBoot = GBServer.globals.minBoot as any;
text = await min.conversationalService.translate(
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
text,
user.systemUser.locale ? user.systemUser.locale : 'pt'
);
const analytics = new AnalyticsService();

if (!isNaN(member.id)) {
await min.whatsAppDirectLine.sendToDevice(member.id, text);
}
else {
await step.context.sendActivity(text);
}
analytics.createMessage(min.instance.instanceId,
user.conversation, null,
text);

}
if (!isNaN(member.id)) {
await min.whatsAppDirectLine.sendToDevice(member.id, text);
}
else {
await step.context.sendActivity(text);
}

}
}
}
3 changes: 0 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ export class GBDeployer implements IGBDeployer {
instance.searchIndexer
);


const connectionString = GBDeployer.getConnectionStringFromInstance(instance);

const dsName = 'gb';
Expand All @@ -468,10 +467,8 @@ export class GBDeployer implements IGBDeployer {
} catch (err) {
GBLog.error(err);
throw err;

}


await search.createIndex(searchSchema, dsName);
}

Expand Down
18 changes: 9 additions & 9 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class GBMinService {
let activeMin;
if (process.env.WHATSAPP_WELCOME_DISABLED !== "true") {
let toSwitchMin = GBServer.globals.minInstances.filter(p => p.instance.botId.toLowerCase() === text.toLowerCase())[0];
if (!toSwitchMin){
if (!toSwitchMin) {
toSwitchMin = GBServer.globals.minInstances.filter(p => p.instance.activationCode.toLowerCase() === text.toLowerCase())[0];
}

Expand All @@ -162,7 +162,7 @@ export class GBMinService {
} else {
// User wants to switch bots.
if (toSwitchMin !== undefined) {

const instance = await this.core.loadInstanceByBotId(activeMin.botId);
await sec.updateUserInstance(id, instance.instanceId);

Expand Down Expand Up @@ -554,10 +554,9 @@ export class GBMinService {
user.subjects = [];
user.cb = undefined;


if (context.activity.membersAdded !== undefined) {
if (context.activity.from.id !== min.botId) {
let sec = new SecService();
const member = context.activity.membersAdded[0];
const member = context.activity.from;

const persistedUser = await sec.ensureUser(instance.instanceId, member.id,
member.name, "", "web", member.name);
Expand All @@ -566,7 +565,6 @@ export class GBMinService {

user.systemUser = persistedUser;
user.conversation = await analytics.createConversation(persistedUser);

}

await min.userProfile.set(step.context, user);
Expand Down Expand Up @@ -646,9 +644,11 @@ export class GBMinService {

const analytics = new AnalyticsService();
const user = await min.userProfile.get(context, {});
analytics.createMessage(min.instance.instanceId,
user.conversation, user.systemUser,
context.activity.text);
if (user) {
analytics.createMessage(min.instance.instanceId,
user.conversation, user.systemUser.userId,
context.activity.text);
}
}

// Checks for global exit kewywords cancelling any active dialogs.
Expand Down
1 change: 1 addition & 0 deletions packages/kb.gbapp/dialogs/AskDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class AskDialog extends IGBDialog {
user2.isAsking = false;
user2.lastQuestionId = resultsB.questionId;
await min.userProfile.set(step.context, user2);

// Informs user that a broader search will be used.
if (user2.subjects.length > 0) {
await min.conversationalService.sendText(min, step, Messages[locale].wider_answer);
Expand Down
13 changes: 2 additions & 11 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,7 @@ export class KBService implements IGBKBService {
return await this.importKbTabularFile(urlJoin(file.root, file.name), instance.instanceId, packageId);
}
})
files = await walkPromise(urlJoin(localPath, 'tabular'));

await CollectionUtil.asyncForEach(files, async file => {

if (file !== null && file.name.endsWith('.xlsx')) {
return await this.importKbTabularFile(urlJoin(file.root, file.name), instance.instanceId, packageId);
}
});


}

public async importSubjectFile(packageId: number, filename: string, instance: IGBInstance): Promise<any> {
Expand Down Expand Up @@ -595,7 +587,6 @@ export class KBService implements IGBKBService {
* @param localPath Path to the .gbkb folder.
*/
public async deployKb(core: IGBCoreService, deployer: GBDeployer, localPath: string, min: GBMinInstance) {
const packageType = Path.extname(localPath);
const packageName = Path.basename(localPath);
GBLog.info(`[GBDeployer] Opening package: ${localPath}`);

Expand All @@ -606,7 +597,7 @@ export class KBService implements IGBKBService {
await this.importKbPackage(localPath, p, instance);
deployer.mountGBKBAssets(packageName, localPath);

deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
GBLog.info(`[GBDeployer] Finished import of ${localPath}`);
}
}

0 comments on commit ca2de56

Please sign in to comment.