diff --git a/src/exceptions/401.exception.ts b/src/exceptions/401.exception.ts index 72734d4e..d5424c71 100644 --- a/src/exceptions/401.exception.ts +++ b/src/exceptions/401.exception.ts @@ -5,7 +5,7 @@ export class UnauthorizedException { throw { status: HttpStatus.UNAUTHORIZED, error: 'Unauthorized', - message: objectError.length > 0 ? objectError : undefined, + message: objectError.length > 0 ? objectError : 'Unauthorized', }; } } diff --git a/src/main.ts b/src/main.ts index 8a14f746..1077d64d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,77 +14,76 @@ 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(); + 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.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.set('view engine', 'hbs'); + app.set('views', join(ROOT_DIR, 'views')); + app.use(express.static(join(ROOT_DIR, 'public'))); - app.use('/', router); + 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', - }, - } - ); - } + app.use( + (err: Error, req: Request, res: Response, next: NextFunction) => { + if (err) { + return res.status(err['status'] || 500).json({ + status: err['status'] || 500, + error: err['error'] || 'Internal Server Error', + response: { + message: err['message'] || 'Internal Server Error', + }, + }); + } - next(); + 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}`], }, - (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(); + }, + ); - next(); - }, - ); + const httpServer = configService.get('SERVER'); - const httpServer = configService.get('SERVER'); + ServerUP.app = app; + const server = ServerUP[httpServer.TYPE]; - ServerUP.app = app; - const server = ServerUP[httpServer.TYPE]; + server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); - server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); + initWA(); - initWA(); - - onUnexpectedError(); + onUnexpectedError(); } bootstrap(); diff --git a/src/whatsapp/abstract/abstract.router.ts b/src/whatsapp/abstract/abstract.router.ts index 170b06fb..7c603880 100644 --- a/src/whatsapp/abstract/abstract.router.ts +++ b/src/whatsapp/abstract/abstract.router.ts @@ -48,20 +48,21 @@ export abstract class RouterBroker { const v = schema ? validate(ref, schema) : { valid: true, errors: [] }; if (!v.valid) { - const message: any[] = v.errors.map(({ property, stack, schema }) => { + const message: any[] = v.errors.map(({ stack, schema }) => { let message: string; if (schema['description']) { message = schema['description']; } else { message = stack.replace('instance.', ''); } - return { - property: property.replace('instance.', ''), - message, - }; + return message; + // return { + // property: property.replace('instance.', ''), + // message, + // }; }); - logger.error([...message]); - throw new BadRequestException(...message); + logger.error(message); + throw new BadRequestException(message); } return await execute(instance, ref); diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 64867a2d..2b62af23 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -220,8 +220,8 @@ export class InstanceController { }, }; } catch (error) { - console.log(error); - return { error: true, message: error.toString() }; + this.logger.error(error.message[0]); + throw new BadRequestException(error.message[0]); } } @@ -269,7 +269,7 @@ export class InstanceController { this.logger.verbose('logging out instance: ' + instanceName); this.waMonitor.waInstances[instanceName]?.client?.ws?.close(); - return { error: false, message: 'Instance restarted' }; + return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } }; } catch (error) { this.logger.error(error); } @@ -310,7 +310,7 @@ export class InstanceController { this.logger.verbose('close connection instance: ' + instanceName); this.waMonitor.waInstances[instanceName]?.client?.ws?.close(); - return { error: false, message: 'Instance logged out' }; + return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } }; } catch (error) { throw new InternalServerErrorException(error.toString()); } @@ -329,13 +329,13 @@ export class InstanceController { await this.logout({ instanceName }); delete this.waMonitor.waInstances[instanceName]; - return { error: false, message: 'Instance deleted' }; + return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } }; } else { this.logger.verbose('deleting instance: ' + instanceName); delete this.waMonitor.waInstances[instanceName]; this.eventEmitter.emit('remove.instance', instanceName, 'inner'); - return { error: false, message: 'Instance deleted' }; + return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } }; } } catch (error) { throw new BadRequestException(error.toString()); diff --git a/src/whatsapp/routers/instance.router.ts b/src/whatsapp/routers/instance.router.ts index ae6d4066..20aa434c 100644 --- a/src/whatsapp/routers/instance.router.ts +++ b/src/whatsapp/routers/instance.router.ts @@ -161,7 +161,9 @@ export class InstanceRouter extends RouterBroker { if (db.ENABLED) { try { await dbserver.dropDatabase(); - return res.status(HttpStatus.CREATED).json({ error: false, message: 'Database deleted' }); + return res + .status(HttpStatus.CREATED) + .json({ status: 'SUCCESS', error: false, response: { message: 'database deleted' } }); } catch (error) { return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: true, message: error.message }); }