From e4e331730d6a7d3c22b70298c7d156345d7862a0 Mon Sep 17 00:00:00 2001 From: Jorge Ramos Date: Mon, 4 Jun 2018 08:03:23 -0300 Subject: [PATCH] - FIX: Packages updated. - NEW: DATABASE_SYNC_ALTER environment parameter. - NEW: DATABASE_SYNC_FORCE environment parameter. - NEW: Define constraint names in MSSQL. --- VERSION.md | 7 + deploy/admin.gbapp/dialogs/AdminDialog.ts | 2 +- deploy/admin.gbapp/index.ts | 17 +- deploy/analytics.gblib/index.ts | 16 +- deploy/analytics.gblib/models/index.ts | 20 +- deploy/console.gblib/index.ts | 8 +- deploy/core.gbapp/index.ts | 10 +- deploy/core.gbapp/models/GBModel.ts | 218 ++++++++++-------- deploy/core.gbapp/services/GBConfigService.ts | 14 +- deploy/core.gbapp/services/GBCoreService.ts | 128 ++++++++-- deploy/core.gbapp/services/GBDeployer.ts | 2 +- deploy/core.gbapp/services/GBImporter.ts | 4 +- deploy/core.gbapp/services/GBMinService.ts | 24 +- deploy/customer-satisfaction.gbapp/index.ts | 11 +- .../models/index.ts | 4 +- deploy/kb.gbapp/index.ts | 17 +- deploy/kb.gbapp/models/index.ts | 34 ++- deploy/kb.gbapp/services/KBService.ts | 4 +- deploy/security.gblib/index.ts | 11 +- deploy/security.gblib/models/index.ts | 16 +- deploy/whatsapp.gblib/index.ts | 51 ++-- package-lock.json | 8 +- package.json | 4 +- src/app.ts | 4 +- 24 files changed, 368 insertions(+), 266 deletions(-) diff --git a/VERSION.md b/VERSION.md index 12d91cd4..9bdb1989 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1,5 +1,12 @@ # Release History +## Version 0.0.30 + +- FIX: Packages updated. +- NEW: DATABASE_SYNC_ALTER environment parameter. +- NEW: DATABASE_SYNC_FORCE environment parameter. +- NEW: Define constraint names in MSSQL. + ## Version 0.0.29 - NEW: Added STT and TTS capabilities to default.gbui. diff --git a/deploy/admin.gbapp/dialogs/AdminDialog.ts b/deploy/admin.gbapp/dialogs/AdminDialog.ts index c6d94414..a8c8e8c8 100644 --- a/deploy/admin.gbapp/dialogs/AdminDialog.ts +++ b/deploy/admin.gbapp/dialogs/AdminDialog.ts @@ -32,8 +32,8 @@ "use strict"; - const UrlJoin = require("url-join"); + import { AzureSearch } from "pragmatismo-io-framework"; import { Prompts, Session, UniversalBot } from 'botbuilder'; import { GBMinInstance } from "botlib"; diff --git a/deploy/admin.gbapp/index.ts b/deploy/admin.gbapp/index.ts index 94c2890e..9f9345b3 100644 --- a/deploy/admin.gbapp/index.ts +++ b/deploy/admin.gbapp/index.ts @@ -30,30 +30,31 @@ | | \*****************************************************************************/ -"use strict"; +'use strict'; + +const UrlJoin = require('url-join'); -const UrlJoin = require("url-join"); import { AdminDialog } from './dialogs/AdminDialog'; -import { GBMinInstance, IGBPackage } from "botlib"; +import { GBMinInstance, IGBPackage, IGBCoreService } from 'botlib'; import { Session } from 'botbuilder'; import { Sequelize } from 'sequelize-typescript'; -import { IGBCoreService } from 'botlib'; export class GBAdminPackage implements IGBPackage { - sysPackages: IGBPackage[] = null; + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { } - unloadPackage(core: IGBCoreService): void { + unloadPackage(core: IGBCoreService): void { } + loadBot(min: GBMinInstance): void { AdminDialog.setup(min.bot, min); } + unloadBot(min: GBMinInstance): void { - } - onNewSession(min: GBMinInstance, session: Session): void { + onNewSession(min: GBMinInstance, session: Session): void { } } diff --git a/deploy/analytics.gblib/index.ts b/deploy/analytics.gblib/index.ts index c58afd26..b97c06e2 100644 --- a/deploy/analytics.gblib/index.ts +++ b/deploy/analytics.gblib/index.ts @@ -34,29 +34,25 @@ const UrlJoin = require("url-join"); - - import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib"; import { Session } from 'botbuilder'; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; export class GBAnalyticsPackage implements IGBPackage { - sysPackages: IGBPackage[] = null; - + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { - } + unloadPackage(core: IGBCoreService): void { - } + loadBot(min: GBMinInstance): void { - } + unloadBot(min: GBMinInstance): void { - } + onNewSession(min: GBMinInstance, session: Session): void { - } } diff --git a/deploy/analytics.gblib/models/index.ts b/deploy/analytics.gblib/models/index.ts index a8ad2469..c410b077 100644 --- a/deploy/analytics.gblib/models/index.ts +++ b/deploy/analytics.gblib/models/index.ts @@ -30,17 +30,17 @@ | | \*****************************************************************************/ - "use strict"; import { - Sequelize, DataTypes, DataTypeUUIDv4, DataTypeDate, DataTypeDecimal } from "sequelize"; + import { + Sequelize, Table, Column, Model, @@ -56,13 +56,14 @@ import { PrimaryKey, AutoIncrement } from "sequelize-typescript"; + import { GuaribasSubject } from "../../kb.gbapp/models"; import { GuaribasUser } from "../../security.gblib/models"; import { GuaribasChannel, GuaribasInstance } from "../../core.gbapp/models/GBModel"; - @Table export class GuaribasConversation extends Model { + @PrimaryKey @AutoIncrement @Column @@ -81,15 +82,13 @@ export class GuaribasConversation extends Model { @Column rateDate: Date; - @Column({ - type: DataType.FLOAT - }) + @Column(DataType.FLOAT) @Column rate: number; @Column @CreatedAt - creationDate: Date; + createdAt: Date; @Column text: string; @@ -106,6 +105,7 @@ export class GuaribasConversation extends Model { @Table export class GuaribasConversationMessage extends Model { + @PrimaryKey @AutoIncrement @Column @@ -115,16 +115,16 @@ export class GuaribasConversationMessage extends Model GuaribasConversation) @Column diff --git a/deploy/console.gblib/index.ts b/deploy/console.gblib/index.ts index 871b6663..27b821c6 100644 --- a/deploy/console.gblib/index.ts +++ b/deploy/console.gblib/index.ts @@ -34,13 +34,11 @@ const UrlJoin = require("url-join"); - import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib"; import { Session } from 'botbuilder'; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { ConsoleDirectLine } from "./services/ConsoleDirectLine"; - export class GBConsolePackage implements IGBPackage { sysPackages: IGBPackage[] = null; channel: ConsoleDirectLine; @@ -49,7 +47,6 @@ export class GBConsolePackage implements IGBPackage { } unloadPackage(core: IGBCoreService): void { - } loadBot(min: GBMinInstance): void { @@ -57,9 +54,8 @@ export class GBConsolePackage implements IGBPackage { } unloadBot(min: GBMinInstance): void { - } - onNewSession(min: GBMinInstance, session: Session): void { + onNewSession(min: GBMinInstance, session: Session): void { } } diff --git a/deploy/core.gbapp/index.ts b/deploy/core.gbapp/index.ts index c4f4c7c1..eadeebd0 100644 --- a/deploy/core.gbapp/index.ts +++ b/deploy/core.gbapp/index.ts @@ -38,13 +38,13 @@ import { GBMinInstance, IGBPackage } from "botlib"; import { Session } from 'botbuilder'; import { WelcomeDialog } from "./dialogs/WelcomeDialog"; import { WhoAmIDialog } from "./dialogs/WhoAmIDialog"; -import { IGBCoreService} from "botlib"; -import { Sequelize } from "sequelize-typescript"; +import { IGBCoreService } from "botlib"; +import { Sequelize } from 'sequelize-typescript'; import { GuaribasInstance, GuaribasException, GuaribasPackage, GuaribasChannel } from "./models/GBModel"; export class GBCorePackage implements IGBPackage { sysPackages: IGBPackage[] = null; - + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { core.sequelize.addModels([ GuaribasInstance, @@ -55,7 +55,6 @@ export class GBCorePackage implements IGBPackage { } unloadPackage(core: IGBCoreService): void { - } loadBot(min: GBMinInstance): void { @@ -64,9 +63,8 @@ export class GBCorePackage implements IGBPackage { } unloadBot(min: GBMinInstance): void { - } + onNewSession(min: GBMinInstance, session: Session): void { - } } diff --git a/deploy/core.gbapp/models/GBModel.ts b/deploy/core.gbapp/models/GBModel.ts index a32feb19..20300077 100644 --- a/deploy/core.gbapp/models/GBModel.ts +++ b/deploy/core.gbapp/models/GBModel.ts @@ -33,164 +33,180 @@ "use strict"; import { - Sequelize, - DataTypes, - DataTypeUUIDv4, - DataTypeDate, - DataTypeDecimal + DataTypes, + DataTypeUUIDv4, + DataTypeDate, + DataTypeDecimal } from "sequelize"; -import { - Table, - Column, - Model, - HasMany, - BelongsTo, - BelongsToMany, - Length, - ForeignKey, - CreatedAt, - UpdatedAt, - DataType, - IsUUID, - PrimaryKey, - AutoIncrement -} from "sequelize-typescript"; -import { IGBInstance } from "botlib"; +import { + Sequelize, + Table, + Column, + Model, + HasMany, + BelongsTo, + BelongsToMany, + Length, + ForeignKey, + CreatedAt, + UpdatedAt, + DataType, + IsUUID, + PrimaryKey, + AutoIncrement +} from "sequelize-typescript"; + +import { IGBInstance } from "botlib"; @Table export class GuaribasInstance extends Model implements IGBInstance { - @Column - whoAmIVideo: string; + @PrimaryKey + @AutoIncrement + @Column + instanceId: number; - @PrimaryKey - @AutoIncrement - @Column - instanceId: number; + @Column + whoAmIVideo: string; - @Column botId: string; + @Column botId: string; - @Column title: string; + @Column title: string; - @Column description: string; + @Column description: string; - @Column version: string; + @Column version: string; - @Column enabledAdmin: boolean; + @Column enabledAdmin: boolean; - /* Services section on bot.json */ + /* Services section on bot.json */ - @Column engineName: string; + @Column engineName: string; - @Column marketplaceId: string; + @Column marketplaceId: string; - @Column textAnalyticsKey: string; + @Column textAnalyticsKey: string; - @Column marketplacePassword: string; + @Column marketplacePassword: string; - @Column webchatKey: string; + @Column webchatKey: string; - @Column whatsappBotKey: string; + @Column whatsappBotKey: string; - @Column whatsappServiceKey: string; + @Column whatsappServiceKey: string; - @Column whatsappServiceNumber: string; + @Column whatsappServiceNumber: string; - @Column whatsappServiceUrl: string; + @Column whatsappServiceUrl: string; - @Column whatsappServiceWebhookUrl: string; + @Column whatsappServiceWebhookUrl: string; - @Column speechKey: string; + @Column speechKey: string; - @Column spellcheckerKey: string; + @Column spellcheckerKey: string; - @Column theme: string; + @Column theme: string; - @Column ui: string; + @Column ui: string; - @Column kb: string; + @Column kb: string; - @Column - @Column({ type: DataType.STRING(512) }) - nlpServerUrl: string; + @Column(DataType.STRING(512)) nlpServerUrl: string; - @Column searchHost: string; + @Column searchHost: string; - @Column searchKey: string; + @Column searchKey: string; - @Column searchIndex: string; + @Column searchIndex: string; - @Column searchIndexer: string; + @Column searchIndexer: string; - /* Settings section of bot.json */ + /* Settings section of bot.json */ - @Column({ - type: DataType.FLOAT - }) - nlpVsSearch: number; + @Column(DataType.FLOAT) nlpVsSearch: number; - @Column({ - type: DataType.FLOAT - }) - searchScore: number; + @Column(DataType.FLOAT) searchScore: number; - @Column({ - type: DataType.FLOAT - }) - nlpScore: number; + @Column(DataType.FLOAT) nlpScore: number; - @Column - @CreatedAt - creationDate: Date; + @Column + @CreatedAt + createdAt: Date; - @Column - @UpdatedAt - updatedOn: Date; + @Column + @UpdatedAt + updatedAt: Date; } @Table export class GuaribasPackage extends Model { - @PrimaryKey - @AutoIncrement - @Column - packageId: number; + @PrimaryKey + @AutoIncrement + @Column + packageId: number; - @Column - packageName: string; + @Column + packageName: string; - @ForeignKey(() => GuaribasInstance) - @Column - instanceId: number; + @ForeignKey(() => GuaribasInstance) + @Column + instanceId: number; + + @BelongsTo(() => GuaribasInstance) + instance: GuaribasInstance; + + @Column + @CreatedAt + createdAt: Date; + + @Column + @UpdatedAt + updatedAt: Date; } @Table export class GuaribasChannel extends Model { - @PrimaryKey - @AutoIncrement - @Column - channelId: number; - @Column title: string; + @PrimaryKey + @AutoIncrement + @Column + channelId: number; + + @Column title: string; + + @Column + @CreatedAt + createdAt: Date; + + @Column + @UpdatedAt + updatedAt: Date; } @Table export class GuaribasException extends Model { - @PrimaryKey - @AutoIncrement - @Column - exceptionId: number; + @PrimaryKey + @AutoIncrement + @Column + exceptionId: number; - @Column message: string; + @Column message: string; - @ForeignKey(() => GuaribasInstance) - @Column - instanceId: number; + @ForeignKey(() => GuaribasInstance) + @Column + instanceId: number; - @Column - @CreatedAt - creationDate: Date; + @BelongsTo(() => GuaribasInstance) + instance: GuaribasInstance; + + @Column + @CreatedAt + createdAt: Date; + + @Column + @UpdatedAt + updatedAt: Date; } - diff --git a/deploy/core.gbapp/services/GBConfigService.ts b/deploy/core.gbapp/services/GBConfigService.ts index f1f16e7e..7088aad5 100644 --- a/deploy/core.gbapp/services/GBConfigService.ts +++ b/deploy/core.gbapp/services/GBConfigService.ts @@ -49,7 +49,7 @@ export class GBConfigService { } } - static get(key: string): any { + static get(key: string): string | undefined { let value = process.env["container:" + key]; if (!value) { @@ -71,9 +71,19 @@ export class GBConfigService { break; case "DATABASE_SYNC": + case "DATABASE_SYNC_ALTER": + case "DATABASE_SYNC_FORCE": value = "false"; break; - + + case "DATABASE_LOGGING": + value = "false"; + break; + + case "DATABASE_ENCRYPT": + value = "true"; + break; + default: logger.trace( `Guaribas General Error: Invalid key on .env file: '${key}'` diff --git a/deploy/core.gbapp/services/GBCoreService.ts b/deploy/core.gbapp/services/GBCoreService.ts index c6f87b6e..d452e74b 100644 --- a/deploy/core.gbapp/services/GBCoreService.ts +++ b/deploy/core.gbapp/services/GBCoreService.ts @@ -41,7 +41,7 @@ const UrlJoin = require("url-join"); const Walk = require("fs-walk"); const logger = require("../../../src/logger"); -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { Promise } from "bluebird"; import { GBConfigService } from "./GBConfigService"; import { DataTypeUUIDv1 } from "sequelize"; @@ -56,9 +56,13 @@ export class GBCoreService implements IGBCoreService { public sequelize: Sequelize; + private queryGenerator: any; + private createTableQuery: (tableName, attributes, options) => string; + private changeColumnQuery: (tableName, attributes) => string; + /** Dialect used. Tested: mssql and sqlite. */ - dialect: string; + private dialect: string; constructor() { this.dialect = GBConfigService.get("DATABASE_DIALECT"); @@ -67,11 +71,11 @@ export class GBCoreService implements IGBCoreService { /** Get config and connect to storage. */ initDatabase(cb) { - let host = ""; - let database = ""; - let username = ""; - let password = ""; - let storage = ""; + let host: string | undefined; + let database: string | undefined; + let username: string | undefined; + let password: string | undefined; + let storage: string | undefined; if (this.dialect === "mssql") { host = GBConfigService.get("DATABASE_HOST"); @@ -82,14 +86,11 @@ export class GBCoreService implements IGBCoreService { storage = GBConfigService.get("DATABASE_STORAGE"); } - let value = GBConfigService.get("DATABASE_LOGGING"); - let logging: boolean | Function = false; + let logging = (GBConfigService.get("DATABASE_LOGGING") === "true") + ? (str: string) => { logger.trace(str); } + : false; - if (value && value == "1") { - logging = (str) => { - logger.trace(str); - }; - } + let encrypt = (GBConfigService.get("DATABASE_ENCRYPT") === "true"); this.sequelize = new Sequelize({ host: host, @@ -100,9 +101,8 @@ export class GBCoreService implements IGBCoreService { operatorsAliases: false, dialect: this.dialect, storage: storage, - dialectOptions: { - encrypt: true + encrypt: encrypt }, pool: { max: 32, @@ -110,26 +110,106 @@ export class GBCoreService implements IGBCoreService { idle: 40000, evict: 40000, acquire: 40000 - } + }, }); - cb(); + + if (this.dialect === "mssql") { + this.queryGenerator = this.sequelize.getQueryInterface().QueryGenerator; + this.createTableQuery = this.queryGenerator.createTableQuery; + this.queryGenerator.createTableQuery = (tableName, attributes, options) => + this.createTableQueryOverride(tableName, attributes, options); + this.changeColumnQuery = this.queryGenerator.changeColumnQuery; + this.queryGenerator.changeColumnQuery = (tableName, attributes) => + this.changeColumnQueryOverride(tableName, attributes); + } + + setImmediate(cb); + } + + private createTableQueryOverride(tableName, attributes, options): string { + let sql: string = this.createTableQuery.apply(this.queryGenerator, + [tableName, attributes, options]); + // let sql: string = '' + + // 'IF OBJECT_ID(\'[UserGroup]\', \'U\') IS NULL\n' + + // 'CREATE TABLE [UserGroup] (\n' + + // ' [id] INTEGER NOT NULL IDENTITY(1,1),\n' + + // ' [userId] INTEGER NULL,\n' + + // ' [groupId] INTEGER NULL,\n' + + // ' [instanceId] INTEGER NULL,\n' + + // ' PRIMARY KEY ([id1], [id2]),\n' + + // ' FOREIGN KEY ([userId1], [userId2], [userId3]) REFERENCES [User] ([userId1], [userId2], [userId3]) ON DELETE NO ACTION,\n' + + // ' FOREIGN KEY ([groupId1], [groupId2]) REFERENCES [Group] ([groupId1], [groupId1]) ON DELETE NO ACTION,\n' + + // ' FOREIGN KEY ([instanceId]) REFERENCES [Instance] ([instanceId]) ON DELETE NO ACTION);'; + const re1 = /CREATE\s+TABLE\s+\[([^\]]*)\]/; + const matches = re1.exec(sql); + if (matches) { + const table = matches[1]; + const re2 = /PRIMARY\s+KEY\s+\(\[[^\]]*\](?:,\s*\[[^\]]*\])*\)/; + sql = sql.replace(re2, (match: string, ...args: any[]): string => { + return 'CONSTRAINT [' + table + '_pk] ' + match; + }); + const re3 = /FOREIGN\s+KEY\s+\((\[[^\]]*\](?:,\s*\[[^\]]*\])*)\)/g; + const re4 = /\[([^\]]*)\]/g; + sql = sql.replace(re3, (match: string, ...args: any[]): string => { + const fkcols = args[0]; + let fkname = table; + let matches = re4.exec(fkcols); + while (matches != null) { + fkname += '_' + matches[1]; + matches = re4.exec(fkcols); + } + return 'CONSTRAINT [' + fkname + '_fk] FOREIGN KEY (' + fkcols + ')'; + }); + } + return sql; + } + + private changeColumnQueryOverride(tableName, attributes): string { + let sql: string = this.changeColumnQuery.apply(this.queryGenerator, + [tableName, attributes]); + // let sql = '' + + // 'ALTER TABLE [UserGroup]\n' + + // ' ADD CONSTRAINT [invalid1] FOREIGN KEY ([userId1], [userId2], [userId3]) REFERENCES [User] ([userId1], [userId2], [userId3]) ON DELETE NO ACTION,\n' + + // ' CONSTRAINT [invalid2] FOREIGN KEY ([groupId1], [groupId2]) REFERENCES [Group] ([groupId1], [groupId2]) ON DELETE NO ACTION, \n' + + // ' CONSTRAINT [invalid3] FOREIGN KEY ([instanceId1]) REFERENCES [Instance] ([instanceId1]) ON DELETE NO ACTION;\n'; + const re1 = /ALTER\s+TABLE\s+\[([^\]]*)\]/; + const matches = re1.exec(sql); + if (matches) { + const table = matches[1]; + const re2 = /(ADD\s+)?CONSTRAINT\s+\[([^\]]*)\]\s+FOREIGN\s+KEY\s+\((\[[^\]]*\](?:,\s*\[[^\]]*\])*)\)/g; + const re3 = /\[([^\]]*)\]/g; + sql = sql.replace(re2, (match: string, ...args: any[]): string => { + const fkcols = args[2]; + let fkname = table; + let matches = re3.exec(fkcols); + while (matches != null) { + fkname += '_' + matches[1]; + matches = re3.exec(fkcols); + } + return (args[0] ? args[0] : '') + 'CONSTRAINT [' + fkname + '_fk] FOREIGN KEY (' + fkcols + ')'; + }); + } + return sql; } syncDatabaseStructure(cb) { if (GBConfigService.get("DATABASE_SYNC") === "true") { + const alter = (GBConfigService.get("DATABASE_SYNC_ALTER") === "true"); + const force = (GBConfigService.get("DATABASE_SYNC_FORCE") === "true"); logger.trace("Syncing database..."); - this.sequelize.sync().then(value => { + this.sequelize.sync({ + alter: alter, + force: force + }).then(value => { logger.trace("Database synced."); cb(); - }); - } - else { + }, err => logger.error(err)); + } else { logger.trace("Database synchronization is disabled."); cb(); } } - /** * Loads all items to start several listeners. * @param cb Instances loaded or error info. @@ -149,8 +229,8 @@ export class GBCoreService implements IGBCoreService { if (reason.message.indexOf("no such table: GuaribasInstance") != -1) { cb([], null); } else { - cb(null, reason); logger.trace(`GuaribasServiceError: ${reason}`); + cb(null, reason); } }); } diff --git a/deploy/core.gbapp/services/GBDeployer.ts b/deploy/core.gbapp/services/GBDeployer.ts index cb613a2a..8d803a21 100644 --- a/deploy/core.gbapp/services/GBDeployer.ts +++ b/deploy/core.gbapp/services/GBDeployer.ts @@ -46,7 +46,7 @@ import { KBService } from './../../kb.gbapp/services/KBService'; import { GBImporter } from "./GBImporter"; import { GBCoreService } from "./GBCoreService"; import { GBServiceCallback, IGBCoreService, IGBInstance } from "botlib"; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { Promise } from "bluebird"; import { GBConfigService } from "./GBConfigService"; import { DataTypeUUIDv1 } from "sequelize"; diff --git a/deploy/core.gbapp/services/GBImporter.ts b/deploy/core.gbapp/services/GBImporter.ts index 1a4fa736..7a64d702 100644 --- a/deploy/core.gbapp/services/GBImporter.ts +++ b/deploy/core.gbapp/services/GBImporter.ts @@ -41,7 +41,7 @@ const Walk = require("fs-walk"); const logger = require("../../../src/logger"); import { KBService } from './../../kb.gbapp/services/KBService'; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { Promise } from "bluebird"; import Fs = require("fs"); import Path = require("path"); @@ -96,7 +96,7 @@ export class GBImporter { packageJson = Object.assign(packageJson, settings, servicesJson); GuaribasInstance.create(packageJson).then((instance: IGBInstance) => { - + // PACKAGE: security.json loading let service = new SecService(); service.importSecurityFile(localPath, instance); diff --git a/deploy/core.gbapp/services/GBMinService.ts b/deploy/core.gbapp/services/GBMinService.ts index 3fb96975..ee4b7e47 100644 --- a/deploy/core.gbapp/services/GBMinService.ts +++ b/deploy/core.gbapp/services/GBMinService.ts @@ -104,6 +104,13 @@ export class GBMinService { _this_.core.loadInstances((instances: IGBInstance[], err) => { + // We are running empty ! + + if (!instances) { + logger.error('The bot server is running empty.'); + return; + } + // Gets the authorization key for each instance from Bot Service. instances.forEach(instance => { @@ -350,20 +357,16 @@ export class GBMinService { dirs.forEach(element => { if (element.startsWith('.')) { logger.trace(`Ignoring ${element}...`); - } - else { + } else { if (element.endsWith('.gbot')) { botPackages.push(element); - } - else if (element.endsWith('.gbapp')) { + } else if (element.endsWith('.gbapp')) { gbappPackages.push(element); - } - else { + } else { generalPackages.push(element); } } }); - } logger.trace(`Starting looking for generalPackages...`); @@ -372,7 +375,6 @@ export class GBMinService { doIt(e); }); - /** Deploys all .gbapp files first. */ let appPackagesProcessed = 0; @@ -382,11 +384,14 @@ export class GBMinService { // Skips .gbapp inside deploy folder. if (!e.startsWith('deploy')) { import(e).then(m => { - let p = new m.Package(); + let p = new m.Package() as IGBPackage; p.loadPackage(core, core.sequelize); appPackages.push(p); logger.trace(`App (.gbapp) deployed: ${e}.`); appPackagesProcessed++; + }, err => { + logger.trace(`Error deploying App (.gbapp): ${e}: ${err}`); + appPackagesProcessed++; }).catch(err => { logger.trace(`Error deploying App (.gbapp): ${e}: ${err}`); appPackagesProcessed++; @@ -396,7 +401,6 @@ export class GBMinService { } }); - WaitUntil() .interval(1000) .times(10) diff --git a/deploy/customer-satisfaction.gbapp/index.ts b/deploy/customer-satisfaction.gbapp/index.ts index 3981ca5d..b28b8ca9 100644 --- a/deploy/customer-satisfaction.gbapp/index.ts +++ b/deploy/customer-satisfaction.gbapp/index.ts @@ -33,6 +33,7 @@ "use strict"; const UrlJoin = require("url-join"); + import { GuaribasQuestionAlternate } from './models/index'; import { QualityDialog } from './dialogs/QualityDialog'; import { FeedbackDialog } from './dialogs/FeedbackDialog'; @@ -42,22 +43,24 @@ import { Sequelize } from 'sequelize-typescript'; export class GBCustomerSatisfactionPackage implements IGBPackage { sysPackages: IGBPackage[] = null; + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { core.sequelize.addModels([ GuaribasQuestionAlternate - ]); + ]); } + unloadPackage(core: IGBCoreService): void { - } + loadBot(min: GBMinInstance): void { FeedbackDialog.setup(min.bot, min); QualityDialog.setup(min.bot, min); } + unloadBot(min: GBMinInstance): void { - } + onNewSession(min: GBMinInstance, session: Session): void { - } } diff --git a/deploy/customer-satisfaction.gbapp/models/index.ts b/deploy/customer-satisfaction.gbapp/models/index.ts index d05ea211..8487ad77 100644 --- a/deploy/customer-satisfaction.gbapp/models/index.ts +++ b/deploy/customer-satisfaction.gbapp/models/index.ts @@ -33,13 +33,14 @@ "use strict"; import { - Sequelize, DataTypes, DataTypeUUIDv4, DataTypeDate, DataTypeDecimal } from "sequelize"; + import { + Sequelize, Table, Column, Model, @@ -55,6 +56,7 @@ import { PrimaryKey, AutoIncrement } from "sequelize-typescript"; + import { GuaribasInstance } from "../../core.gbapp/models/GBModel"; @Table diff --git a/deploy/kb.gbapp/index.ts b/deploy/kb.gbapp/index.ts index 39fe4311..ba6f46a5 100644 --- a/deploy/kb.gbapp/index.ts +++ b/deploy/kb.gbapp/index.ts @@ -44,31 +44,28 @@ import { Sequelize } from 'sequelize-typescript'; import { IGBCoreService } from 'botlib'; export class GBKBPackage implements IGBPackage { - sysPackages: IGBPackage[] = null; - + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { core.sequelize.addModels([ GuaribasAnswer, GuaribasQuestion, GuaribasSubject ]); - } - unloadPackage(core: IGBCoreService): void { - - } - loadBot(min: GBMinInstance): void { + unloadPackage(core: IGBCoreService): void { + } + + loadBot(min: GBMinInstance): void { AskDialog.setup(min.bot, min); FaqDialog.setup(min.bot, min); MenuDialog.setup(min.bot, min); - } + unloadBot(min: GBMinInstance): void { - } + onNewSession(min: GBMinInstance, session: Session): void { - } } diff --git a/deploy/kb.gbapp/models/index.ts b/deploy/kb.gbapp/models/index.ts index 78a23db1..dd33fec6 100644 --- a/deploy/kb.gbapp/models/index.ts +++ b/deploy/kb.gbapp/models/index.ts @@ -33,13 +33,14 @@ "use strict"; import { - Sequelize, DataTypes, DataTypeUUIDv4, DataTypeDate, DataTypeDecimal } from "sequelize"; + import { + Sequelize, Table, Column, Model, @@ -59,7 +60,6 @@ import { import { GuaribasUser } from "../../security.gblib/models"; import { GuaribasInstance, GuaribasPackage } from "../../core.gbapp/models/GBModel"; - @Table export class GuaribasSubject extends Model { @PrimaryKey @@ -84,7 +84,7 @@ export class GuaribasSubject extends Model { @BelongsTo(() => GuaribasSubject, "parentSubjectId") parentSubject: GuaribasSubject; - @HasMany(() => GuaribasSubject, {foreignKey: "parentSubjectId"}) + @HasMany(() => GuaribasSubject, { foreignKey: "parentSubjectId" }) childrenSubjects: GuaribasSubject[]; @ForeignKey(() => GuaribasInstance) @@ -107,10 +107,8 @@ export class GuaribasSubject extends Model { @BelongsTo(() => GuaribasPackage) package: GuaribasPackage; - } - @Table export class GuaribasQuestion extends Model { @PrimaryKey @@ -118,42 +116,42 @@ export class GuaribasQuestion extends Model { @Column questionId: number; - @Column({ type: DataType.STRING(64) }) + @Column(DataType.STRING(64)) @Column subject1: string; - @Column({ type: DataType.STRING(64) }) + @Column(DataType.STRING(64)) @Column subject2: string; - @Column({ type: DataType.STRING(64) }) + @Column(DataType.STRING(64)) @Column subject3: string; - @Column({ type: DataType.STRING(64) }) + @Column(DataType.STRING(64)) @Column subject4: string; - @Column({ type: DataType.STRING(1024) }) + @Column(DataType.STRING(1024)) @Column keywords: string; - @Column({ type: DataType.STRING(512) }) + @Column(DataType.STRING(512)) from: string; - @Column({ type: DataType.STRING(512) }) + @Column(DataType.STRING(512)) to: string; - @Column({ type: DataType.TEXT }) + @Column(DataType.TEXT) content: string; @Column @CreatedAt - creationDate: Date; + createdAt: Date; @Column @UpdatedAt - updatedOn: Date; + updatedAt: Date; @ForeignKey(() => GuaribasAnswer) @Column @@ -189,16 +187,16 @@ export class GuaribasAnswer extends Model { @Column format: string; - @Column({ type: DataType.TEXT }) + @Column(DataType.TEXT) content: string; @Column @CreatedAt - creationDate: Date; + createdAt: Date; @Column @UpdatedAt - updatedOn: Date; + updatedAt: Date; @HasMany(() => GuaribasQuestion) questions: GuaribasQuestion[]; diff --git a/deploy/kb.gbapp/services/KBService.ts b/deploy/kb.gbapp/services/KBService.ts index a54eb041..24a4750c 100644 --- a/deploy/kb.gbapp/services/KBService.ts +++ b/deploy/kb.gbapp/services/KBService.ts @@ -44,7 +44,7 @@ const marked = require("marked"); -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { GBConfigService } from './../../core.gbapp/services/GBConfigService'; import { GuaribasQuestion, GuaribasAnswer, GuaribasSubject } from "../models"; import { GBServiceCallback, IGBCoreService, IGBConversationalService, IGBInstance } from "botlib"; @@ -129,7 +129,7 @@ export class KBService { let text = KBService.getSubjectItemsSeparatedBySpaces( subjects ); - if (text){ + if (text) { what = `${what} ${text}`; } } diff --git a/deploy/security.gblib/index.ts b/deploy/security.gblib/index.ts index 78b071b9..89b4c5ea 100644 --- a/deploy/security.gblib/index.ts +++ b/deploy/security.gblib/index.ts @@ -34,14 +34,14 @@ const UrlJoin = require("url-join"); - import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib"; import { Session } from 'botbuilder'; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { GuaribasUser, GuaribasGroup, GuaribasUserGroup } from "./models"; export class GBSecurityPackage implements IGBPackage { sysPackages: IGBPackage[] = null; + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { core.sequelize.addModels([ GuaribasGroup, @@ -49,19 +49,16 @@ export class GBSecurityPackage implements IGBPackage { GuaribasUserGroup ]); } - + unloadPackage(core: IGBCoreService): void { - } loadBot(min: GBMinInstance): void { - } unloadBot(min: GBMinInstance): void { - } + onNewSession(min: GBMinInstance, session: Session): void { - } } diff --git a/deploy/security.gblib/models/index.ts b/deploy/security.gblib/models/index.ts index 9fe06cfd..d889ff81 100644 --- a/deploy/security.gblib/models/index.ts +++ b/deploy/security.gblib/models/index.ts @@ -30,17 +30,17 @@ | | \*****************************************************************************/ - "use strict"; import { - Sequelize, DataTypes, DataTypeUUIDv4, DataTypeDate, DataTypeDecimal } from "sequelize"; + import { + Sequelize, Table, Column, Model, @@ -56,8 +56,8 @@ import { PrimaryKey, AutoIncrement } from "sequelize-typescript"; -import { GuaribasInstance } from "../../core.gbapp/models/GBModel"; +import { GuaribasInstance } from "../../core.gbapp/models/GBModel"; @Table export class GuaribasUser extends Model { @@ -75,18 +75,15 @@ export class GuaribasUser extends Model { @Column email: string; - @Column({ type: DataType.STRING(512) }) - @Column + @Column(DataType.STRING(512)) internalAddress: string; - @ForeignKey(() => - GuaribasInstance) + @ForeignKey(() => GuaribasInstance) @Column instanceId: number; @BelongsTo(() => GuaribasInstance) instance: GuaribasInstance; - } @Table @@ -100,12 +97,11 @@ export class GuaribasGroup extends Model { @Column displayName: string; - @ForeignKey(() => GuaribasInstance) @Column instanceId: number; - @BelongsTo(() => GuaribasInstance ) + @BelongsTo(() => GuaribasInstance) instance: GuaribasInstance; } diff --git a/deploy/whatsapp.gblib/index.ts b/deploy/whatsapp.gblib/index.ts index 47d8ea94..acac77e1 100644 --- a/deploy/whatsapp.gblib/index.ts +++ b/deploy/whatsapp.gblib/index.ts @@ -34,39 +34,40 @@ const UrlJoin = require("url-join"); - import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib"; import { Session } from 'botbuilder'; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { WhatsappDirectLine } from "./services/WhatsappDirectLine"; - export class GBWhatsappPackage implements IGBPackage { + sysPackages: IGBPackage[] = null; + channel: WhatsappDirectLine; - sysPackages: IGBPackage[] = null; - channel: WhatsappDirectLine; + loadPackage(core: IGBCoreService, sequelize: Sequelize): void { + } - loadPackage(core: IGBCoreService, sequelize: Sequelize): void { + unloadPackage(core: IGBCoreService): void { + } + + loadBot(min: GBMinInstance): void { + + // Only loads engine if it is defined on services.json. + + if (min.instance.whatsappBotKey != "") { + this.channel = new WhatsappDirectLine( + min.botId, + min.instance.whatsappBotKey, + min.instance.whatsappServiceKey, + min.instance.whatsappServiceNumber, + min.instance.whatsappServiceUrl, + min.instance.whatsappServiceWebhookUrl + ); } + } - unloadPackage(core: IGBCoreService): void { + unloadBot(min: GBMinInstance): void { + } - } - - loadBot(min: GBMinInstance): void { - - // Only loads engine if it is defined on services.json. - - if (min.instance.whatsappBotKey != "") { - this.channel = new WhatsappDirectLine(min.botId, min.instance.whatsappBotKey, min.instance.whatsappServiceKey, - min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl, min.instance.whatsappServiceWebhookUrl); - } - } - - unloadBot(min: GBMinInstance): void { - - } - onNewSession(min: GBMinInstance, session: Session): void { - - } + onNewSession(min: GBMinInstance, session: Session): void { + } } diff --git a/package-lock.json b/package-lock.json index a7f8c7dd..65a6f65f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "botserver", - "version": "0.0.28", + "version": "0.0.30", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -597,9 +597,9 @@ } }, "botlib": { - "version": "0.0.25", - "resolved": "https://registry.npmjs.org/botlib/-/botlib-0.0.25.tgz", - "integrity": "sha512-GRR4Hk+seayNzEoceoUYxE6MyAo4YjSH/54Cq+P2v0X2toT7jkdggL5ii2bTzJyHqbaiGYwyh0n8ZDInLk8+dg==", + "version": "0.0.31", + "resolved": "https://registry.npmjs.org/botlib/-/botlib-0.0.31.tgz", + "integrity": "sha512-LgWZsgPnEj5kZx5Hp4yM4ntKKUGrx1ONLyhfHsl/gXX2Pz9AMTtskkMmy5llbV2wdipuD85lorDCNez+nZxFlw==", "requires": { "async": "2.6.1", "botbuilder": "3.15.0", diff --git a/package.json b/package.json index a98cf89e..bd449ae5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "botserver", - "version": "0.0.29", + "version": "0.0.30", "description": "General Bot Community Edition open-core server.", "main": "./src/app.ts", "homepage": "http://www.generalbot.com", @@ -33,7 +33,7 @@ "async": "^2.6.1", "body-parser": "^1.18.3", "botbuilder": "^3.15.0", - "botlib": "^0.0.28", + "botlib": "^0.0.31", "chokidar": "^2.0.3", "csv-parse": "^2.4.0", "dotenv-extended": "^2.0.2", diff --git a/src/app.ts b/src/app.ts index 65be40f8..23dbd119 100644 --- a/src/app.ts +++ b/src/app.ts @@ -37,8 +37,9 @@ const UrlJoin = require("url-join"); const logger = require("./logger"); const express = require("express"); const bodyParser = require("body-parser"); + import { UniversalBot } from "botbuilder"; -import { Sequelize } from "sequelize-typescript"; +import { Sequelize } from 'sequelize-typescript'; import { GBConfigService } from "../deploy/core.gbapp/services/GBConfigService"; import { GBConversationalService } from "../deploy/core.gbapp/services/GBConversationalService"; import { GBMinService } from "../deploy/core.gbapp/services/GBMinService"; @@ -56,7 +57,6 @@ import { IGBPackage } from 'botlib'; let appPackages = new Array(); - /** * General Bots open-core entry point. */