Skip to content

Commit

Permalink
fix(core.gbapp): Language and translator improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Nov 17, 2020
1 parent 933729a commit f967d8f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
49 changes: 25 additions & 24 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,7 @@ export class GBConversationalService {
public async getLanguage(min: GBMinInstance, text: string): Promise<string> {
return await AzureText.getLocale(
min.instance.textAnalyticsKey ? min.instance.textAnalyticsKey : min.instance.textAnalyticsKey,
min.instance.textAnalyticsEndpoint ? min.instance.textAnalyticsEndpoint :
min.instance.textAnalyticsEndpoint,
min.instance.textAnalyticsEndpoint ? min.instance.textAnalyticsEndpoint : min.instance.textAnalyticsEndpoint,
text
);
}
Expand Down Expand Up @@ -612,14 +611,16 @@ export class GBConversationalService {
}

public async prompt(min: GBMinInstance, step: GBDialogStep, text: string) {
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 user = await min.userProfile.get(step.context, {});
const systemUser = user.systemUser;

if (text !== null) {
text = await min.conversationalService.translate(
min,
text,
user.locale ? user.locale : min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
systemUser.locale
? systemUser.locale
: min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);
GBLog.info(`Translated text(prompt): ${text}.`);
}
Expand All @@ -628,27 +629,27 @@ export class GBConversationalService {
}

public async sendText(min: GBMinInstance, 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);

if (user) {
text = await min.conversationalService.translate(
min,
text,
user.locale ? user.locale : min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);
GBLog.info(`Translated text(sendText): ${text}.`);
const user = await min.userProfile.get(step.context, {});
const systemUser = user.systemUser;

text = await min.conversationalService.translate(
min,
text,
systemUser.locale
? systemUser.locale
: min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);
GBLog.info(`Translated text(sendText): ${text}.`);

const analytics = new AnalyticsService();
const userProfile = await min.userProfile.get(step.context, {});
analytics.createMessage(min.instance.instanceId, userProfile.conversation, null, text);
const analytics = new AnalyticsService();

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);
}
if (!isNaN(member.id)) {
await min.whatsAppDirectLine.sendToDevice(member.id, text);
} else {
await step.context.sendActivity(text);
}
}

Expand Down
10 changes: 8 additions & 2 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,14 +787,20 @@ export class GBMinService {
const systemUser = user.systemUser;
if (systemUser.locale != locale) {
let sec = new SecService();
await sec.updateUserLocale(systemUser.userSystemId, locale);
await sec.updateUserLocale(systemUser.userId, locale);
}
}

// Translates the input text if is turned on instance params.

const originalText = context.text;
text = await min.conversationalService.translate(min, text, locale);
let contentLocale = min.core.getParam<string>(
min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);

text = await min.conversationalService.translate(min, text, contentLocale);
GBLog.info(`Translated text (processMessageActivity): ${text}.`);
context.activity.text = text;
context.activity.originalText = originalText;
Expand Down
2 changes: 1 addition & 1 deletion packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export class KBService implements IGBKBService {
min,
answer.content,
user.systemUser.locale
? user.locale
? user.systemUser.locale
: min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);
GBLog.info(`Translated text(playMarkdown): ${text}.`);
Expand Down
2 changes: 1 addition & 1 deletion packages/security.gbapp/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class GuaribasUser extends Model<GuaribasUser> {
@Column public email: string;

@Column(DataType.STRING(5))
@Column public locale: string;
public locale: string;

@ForeignKey(() => GuaribasInstance)
@Column
Expand Down
8 changes: 2 additions & 6 deletions packages/security.gbapp/services/SecService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { CollectionUtil } from 'pragmatismo-io-framework';
* Security service layer.
*/
export class SecService extends GBService {

public async importSecurityFile(localPath: string, instance: IGBInstance) {
const security = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'security.json'), 'utf8'));
await CollectionUtil.asyncForEach(security.groups, async group => {
Expand Down Expand Up @@ -82,14 +81,13 @@ export class SecService extends GBService {
await user.save();
}

public async updateUserLocale(userSystemId: number, locale: any) : Promise<GuaribasUser> {
public async updateUserLocale(userId: number, locale: any): Promise<GuaribasUser> {
let user = await GuaribasUser.findOne({
where: {
userSystemId: userSystemId
userId: userId
}
});
user.locale = locale;

return await user.save();
}

Expand Down Expand Up @@ -183,8 +181,6 @@ export class SecService extends GBService {
return agentSystemId;
}



public async getUserFromSystemId(systemId: string): Promise<GuaribasUser> {
return await GuaribasUser.findOne({
where: {
Expand Down

0 comments on commit f967d8f

Please sign in to comment.