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 Excel from 'exceljs';
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 { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService.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.
*/
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);
return await service.botExists(botId);
}
}
/**

View file

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

View file

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

View file

@ -37,6 +37,7 @@ import { GBOnlineSubscription } from './model/MainModel.js'
import { MSSubscriptionService } from './service/MSSubscription.js'
import { CollectionUtil } from 'pragmatismo-io-framework';
import { NewUserDialog } from './dialog/NewUserDialog.js'
import { GBOService } from './service/GBOService.js'
export class SaaSPackage implements IGBPackage {
sysPackages: IGBPackage[]
@ -109,7 +110,7 @@ export class SaaSPackage implements IGBPackage {
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.

View file

@ -32,26 +32,24 @@
import { GBMinInstance, GBLog } from "botlib";
import { CollectionUtil } from 'pragmatismo-io-framework';
const MicrosoftGraph = require("@microsoft/microsoft-graph-client");
const Juno = require('juno-payment-node');
const sgMail = require('@sendgrid/mail');
const PasswordGenerator = require('strict-password-generator').default;
import MicrosoftGraph from "@microsoft/microsoft-graph-client";
import sgMail from '@sendgrid/mail';
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 {
public isValidCardNumber(ccNumber) {
let card = new Juno.Card();
return card.validateNumber(ccNumber);
}
public isValidSecurityCode(ccNumber, cvcNumber) {
let card = new Juno.Card();
return card.validateCvc(ccNumber, cvcNumber);
}
public isValidExpireDate(month, year) {
let card = new Juno.Card();
return card.validateExpireDate(month, year);
}
public async sendEmail(token: string, to: string, from: string,
@ -127,7 +125,7 @@ export class GBOService {
}
public async listTemplates(min: GBMinInstance) {
if (GBConfigService.get('GB_MODE') === 'legacy') {
let templateLibraryId = process.env.SAAS_TEMPLATE_LIBRARY;
let siteId = process.env.STORAGE_SITE_ID;
@ -145,7 +143,23 @@ export class GBOService {
.get();
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) {

View file

@ -34,6 +34,7 @@ import { GBOnlineSubscription } from '../model/MainModel.js';
import { GBMinInstance, GBLog } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework';
import urlJoin from 'url-join';
import { GBOService } from './GBOService.js';
export class MainService {
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 libraryId = process.env.STORAGE_LIBRARY;
let gboService = min.gbappServices['gboService'];
let gboService = new GBOService();
let sleep = ms => {
return new Promise(resolve => {