@@ -5,12 +5,13 @@ import { MessageUpRepository } from './messageUp.repository';
55import { MongoClient } from 'mongodb' ;
66import { WebhookRepository } from './webhook.repository' ;
77import { ChatwootRepository } from './chatwoot.repository' ;
8+
89import { AuthRepository } from './auth.repository' ;
910import { Auth , ConfigService , Database } from '../../config/env.config' ;
1011import { execSync } from 'child_process' ;
1112import { join } from 'path' ;
13+ import fs from 'fs' ;
1214import { Logger } from '../../config/logger.config' ;
13-
1415export class RepositoryBroker {
1516 constructor (
1617 public readonly message : MessageRepository ,
@@ -23,7 +24,6 @@ export class RepositoryBroker {
2324 private configService : ConfigService ,
2425 dbServer ?: MongoClient ,
2526 ) {
26- this . logger . verbose ( 'initializing repository broker' ) ;
2727 this . dbClient = dbServer ;
2828 this . __init_repo_without_db__ ( ) ;
2929 }
@@ -38,39 +38,46 @@ export class RepositoryBroker {
3838 private __init_repo_without_db__ ( ) {
3939 this . logger . verbose ( 'initializing repository without db' ) ;
4040 if ( ! this . configService . get < Database > ( 'DATABASE' ) . ENABLED ) {
41- this . logger . verbose ( 'database is disabled' ) ;
42-
4341 const storePath = join ( process . cwd ( ) , 'store' ) ;
44-
4542 this . logger . verbose ( 'creating store path: ' + storePath ) ;
46- execSync (
47- `mkdir -p ${ join (
43+ try {
44+ const authDir = join (
4845 storePath ,
4946 'auth' ,
5047 this . configService . get < Auth > ( 'AUTHENTICATION' ) . TYPE ,
51- ) } `,
52- ) ;
53-
54- this . logger . verbose ( 'creating chats path: ' + join ( storePath , 'chats' ) ) ;
55- execSync ( `mkdir -p ${ join ( storePath , 'chats' ) } ` ) ;
56-
57- this . logger . verbose ( 'creating contacts path: ' + join ( storePath , 'contacts' ) ) ;
58- execSync ( `mkdir -p ${ join ( storePath , 'contacts' ) } ` ) ;
59-
60- this . logger . verbose ( 'creating messages path: ' + join ( storePath , 'messages' ) ) ;
61- execSync ( `mkdir -p ${ join ( storePath , 'messages' ) } ` ) ;
62-
63- this . logger . verbose ( 'creating message-up path: ' + join ( storePath , 'message-up' ) ) ;
64- execSync ( `mkdir -p ${ join ( storePath , 'message-up' ) } ` ) ;
65-
66- this . logger . verbose ( 'creating webhook path: ' + join ( storePath , 'webhook' ) ) ;
67- execSync ( `mkdir -p ${ join ( storePath , 'webhook' ) } ` ) ;
68-
69- this . logger . verbose ( 'creating chatwoot path: ' + join ( storePath , 'chatwoot' ) ) ;
70- execSync ( `mkdir -p ${ join ( storePath , 'chatwoot' ) } ` ) ;
48+ ) ;
49+ const chatsDir = join ( storePath , 'chats' ) ;
50+ const contactsDir = join ( storePath , 'contacts' ) ;
51+ const messagesDir = join ( storePath , 'messages' ) ;
52+ const messageUpDir = join ( storePath , 'message-up' ) ;
53+ const webhookDir = join ( storePath , 'webhook' ) ;
54+ const chatwootDir = join ( storePath , 'chatwoot' ) ;
7155
72- this . logger . verbose ( 'creating temp path: ' + join ( storePath , 'temp' ) ) ;
73- execSync ( `mkdir -p ${ join ( storePath , 'temp' ) } ` ) ;
56+ // Check if directories exist, create them if not
57+ if ( ! fs . existsSync ( authDir ) ) {
58+ fs . mkdirSync ( authDir , { recursive : true } ) ;
59+ }
60+ if ( ! fs . existsSync ( chatsDir ) ) {
61+ fs . mkdirSync ( chatsDir , { recursive : true } ) ;
62+ }
63+ if ( ! fs . existsSync ( contactsDir ) ) {
64+ fs . mkdirSync ( contactsDir , { recursive : true } ) ;
65+ }
66+ if ( ! fs . existsSync ( messagesDir ) ) {
67+ fs . mkdirSync ( messagesDir , { recursive : true } ) ;
68+ }
69+ if ( ! fs . existsSync ( messageUpDir ) ) {
70+ fs . mkdirSync ( messageUpDir , { recursive : true } ) ;
71+ }
72+ if ( ! fs . existsSync ( webhookDir ) ) {
73+ fs . mkdirSync ( webhookDir , { recursive : true } ) ;
74+ }
75+ if ( ! fs . existsSync ( chatwootDir ) ) {
76+ fs . mkdirSync ( chatwootDir , { recursive : true } ) ;
77+ }
78+ } catch ( error ) {
79+ console . error ( error ) ;
80+ }
7481 }
7582 }
7683}
0 commit comments