oex2003/evolution-api
0
1import 'express-async-errors';2 3import { GetParticipant, GroupInvite } from '@api/dto/group.dto';4import { InstanceDto } from '@api/dto/instance.dto';5import { Logger } from '@config/logger.config';6import { BadRequestException } from '@exceptions';7import { Request } from 'express';8import { JSONSchema7 } from 'json-schema';9import { validate } from 'jsonschema';10 11type DataValidate<T> = {12 request: Request;13 schema: JSONSchema7;14 ClassRef: any;15 execute: (instance: InstanceDto, data: T) => Promise<any>;16};17 18const logger = new Logger('Validate');19 20export abstract class RouterBroker {21 constructor() {}22 public routerPath(path: string, param = true) {23 let route = '/' + path;24 param ? (route += '/:instanceName') : null;25 26 return route;27 }28 29 public async dataValidate<T>(args: DataValidate<T>) {30 const { request, schema, ClassRef, execute } = args;31 32 const ref = new ClassRef();33 const body = request.body;34 const instance = request.params as unknown as InstanceDto;35 36 if (request?.query && Object.keys(request.query).length > 0) {37 Object.assign(instance, request.query);38 }39 40 if (request.originalUrl.includes('/instance/create')) {41 Object.assign(instance, body);42 }43 44 Object.assign(ref, body);45 46 const v = schema ? validate(ref, schema) : { valid: true, errors: [] };47 48 if (!v.valid) {49 const message: any[] = v.errors.map(({ stack, schema }) => {50 let message: string;51 if (schema['description']) {52 message = schema['description'];53 } else {54 message = stack.replace('instance.', '');55 }56 return message;57 });58 logger.error(message);59 throw new BadRequestException(message);60 }61 62 return await execute(instance, ref);63 }64 65 public async groupNoValidate<T>(args: DataValidate<T>) {66 const { request, ClassRef, schema, execute } = args;67 68 const instance = request.params as unknown as InstanceDto;69 70 const ref = new ClassRef();71 72 Object.assign(ref, request.body);73 74 const v = validate(ref, schema);75 76 if (!v.valid) {77 const message: any[] = v.errors.map(({ property, stack, schema }) => {78 let message: string;79 if (schema['description']) {80 message = schema['description'];81 } else {82 message = stack.replace('instance.', '');83 }84 return {85 property: property.replace('instance.', ''),86 message,87 };88 });89 logger.error([...message]);90 throw new BadRequestException(...message);91 }92 93 return await execute(instance, ref);94 }95 96 public async groupValidate<T>(args: DataValidate<T>) {97 const { request, ClassRef, schema, execute } = args;98 99 const instance = request.params as unknown as InstanceDto;100 const body = request.body;101 102 let groupJid = body?.groupJid;103 104 if (!groupJid) {105 if (request.query?.groupJid) {106 groupJid = request.query.groupJid;107 } else {108 throw new BadRequestException('The group id needs to be informed in the query', 'ex: "groupJid=120362@g.us"');109 }110 }111 112 if (!groupJid.endsWith('@g.us')) {113 groupJid = groupJid + '@g.us';114 }115 116 Object.assign(body, {117 groupJid: groupJid,118 });119 120 const ref = new ClassRef();121 122 Object.assign(ref, body);123 124 const v = validate(ref, schema);125 126 if (!v.valid) {127 const message: any[] = v.errors.map(({ property, stack, schema }) => {128 let message: string;129 if (schema['description']) {130 message = schema['description'];131 } else {132 message = stack.replace('instance.', '');133 }134 return {135 property: property.replace('instance.', ''),136 message,137 };138 });139 logger.error([...message]);140 throw new BadRequestException(...message);141 }142 143 return await execute(instance, ref);144 }145 146 public async inviteCodeValidate<T>(args: DataValidate<T>) {147 const { request, ClassRef, schema, execute } = args;148 149 const inviteCode = request.query as unknown as GroupInvite;150 151 if (!inviteCode?.inviteCode) {152 throw new BadRequestException(153 'The group invite code id needs to be informed in the query',154 'ex: "inviteCode=F1EX5QZxO181L3TMVP31gY" (Obtained from group join link)',155 );156 }157 158 const instance = request.params as unknown as InstanceDto;159 const body = request.body;160 161 const ref = new ClassRef();162 163 Object.assign(body, inviteCode);164 Object.assign(ref, body);165 166 const v = validate(ref, schema);167 168 if (!v.valid) {169 const message: any[] = v.errors.map(({ property, stack, schema }) => {170 let message: string;171 if (schema['description']) {172 message = schema['description'];173 } else {174 message = stack.replace('instance.', '');175 }176 return {177 property: property.replace('instance.', ''),178 message,179 };180 });181 logger.error([...message]);182 throw new BadRequestException(...message);183 }184 185 return await execute(instance, ref);186 }187 188 public async getParticipantsValidate<T>(args: DataValidate<T>) {189 const { request, ClassRef, schema, execute } = args;190 191 const getParticipants = request.query as unknown as GetParticipant;192 193 if (!getParticipants?.getParticipants) {194 throw new BadRequestException('The getParticipants needs to be informed in the query');195 }196 197 const instance = request.params as unknown as InstanceDto;198 const body = request.body;199 200 const ref = new ClassRef();201 202 Object.assign(body, getParticipants);203 Object.assign(ref, body);204 205 const v = validate(ref, schema);206 207 if (!v.valid) {208 const message: any[] = v.errors.map(({ property, stack, schema }) => {209 let message: string;210 if (schema['description']) {211 message = schema['description'];212 } else {213 message = stack.replace('instance.', '');214 }215 return {216 property: property.replace('instance.', ''),217 message,218 };219 });220 logger.error([...message]);221 throw new BadRequestException(...message);222 }223 224 return await execute(instance, ref);225 }226}227 