diff --git a/DATABASE-CHANGES.md b/DATABASE-CHANGES.md index 2afc9d7b1..0d685a06f 100644 --- a/DATABASE-CHANGES.md +++ b/DATABASE-CHANGES.md @@ -137,7 +137,12 @@ CREATE TABLE [dbo].[GuaribasSchedule] GO - -# 3.0.0 - ALTER TABLE dbo.GuaribasInstance ADD botKey nvarchar(64) NULL; + +# 2.3.9 + +GO + +ALTER TABLE dbo.GuaribasUser ADD + params nvarchar(4000) NULL +GO diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index e12675858..409b698e2 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -371,6 +371,8 @@ export class GBVMService extends GBService { } ]; + // Based on https://github.com/uweg/vbscript-to-typescript. + keywords[i++] = [/^\s*else(?!{)/gim, '}\nelse {']; keywords[i++] = [/^\s*select case +(.*)/gim, 'switch ($1) {']; @@ -378,6 +380,7 @@ export class GBVMService extends GBService { keywords[i++] = [/^\s*end select/gim, '}']; keywords[i++] = [/^\s*end function/gim, '}']; + keywords[i++] = [/^\s*function +(.*)\((.*)\)/gim, '$1 = ($2) => {\n']; keywords[i++] = [/^\s*for +(.*to.*)/gim, 'for ($1) {']; @@ -780,21 +783,21 @@ export class GBVMService extends GBService { keywords[i++] = [ /^\s*(\btransfer\b)(?=(?:[^"]|"[^"]*")*$)/gim, () => { - return `await dk.transferTo ({pid: pid, })`; + return `await dk.transferTo ({pid: pid})`; } ]; keywords[i++] = [ /^\s*(exit)/gim, () => { - return ``; + return `return;`; } ]; keywords[i++] = [ /^\s*(END)/gim, () => { - return ``; + return `return;`; } ]; diff --git a/packages/security.gbapp/models/index.ts b/packages/security.gbapp/models/index.ts index ce0b09383..03ec59f79 100644 --- a/packages/security.gbapp/models/index.ts +++ b/packages/security.gbapp/models/index.ts @@ -99,6 +99,9 @@ export class GuaribasUser extends Model { @Column(DataType.STRING(64)) declare hearOnDialog: string; + + @Column(DataType.STRING(4000)) + declare params: string; } /** diff --git a/packages/security.gbapp/services/SecService.ts b/packages/security.gbapp/services/SecService.ts index d6aa5dc92..7f2568fb2 100644 --- a/packages/security.gbapp/services/SecService.ts +++ b/packages/security.gbapp/services/SecService.ts @@ -1,10 +1,8 @@ -import Fs from 'fs'; -import urlJoin from 'url-join'; - +import { GBServer } from '../../../src/app.js'; import { ConversationReference } from 'botbuilder'; import { GBLog, GBMinInstance, GBService, IGBInstance } from 'botlib'; import { CollectionUtil } from 'pragmatismo-io-framework'; -import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models/index.js'; +import { GuaribasUser } from '../models/index.js'; import { FindOptions } from 'sequelize'; /** @@ -228,4 +226,57 @@ export class SecService extends GBService { } }); } -} + + + /** + * 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. + */ + public getParam (user: IGBInstance, 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(); + } +} \ No newline at end of file