From 5829762f91f6cafa832489bfdef4487c51741477 Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 00:13:00 -0300 Subject: [PATCH 1/7] update: docker with expose port and localcache hGet and hSet return null --- Dockerfile | 2 ++ src/cache/localcache.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index f9fa812c..f23ebf00 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,4 +53,6 @@ COPY --from=builder /evolution/tsup.config.ts ./tsup.config.ts ENV DOCKER_ENV=true +EXPOSE 8080 + ENTRYPOINT ["/bin/bash", "-c", ". ./Docker/scripts/deploy_database.sh && npm run start:prod" ] \ No newline at end of file diff --git a/src/cache/localcache.ts b/src/cache/localcache.ts index 130d4865..1eb845aa 100644 --- a/src/cache/localcache.ts +++ b/src/cache/localcache.ts @@ -47,10 +47,12 @@ export class LocalCache implements ICache { async hGet() { console.log('hGet not implemented'); + return null } async hSet() { console.log('hSet not implemented'); + return null } async hDelete() { From 185fb587e0614afd7737ea2019863ea28d5d182a Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 01:25:40 -0300 Subject: [PATCH 2/7] DATABASE_PROVIDER default is posttgresql --- runWithProvider.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runWithProvider.js b/runWithProvider.js index 8fe1af0d..ea9fac35 100644 --- a/runWithProvider.js +++ b/runWithProvider.js @@ -6,7 +6,8 @@ const { DATABASE_PROVIDER } = process.env; if (!DATABASE_PROVIDER) { console.error('DATABASE_PROVIDER is not set in the .env file'); - process.exit(1); + DATABASE_PROVIDER = "postgresql" + // process.exit(1); } const command = process.argv From 58a8d92c37150f7a924d2bec5b78d208118584bd Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 01:33:20 -0300 Subject: [PATCH 3/7] DATABASE_PROVIDER default is posttgresql #1 --- runWithProvider.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runWithProvider.js b/runWithProvider.js index ea9fac35..a8275763 100644 --- a/runWithProvider.js +++ b/runWithProvider.js @@ -3,17 +3,17 @@ const { execSync } = require('child_process'); dotenv.config(); const { DATABASE_PROVIDER } = process.env; +const databaseProviderDefault = DATABASE_PROVIDER ?? "postgresql" if (!DATABASE_PROVIDER) { - console.error('DATABASE_PROVIDER is not set in the .env file'); - DATABASE_PROVIDER = "postgresql" + console.error(`DATABASE_PROVIDER is not set in the .env file, using default: ${databaseProviderDefault}`); // process.exit(1); } const command = process.argv .slice(2) .join(' ') - .replace(/\DATABASE_PROVIDER/g, DATABASE_PROVIDER); + .replace(/\DATABASE_PROVIDER/g, databaseProviderDefault); try { execSync(command, { stdio: 'inherit' }); From fe4a439fde4a9f21ddd1a8db3c07cde92096793e Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 12:24:33 -0300 Subject: [PATCH 4/7] fix: environment variables with optional --- src/config/env.config.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 76aac9c8..8e8ac12e 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -269,8 +269,8 @@ export class ConfigService { DISABLE_MANAGER: process.env?.SERVER_DISABLE_MANAGER === 'true', }, CORS: { - ORIGIN: process.env.CORS_ORIGIN.split(',') || ['*'], - METHODS: (process.env.CORS_METHODS.split(',') as HttpMethods[]) || ['POST', 'GET', 'PUT', 'DELETE'], + ORIGIN: process.env.CORS_ORIGIN?.split(',') || ['*'], + METHODS: (process.env.CORS_METHODS?.split(',') as HttpMethods[]) || ['POST', 'GET', 'PUT', 'DELETE'], CREDENTIALS: process.env?.CORS_CREDENTIALS === 'true', }, SSL_CONF: { @@ -354,7 +354,7 @@ export class ConfigService { LANGUAGE: process.env.WA_BUSINESS_LANGUAGE || 'en', }, LOG: { - LEVEL: (process.env?.LOG_LEVEL.split(',') as LogLevel[]) || [ + LEVEL: (process.env?.LOG_LEVEL?.split(',') as LogLevel[]) || [ 'ERROR', 'WARN', 'DEBUG', From 0b40583fa899880457a9bd8707ee1f9437b72ad4 Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 12:39:11 -0300 Subject: [PATCH 5/7] fix: environment variables with optional #1 --- src/config/env.config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 8e8ac12e..470c9473 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -270,7 +270,7 @@ export class ConfigService { }, CORS: { ORIGIN: process.env.CORS_ORIGIN?.split(',') || ['*'], - METHODS: (process.env.CORS_METHODS?.split(',') as HttpMethods[]) || ['POST', 'GET', 'PUT', 'DELETE'], + METHODS: (process.env.CORS_METHODS?.split(',') as HttpMethods[]) || ['POST', 'GET', 'PUT', 'DELETE'] as HttpMethods[], CREDENTIALS: process.env?.CORS_CREDENTIALS === 'true', }, SSL_CONF: { @@ -364,7 +364,7 @@ export class ConfigService { 'DARK', 'WEBHOOKS', 'WEBSOCKET', - ], + ] as LogLevel[], COLOR: process.env?.LOG_COLOR === 'true', BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error', }, From 9a6d4a8e443d42f854103e2dca8d30b63565504b Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 14:55:34 -0300 Subject: [PATCH 6/7] add: hGet and hSet on LocalCache --- src/cache/localcache.ts | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/cache/localcache.ts b/src/cache/localcache.ts index 1eb845aa..852044ff 100644 --- a/src/cache/localcache.ts +++ b/src/cache/localcache.ts @@ -1,8 +1,11 @@ import { ICache } from '@api/abstract/abstract.cache'; import { CacheConf, CacheConfLocal, ConfigService } from '@config/env.config'; import NodeCache from 'node-cache'; +import { BufferJSON } from 'baileys'; +import { Logger } from '@config/logger.config'; export class LocalCache implements ICache { + private readonly logger = new Logger('LocalCache'); private conf: CacheConfLocal; static localCache = new NodeCache(); @@ -45,14 +48,36 @@ export class LocalCache implements ICache { return `${this.module}:${key}`; } - async hGet() { - console.log('hGet not implemented'); - return null + async hGet(key: string, field: string) { + try { + const data = LocalCache.localCache.get(this.buildKey(key)) as Object; + + if (data && field in data) { + return JSON.parse(data[field], BufferJSON.reviver); + } + + return null; + } catch (error) { + this.logger.error(error); + } } - async hSet() { - console.log('hSet not implemented'); - return null + async hSet(key: string, field: string, value: any) { + try { + const json = JSON.stringify(value, BufferJSON.replacer); + + let hash = LocalCache.localCache.get(this.buildKey(key)); + + if (!hash) { + hash = {}; + } + + hash[field] = json; + LocalCache.localCache.set(key, hash); + + } catch (error) { + this.logger.error(error); + } } async hDelete() { From 0ad330bdb32dabe0bc92ba5962213ae3bf85ab75 Mon Sep 17 00:00:00 2001 From: oismaelash Date: Wed, 2 Oct 2024 15:15:12 -0300 Subject: [PATCH 7/7] add: hDelete on LocalCache --- src/cache/localcache.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cache/localcache.ts b/src/cache/localcache.ts index 852044ff..7e328195 100644 --- a/src/cache/localcache.ts +++ b/src/cache/localcache.ts @@ -80,8 +80,19 @@ export class LocalCache implements ICache { } } - async hDelete() { - console.log('hDelete not implemented'); - return 0; + async hDelete(key: string, field: string) { + try { + const data = LocalCache.localCache.get(this.buildKey(key)) as Object; + + if (data && field in data) { + delete data[field]; + LocalCache.localCache.set(key, data); + return 1; + } + + return 0; + } catch (error) { + this.logger.error(error); + } } }