From 7b1a4554ad9bccaebf978c04150bc866967f58cd Mon Sep 17 00:00:00 2001 From: w3nder Date: Tue, 18 Jul 2023 13:08:53 -0300 Subject: [PATCH] fix: create folder store --- src/whatsapp/repository/repository.manager.ts | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/whatsapp/repository/repository.manager.ts b/src/whatsapp/repository/repository.manager.ts index 9740b436..ca6e09c7 100644 --- a/src/whatsapp/repository/repository.manager.ts +++ b/src/whatsapp/repository/repository.manager.ts @@ -5,12 +5,13 @@ import { MessageUpRepository } from './messageUp.repository'; import { MongoClient } from 'mongodb'; import { WebhookRepository } from './webhook.repository'; import { ChatwootRepository } from './chatwoot.repository'; + import { AuthRepository } from './auth.repository'; import { Auth, ConfigService, Database } from '../../config/env.config'; import { execSync } from 'child_process'; import { join } from 'path'; +import fs from 'fs'; import { Logger } from '../../config/logger.config'; - export class RepositoryBroker { constructor( public readonly message: MessageRepository, @@ -23,7 +24,6 @@ export class RepositoryBroker { private configService: ConfigService, dbServer?: MongoClient, ) { - this.logger.verbose('initializing repository broker'); this.dbClient = dbServer; this.__init_repo_without_db__(); } @@ -38,39 +38,46 @@ export class RepositoryBroker { private __init_repo_without_db__() { this.logger.verbose('initializing repository without db'); if (!this.configService.get('DATABASE').ENABLED) { - this.logger.verbose('database is disabled'); - const storePath = join(process.cwd(), 'store'); - this.logger.verbose('creating store path: ' + storePath); - execSync( - `mkdir -p ${join( + try { + const authDir = join( storePath, 'auth', this.configService.get('AUTHENTICATION').TYPE, - )}`, - ); + ); + const chatsDir = join(storePath, 'chats'); + const contactsDir = join(storePath, 'contacts'); + const messagesDir = join(storePath, 'messages'); + const messageUpDir = join(storePath, 'message-up'); + const webhookDir = join(storePath, 'webhook'); + const chatwootDir = join(storePath, 'chatwoot'); - this.logger.verbose('creating chats path: ' + join(storePath, 'chats')); - execSync(`mkdir -p ${join(storePath, 'chats')}`); - - this.logger.verbose('creating contacts path: ' + join(storePath, 'contacts')); - execSync(`mkdir -p ${join(storePath, 'contacts')}`); - - this.logger.verbose('creating messages path: ' + join(storePath, 'messages')); - execSync(`mkdir -p ${join(storePath, 'messages')}`); - - this.logger.verbose('creating message-up path: ' + join(storePath, 'message-up')); - execSync(`mkdir -p ${join(storePath, 'message-up')}`); - - this.logger.verbose('creating webhook path: ' + join(storePath, 'webhook')); - execSync(`mkdir -p ${join(storePath, 'webhook')}`); - - this.logger.verbose('creating chatwoot path: ' + join(storePath, 'chatwoot')); - execSync(`mkdir -p ${join(storePath, 'chatwoot')}`); - - this.logger.verbose('creating temp path: ' + join(storePath, 'temp')); - execSync(`mkdir -p ${join(storePath, 'temp')}`); + // Check if directories exist, create them if not + if (!fs.existsSync(authDir)) { + fs.mkdirSync(authDir, { recursive: true }); + } + if (!fs.existsSync(chatsDir)) { + fs.mkdirSync(chatsDir, { recursive: true }); + } + if (!fs.existsSync(contactsDir)) { + fs.mkdirSync(contactsDir, { recursive: true }); + } + if (!fs.existsSync(messagesDir)) { + fs.mkdirSync(messagesDir, { recursive: true }); + } + if (!fs.existsSync(messageUpDir)) { + fs.mkdirSync(messageUpDir, { recursive: true }); + } + if (!fs.existsSync(webhookDir)) { + fs.mkdirSync(webhookDir, { recursive: true }); + } + if (!fs.existsSync(chatwootDir)) { + fs.mkdirSync(chatwootDir, { recursive: true }); + } + } catch (error) { + console.error(error); + } } } }