From 80e3116cd8e879db47bf1c429d4c270e8dc08813 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 26 Jul 2023 21:45:23 -0300 Subject: [PATCH 1/8] text: Fix problem No Session --- .DS_Store | Bin 6148 -> 6148 bytes package.json | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 11ffa2bd7bc24f00311a501886899f63ac042d9d..f07b5fd410a708405b4339fdbe299d449393688a 100644 GIT binary patch delta 32 ocmZoMXfc@J&&aVcU^gQp$7UWTX2#7+nUh#17EIgB&heKY0HA9M0ssI2 delta 172 zcmZoMXfc@J&&azmU^gQp?`9q*X2yCJh7yKUhFpe%oOHwB Date: Wed, 26 Jul 2023 21:45:49 -0300 Subject: [PATCH 2/8] text: Fix problem No Session --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 465c7324f..db23ce9f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.4.6", + "version": "1.4.7", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { From 127d5b97c405c67c5f66deb32c902e2c529ff859 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:36:18 -0300 Subject: [PATCH 3/8] fix: fixed error return bug --- CHANGELOG.md | 6 +++ src/exceptions/400.exception.ts | 1 + src/main.ts | 43 +++---------------- src/whatsapp/abstract/abstract.router.ts | 12 +++--- .../controllers/settings.controller.ts | 1 - src/whatsapp/repository/repository.manager.ts | 1 - 6 files changed, 20 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a5235d40..5013ceaf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.4.7 (2023-07-26 17:54) + +### Fixed + +* Fixed error return bug + # 1.4.6 (2023-07-26 17:54) ### Fixed diff --git a/src/exceptions/400.exception.ts b/src/exceptions/400.exception.ts index 833295c1a..a9256bcb1 100644 --- a/src/exceptions/400.exception.ts +++ b/src/exceptions/400.exception.ts @@ -2,6 +2,7 @@ import { HttpStatus } from '../whatsapp/routers/index.router'; export class BadRequestException { constructor(...objectError: any[]) { + console.log('BadRequestException', objectError); throw { status: HttpStatus.BAD_REQUEST, error: 'Bad Request', diff --git a/src/main.ts b/src/main.ts index b0d2e03eb..6e15f84de 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,18 +1,15 @@ -import 'express-async-errors'; - -// import * as Sentry from '@sentry/node'; import compression from 'compression'; +import { configService, Cors, HttpServer } from './config/env.config'; import cors from 'cors'; import express, { json, NextFunction, Request, Response, urlencoded } from 'express'; import { join } from 'path'; - -import { configService, Cors, HttpServer } from './config/env.config'; import { onUnexpectedError } from './config/error.config'; import { Logger } from './config/logger.config'; import { ROOT_DIR } from './config/path.config'; -import { ServerUP } from './utils/server-up'; -import { HttpStatus, router } from './whatsapp/routers/index.router'; import { waMonitor } from './whatsapp/whatsapp.module'; +import { HttpStatus, router } from './whatsapp/routers/index.router'; +import 'express-async-errors'; +import { ServerUP } from './utils/server-up'; function initWA() { waMonitor.loadInstance(); @@ -22,27 +19,6 @@ function bootstrap() { const logger = new Logger('SERVER'); const app = express(); - // Sentry.init({ - // dsn: '', - // integrations: [ - // // enable HTTP calls tracing - // new Sentry.Integrations.Http({ tracing: true }), - // // enable Express.js middleware tracing - // new Sentry.Integrations.Express({ app }), - // // Automatically instrument Node.js libraries and frameworks - // ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), - // ], - - // // Set tracesSampleRate to 1.0 to capture 100% - // // of transactions for performance monitoring. - // // We recommend adjusting this value in production - // tracesSampleRate: 1.0, - // }); - - // app.use(Sentry.Handlers.requestHandler()); - - // app.use(Sentry.Handlers.tracingHandler()); - app.use( cors({ origin(requestOrigin, callback) { @@ -67,18 +43,13 @@ function bootstrap() { app.use('/', router); - // app.use(Sentry.Handlers.errorHandler()); - - // app.use(function onError(err, req, res, next) { - // res.statusCode = 500; - // res.end(res.sentry + '\n'); - // }); - app.use( - (err: Error, req: Request, res: Response) => { + (err: Error, req: Request, res: Response, next: NextFunction) => { if (err) { return res.status(err['status'] || 500).json(err); } + + next(); }, (req: Request, res: Response, next: NextFunction) => { const { method, url } = req; diff --git a/src/whatsapp/abstract/abstract.router.ts b/src/whatsapp/abstract/abstract.router.ts index 136b946a5..170b06fbb 100644 --- a/src/whatsapp/abstract/abstract.router.ts +++ b/src/whatsapp/abstract/abstract.router.ts @@ -6,7 +6,7 @@ import { validate } from 'jsonschema'; import { Logger } from '../../config/logger.config'; import { BadRequestException } from '../../exceptions'; -import { GetParticipant, GroupInvite, GroupJid } from '../dto/group.dto'; +import { GetParticipant, GroupInvite } from '../dto/group.dto'; import { InstanceDto } from '../dto/instance.dto'; type DataValidate = { @@ -105,7 +105,7 @@ export abstract class RouterBroker { const body = request.body; let groupJid = body?.groupJid; - + if (!groupJid) { if (request.query?.groupJid) { groupJid = request.query.groupJid; @@ -113,15 +113,15 @@ export abstract class RouterBroker { throw new BadRequestException('The group id needs to be informed in the query', 'ex: "groupJid=120362@g.us"'); } } - + if (!groupJid.endsWith('@g.us')) { groupJid = groupJid + '@g.us'; } - + Object.assign(body, { - groupJid: groupJid + groupJid: groupJid, }); - + const ref = new ClassRef(); Object.assign(ref, body); diff --git a/src/whatsapp/controllers/settings.controller.ts b/src/whatsapp/controllers/settings.controller.ts index 32713b1f9..1a8baafc9 100644 --- a/src/whatsapp/controllers/settings.controller.ts +++ b/src/whatsapp/controllers/settings.controller.ts @@ -12,7 +12,6 @@ export class SettingsController { constructor(private readonly settingsService: SettingsService) {} public async createSettings(instance: InstanceDto, data: SettingsDto) { - logger.verbose('requested createSettings from ' + instance.instanceName + ' instance'); return this.settingsService.create(instance, data); diff --git a/src/whatsapp/repository/repository.manager.ts b/src/whatsapp/repository/repository.manager.ts index ae02849f0..e1292329a 100644 --- a/src/whatsapp/repository/repository.manager.ts +++ b/src/whatsapp/repository/repository.manager.ts @@ -109,7 +109,6 @@ export class RepositoryBroker { this.logger.verbose('creating temp dir: ' + tempDir); fs.mkdirSync(tempDir, { recursive: true }); } - } catch (error) { this.logger.error(error); } From 14f3f3d2ac930757a5b7d962df16d09809c8b55e Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:36:37 -0300 Subject: [PATCH 4/8] fix: fixed error return bug --- src/main.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 6e15f84de..a5b7fe8c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,15 +1,17 @@ +import 'express-async-errors'; + import compression from 'compression'; -import { configService, Cors, HttpServer } from './config/env.config'; import cors from 'cors'; import express, { json, NextFunction, Request, Response, urlencoded } from 'express'; import { join } from 'path'; + +import { configService, Cors, HttpServer } from './config/env.config'; import { onUnexpectedError } from './config/error.config'; import { Logger } from './config/logger.config'; import { ROOT_DIR } from './config/path.config'; -import { waMonitor } from './whatsapp/whatsapp.module'; -import { HttpStatus, router } from './whatsapp/routers/index.router'; -import 'express-async-errors'; import { ServerUP } from './utils/server-up'; +import { HttpStatus, router } from './whatsapp/routers/index.router'; +import { waMonitor } from './whatsapp/whatsapp.module'; function initWA() { waMonitor.loadInstance(); From d3fce5fc89b648b3774ca9846bb137462138f23c Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:45:02 -0300 Subject: [PATCH 5/8] fix: Fixed problem of getting message when deleting message in chatwoot --- CHANGELOG.md | 1 + src/whatsapp/services/chatwoot.service.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5013ceaf5..97ea5c42a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Fixed * Fixed error return bug +* Fixed problem of getting message when deleting message in chatwoot # 1.4.6 (2023-07-26 17:54) diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 0f2f46c8b..283a4198f 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -930,6 +930,7 @@ export class ChatwootService { } public async receiveWebhook(instance: InstanceDto, body: any) { + console.log(body); try { this.logger.verbose('receive webhook to chatwoot instance: ' + instance.instanceName); const client = await this.clientCw(instance); @@ -940,7 +941,7 @@ export class ChatwootService { } this.logger.verbose('check if is bot'); - if (!body?.conversation || body.private) return { message: 'bot' }; + if (!body?.conversation || body.private || body.event === 'message_updated') return { message: 'bot' }; this.logger.verbose('check if is group'); const chatId = From f74a7e87bdf4502c872311a43476537614fc0a88 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:48:03 -0300 Subject: [PATCH 6/8] version: 1.4.7 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ea5c42a..85974fac6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.4.7 (2023-07-26 17:54) +# 1.4.7 (2023-07-27 08:47) ### Fixed From f95d938fb677e0b5936903f57a392043d0eb16b9 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:51:58 -0300 Subject: [PATCH 7/8] fix: Adjusts in return errors --- src/exceptions/400.exception.ts | 1 - src/main.ts | 127 ++++++++++++---------- src/whatsapp/services/chatwoot.service.ts | 1 - 3 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/exceptions/400.exception.ts b/src/exceptions/400.exception.ts index a9256bcb1..833295c1a 100644 --- a/src/exceptions/400.exception.ts +++ b/src/exceptions/400.exception.ts @@ -2,7 +2,6 @@ import { HttpStatus } from '../whatsapp/routers/index.router'; export class BadRequestException { constructor(...objectError: any[]) { - console.log('BadRequestException', objectError); throw { status: HttpStatus.BAD_REQUEST, error: 'Bad Request', diff --git a/src/main.ts b/src/main.ts index a5b7fe8c8..8a14f7466 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,68 +14,77 @@ import { HttpStatus, router } from './whatsapp/routers/index.router'; import { waMonitor } from './whatsapp/whatsapp.module'; function initWA() { - waMonitor.loadInstance(); + waMonitor.loadInstance(); } function bootstrap() { - const logger = new Logger('SERVER'); - const app = express(); - - app.use( - cors({ - origin(requestOrigin, callback) { - const { ORIGIN } = configService.get('CORS'); - !requestOrigin ? (requestOrigin = '*') : undefined; - if (ORIGIN.indexOf(requestOrigin) !== -1) { - return callback(null, true); - } - return callback(new Error('Not allowed by CORS')); - }, - methods: [...configService.get('CORS').METHODS], - credentials: configService.get('CORS').CREDENTIALS, - }), - urlencoded({ extended: true, limit: '136mb' }), - json({ limit: '136mb' }), - compression(), - ); - - app.set('view engine', 'hbs'); - app.set('views', join(ROOT_DIR, 'views')); - app.use(express.static(join(ROOT_DIR, 'public'))); - - app.use('/', router); - - app.use( - (err: Error, req: Request, res: Response, next: NextFunction) => { - if (err) { - return res.status(err['status'] || 500).json(err); - } - - next(); - }, - (req: Request, res: Response, next: NextFunction) => { - const { method, url } = req; - - res.status(HttpStatus.NOT_FOUND).json({ - status: HttpStatus.NOT_FOUND, - message: `Cannot ${method.toUpperCase()} ${url}`, - error: 'Not Found', - }); - - next(); - }, - ); - - const httpServer = configService.get('SERVER'); - - ServerUP.app = app; - const server = ServerUP[httpServer.TYPE]; - - server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); - - initWA(); - - onUnexpectedError(); + const logger = new Logger('SERVER'); + const app = express(); + + app.use( + cors({ + origin(requestOrigin, callback) { + const { ORIGIN } = configService.get('CORS'); + !requestOrigin ? (requestOrigin = '*') : undefined; + if (ORIGIN.indexOf(requestOrigin) !== -1) { + return callback(null, true); + } + return callback(new Error('Not allowed by CORS')); + }, + methods: [...configService.get('CORS').METHODS], + credentials: configService.get('CORS').CREDENTIALS, + }), + urlencoded({ extended: true, limit: '136mb' }), + json({ limit: '136mb' }), + compression(), + ); + + app.set('view engine', 'hbs'); + app.set('views', join(ROOT_DIR, 'views')); + app.use(express.static(join(ROOT_DIR, 'public'))); + + app.use('/', router); + + app.use( + (err: Error, req: Request, res: Response, next: NextFunction) => { + if (err) { + return res.status(err['status'] || 500).json({ + status: 'ERROR', + error: err['error'] || 'Internal Server Error', + response: { + message: err['message'] || 'Internal Server Error', + }, + } + ); + } + + next(); + }, + (req: Request, res: Response, next: NextFunction) => { + const { method, url } = req; + + res.status(HttpStatus.NOT_FOUND).json({ + status: HttpStatus.NOT_FOUND, + error: 'Not Found', + response: { + message: `Cannot ${method.toUpperCase()} ${url}`, + }, + }); + + next(); + }, + ); + + const httpServer = configService.get('SERVER'); + + ServerUP.app = app; + const server = ServerUP[httpServer.TYPE]; + + server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); + + initWA(); + + onUnexpectedError(); } bootstrap(); diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 283a4198f..23efc66f7 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -930,7 +930,6 @@ export class ChatwootService { } public async receiveWebhook(instance: InstanceDto, body: any) { - console.log(body); try { this.logger.verbose('receive webhook to chatwoot instance: ' + instance.instanceName); const client = await this.clientCw(instance); From 9af7f679300ac0d62fb65a6d03b1abad235291aa Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 27 Jul 2023 08:52:45 -0300 Subject: [PATCH 8/8] version: 1.4.7 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85974fac6..358e9ccc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Fixed error return bug * Fixed problem of getting message when deleting message in chatwoot +* Change in error return pattern # 1.4.6 (2023-07-26 17:54)