fix(services): refactor GBOService instantiation and update template listing logic

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-03-29 11:03:46 -03:00
parent 61d8cfe93c
commit 0e74502cc1
6 changed files with 91 additions and 60 deletions

View file

@ -49,7 +49,7 @@ import { GBServer } from '../../../src/app.js';
import { GBVMService } from '../../basic.gblib/services/GBVMService.js'; import { GBVMService } from '../../basic.gblib/services/GBVMService.js';
import Excel from 'exceljs'; import Excel from 'exceljs';
import asyncPromise from 'async-promises'; import asyncPromise from 'async-promises';
import { GuaribasPackage } from '../models/GBModel.js'; import { GuaribasInstance, GuaribasPackage } from '../models/GBModel.js';
import { GBAdminService } from './../../admin.gbapp/services/GBAdminService.js'; import { GBAdminService } from './../../admin.gbapp/services/GBAdminService.js';
import { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService.js'; import { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService.js';
import { KBService } from './../../kb.gbapp/services/KBService.js'; import { KBService } from './../../kb.gbapp/services/KBService.js';
@ -269,9 +269,22 @@ export class GBDeployer implements IGBDeployer {
* Verifies if bot exists on bot catalog. * Verifies if bot exists on bot catalog.
*/ */
public async botExists(botId: string): Promise<boolean> { public async botExists(botId: string): Promise<boolean> {
if (GBConfigService.get('GB_MODE') !== 'legacy') {
const where = { botId: botId };
return await GuaribasInstance.findOne({
where: where
}) !== null;
}
else {
const service = await AzureDeployerService.createInstance(this); const service = await AzureDeployerService.createInstance(this);
return await service.botExists(botId); return await service.botExists(botId);
}
} }
/** /**

View file

@ -1269,7 +1269,7 @@ export class GBMinService {
if (GBConfigService.get('GB_MODE') !== 'legacy') { if (GBConfigService.get('GB_MODE') !== 'legacy') {
const context = adapter['createContext'](req); const context = adapter['createContext'](req);
context['_activity'] = context.activity.body; context['_activity'] = context.activity.body;
await handler(context); await adapter['processActivity'](req, res, handler);
// Return status // Return status
res.status(200); res.status(200);

View file

@ -35,6 +35,7 @@ import { Messages } from '../strings.js';
import { MainService } from '../service/MainService.js'; import { MainService } from '../service/MainService.js';
import { SaaSPackage } from '../index.js'; import { SaaSPackage } from '../index.js';
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import { GBOService } from '../service/GBOService.js';
export class NewUserDialog extends IGBDialog { export class NewUserDialog extends IGBDialog {
static getBotNameDialog(min: GBMinInstance) { static getBotNameDialog(min: GBMinInstance) {
@ -83,7 +84,7 @@ export class NewUserDialog extends IGBDialog {
async step => { async step => {
const locale = 'en-US'; const locale = 'en-US';
await step.context.sendActivity('Aqui estão alguns modelos para você escolher:'); await step.context.sendActivity('Aqui estão alguns modelos para você escolher:');
let gboService = min.gbappServices['gboService']; let gboService = new GBOService();
const list = await gboService.listTemplates(min); const list = await gboService.listTemplates(min);
let templateMessage = undefined; let templateMessage = undefined;
@ -101,8 +102,9 @@ export class NewUserDialog extends IGBDialog {
async step => { async step => {
const list = step.activeDialog.state.options.templateList; const list = step.activeDialog.state.options.templateList;
let template = null; let template = null;
let gboService = new GBOService();
await CollectionUtil.asyncForEach(list, async item => { await CollectionUtil.asyncForEach(list, async item => {
let gboService = min.gbappServices['gboService'];
if (gboService.kmpSearch(step.context.activity.originalText, item.name) != -1) { if (gboService.kmpSearch(step.context.activity.originalText, item.name) != -1) {
template = item.name; template = item.name;
} }

View file

@ -37,6 +37,7 @@ import { GBOnlineSubscription } from './model/MainModel.js'
import { MSSubscriptionService } from './service/MSSubscription.js' import { MSSubscriptionService } from './service/MSSubscription.js'
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import { NewUserDialog } from './dialog/NewUserDialog.js' import { NewUserDialog } from './dialog/NewUserDialog.js'
import { GBOService } from './service/GBOService.js'
export class SaaSPackage implements IGBPackage { export class SaaSPackage implements IGBPackage {
sysPackages: IGBPackage[] sysPackages: IGBPackage[]
@ -109,7 +110,7 @@ export class SaaSPackage implements IGBPackage {
async loadBot(min: GBMinInstance): Promise<void> { async loadBot(min: GBMinInstance): Promise<void> {
let gboService = min.gbappServices['gboService']; let gboService = new GBOService();
// Gets the sendToDevice method of whatsapp.gblib and setups scheduler. // Gets the sendToDevice method of whatsapp.gblib and setups scheduler.

View file

@ -32,26 +32,24 @@
import { GBMinInstance, GBLog } from "botlib"; import { GBMinInstance, GBLog } from "botlib";
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
const MicrosoftGraph = require("@microsoft/microsoft-graph-client"); import MicrosoftGraph from "@microsoft/microsoft-graph-client";
const Juno = require('juno-payment-node');
const sgMail = require('@sendgrid/mail'); import sgMail from '@sendgrid/mail';
const PasswordGenerator = require('strict-password-generator').default; import { default as PasswordGenerator } from 'strict-password-generator';
import { promises as fs } from 'fs';
import { GBConfigService } from "../../core.gbapp/services/GBConfigService.js";
import path from "path";
export class GBOService { export class GBOService {
public isValidCardNumber(ccNumber) { public isValidCardNumber(ccNumber) {
let card = new Juno.Card();
return card.validateNumber(ccNumber);
} }
public isValidSecurityCode(ccNumber, cvcNumber) { public isValidSecurityCode(ccNumber, cvcNumber) {
let card = new Juno.Card();
return card.validateCvc(ccNumber, cvcNumber);
} }
public isValidExpireDate(month, year) { public isValidExpireDate(month, year) {
let card = new Juno.Card();
return card.validateExpireDate(month, year);
} }
public async sendEmail(token: string, to: string, from: string, public async sendEmail(token: string, to: string, from: string,
@ -127,7 +125,7 @@ export class GBOService {
} }
public async listTemplates(min: GBMinInstance) { public async listTemplates(min: GBMinInstance) {
if (GBConfigService.get('GB_MODE') === 'legacy') {
let templateLibraryId = process.env.SAAS_TEMPLATE_LIBRARY; let templateLibraryId = process.env.SAAS_TEMPLATE_LIBRARY;
let siteId = process.env.STORAGE_SITE_ID; let siteId = process.env.STORAGE_SITE_ID;
@ -145,7 +143,23 @@ export class GBOService {
.get(); .get();
return res.value; return res.value;
}
else {
const templatesDir = path.join(process.env.PWD,'templates');
const gbaiDirectories = [];
// Read all entries in the templates directory
const entries = await fs.readdir(templatesDir, { withFileTypes: true });
for (const entry of entries) {
// Check if it's a directory and ends with .gbai
if (entry.isDirectory() && entry.name.endsWith('.gbai')) {
gbaiDirectories.push({ name: entry.name });
}
}
return gbaiDirectories;
}
} }
public async copyTemplates(min: GBMinInstance, gbaiDest, templateName: string, kind: string, botName: string) { public async copyTemplates(min: GBMinInstance, gbaiDest, templateName: string, kind: string, botName: string) {

View file

@ -34,6 +34,7 @@ import { GBOnlineSubscription } from '../model/MainModel.js';
import { GBMinInstance, GBLog } from 'botlib'; import { GBMinInstance, GBLog } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import urlJoin from 'url-join'; import urlJoin from 'url-join';
import { GBOService } from './GBOService.js';
export class MainService { export class MainService {
async createSubscriptionMSFT(email: string, plan: string, offer: string, quantity: number, additionalData: any) { } async createSubscriptionMSFT(email: string, plan: string, offer: string, quantity: number, additionalData: any) { }
@ -108,7 +109,7 @@ export class MainService {
let siteId = process.env.STORAGE_SITE_ID; let siteId = process.env.STORAGE_SITE_ID;
let libraryId = process.env.STORAGE_LIBRARY; let libraryId = process.env.STORAGE_LIBRARY;
let gboService = min.gbappServices['gboService']; let gboService = new GBOService();
let sleep = ms => { let sleep = ms => {
return new Promise(resolve => { return new Promise(resolve => {