From a95c843e7769639769134223a5bc2ac9148a55f5 Mon Sep 17 00:00:00 2001 From: ricael Date: Thu, 30 Oct 2025 16:28:53 -0300 Subject: [PATCH 1/3] feat(template): add edit/delete endpoints, DTOs and validation" --- src/api/controllers/template.controller.ts | 11 +++ src/api/dto/template.dto.ts | 13 +++ src/api/dto/templateDelete.dto.ts | 0 src/api/dto/templateEdit.dto.ts | 0 src/api/routes/template.router.ts | 36 ++++++- src/api/services/template.service.ts | 105 +++++++++++++++++++++ src/validate/templateDelete.schema.ts | 32 +++++++ src/validate/templateEdit.schema.ts | 35 +++++++ src/validate/validate.schema.ts | 2 + 9 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 src/api/dto/templateDelete.dto.ts create mode 100644 src/api/dto/templateEdit.dto.ts create mode 100644 src/validate/templateDelete.schema.ts create mode 100644 src/validate/templateEdit.schema.ts diff --git a/src/api/controllers/template.controller.ts b/src/api/controllers/template.controller.ts index d9b620457..52f8182b3 100644 --- a/src/api/controllers/template.controller.ts +++ b/src/api/controllers/template.controller.ts @@ -12,4 +12,15 @@ export class TemplateController { public async findTemplate(instance: InstanceDto) { return this.templateService.find(instance); } + + public async editTemplate( + instance: InstanceDto, + data: { templateId: string; category?: string; components?: any; allowCategoryChange?: boolean; ttl?: number }, + ) { + return this.templateService.edit(instance, data); + } + + public async deleteTemplate(instance: InstanceDto, data: { name: string; hsmId?: string }) { + return this.templateService.delete(instance, data); + } } diff --git a/src/api/dto/template.dto.ts b/src/api/dto/template.dto.ts index cec7d6c53..9b0339e88 100644 --- a/src/api/dto/template.dto.ts +++ b/src/api/dto/template.dto.ts @@ -6,3 +6,16 @@ export class TemplateDto { components: any; webhookUrl?: string; } + +export class TemplateEditDto { + templateId: string; + category?: 'AUTHENTICATION' | 'MARKETING' | 'UTILITY'; + allowCategoryChange?: boolean; + ttl?: number; + components?: any; +} + +export class TemplateDeleteDto { + name: string; + hsmId?: string; +} diff --git a/src/api/dto/templateDelete.dto.ts b/src/api/dto/templateDelete.dto.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/api/dto/templateEdit.dto.ts b/src/api/dto/templateEdit.dto.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/api/routes/template.router.ts b/src/api/routes/template.router.ts index 39d7b1283..249019a0c 100644 --- a/src/api/routes/template.router.ts +++ b/src/api/routes/template.router.ts @@ -1,9 +1,11 @@ import { RouterBroker } from '@api/abstract/abstract.router'; import { InstanceDto } from '@api/dto/instance.dto'; -import { TemplateDto } from '@api/dto/template.dto'; +import { TemplateDeleteDto, TemplateDto, TemplateEditDto } from '@api/dto/template.dto'; import { templateController } from '@api/server.module'; import { ConfigService } from '@config/env.config'; import { createMetaErrorResponse } from '@utils/errorResponse'; +import { templateDeleteSchema } from '@validate/templateDelete.schema'; +import { templateEditSchema } from '@validate/templateEdit.schema'; import { instanceSchema, templateSchema } from '@validate/validate.schema'; import { RequestHandler, Router } from 'express'; @@ -35,6 +37,38 @@ export class TemplateRouter extends RouterBroker { res.status(errorResponse.status).json(errorResponse); } }) + .post(this.routerPath('edit'), ...guards, async (req, res) => { + try { + const response = await this.dataValidate({ + request: req, + schema: templateEditSchema, + ClassRef: TemplateEditDto, + execute: (instance, data) => templateController.editTemplate(instance, data), + }); + + res.status(HttpStatus.OK).json(response); + } catch (error) { + console.error('Template edit error:', error); + const errorResponse = createMetaErrorResponse(error, 'template_edit'); + res.status(errorResponse.status).json(errorResponse); + } + }) + .delete(this.routerPath('delete'), ...guards, async (req, res) => { + try { + const response = await this.dataValidate({ + request: req, + schema: templateDeleteSchema, + ClassRef: TemplateDeleteDto, + execute: (instance, data) => templateController.deleteTemplate(instance, data), + }); + + res.status(HttpStatus.OK).json(response); + } catch (error) { + console.error('Template delete error:', error); + const errorResponse = createMetaErrorResponse(error, 'template_delete'); + res.status(errorResponse.status).json(errorResponse); + } + }) .get(this.routerPath('find'), ...guards, async (req, res) => { try { const response = await this.dataValidate({ diff --git a/src/api/services/template.service.ts b/src/api/services/template.service.ts index 8c36feab5..5bf0ba5d6 100644 --- a/src/api/services/template.service.ts +++ b/src/api/services/template.service.ts @@ -88,6 +88,77 @@ export class TemplateService { } } + public async edit( + instance: InstanceDto, + data: { templateId: string; category?: string; components?: any; allowCategoryChange?: boolean; ttl?: number }, + ) { + const getInstance = await this.waMonitor.waInstances[instance.instanceName].instance; + if (!getInstance) { + throw new Error('Instance not found'); + } + + this.businessId = getInstance.businessId; + this.token = getInstance.token; + + const payload: Record = {}; + if (typeof data.category === 'string') payload.category = data.category; + if (typeof data.allowCategoryChange === 'boolean') payload.allow_category_change = data.allowCategoryChange; + if (typeof data.ttl === 'number') payload.time_to_live = data.ttl; + if (data.components) payload.components = data.components; + + const response = await this.requestEditTemplate(data.templateId, payload); + + if (!response || response.error) { + if (response && response.error) { + const metaError = new Error(response.error.message || 'WhatsApp API Error'); + (metaError as any).template = response.error; + throw metaError; + } + throw new Error('Error to edit template'); + } + + return response; + } + + public async delete(instance: InstanceDto, data: { name: string; hsmId?: string }) { + const getInstance = await this.waMonitor.waInstances[instance.instanceName].instance; + if (!getInstance) { + throw new Error('Instance not found'); + } + + this.businessId = getInstance.businessId; + this.token = getInstance.token; + + const response = await this.requestDeleteTemplate({ name: data.name, hsm_id: data.hsmId }); + + if (!response || response.error) { + if (response && response.error) { + const metaError = new Error(response.error.message || 'WhatsApp API Error'); + (metaError as any).template = response.error; + throw metaError; + } + throw new Error('Error to delete template'); + } + + try { + // Best-effort local cleanup of stored template metadata + await this.prismaRepository.template.deleteMany({ + where: { + OR: [ + { name: data.name, instanceId: getInstance.id }, + data.hsmId ? { templateId: data.hsmId, instanceId: getInstance.id } : undefined, + ].filter(Boolean) as any, + }, + }); + } catch (err) { + this.logger.warn( + `Failed to cleanup local template records after delete: ${(err as Error)?.message || String(err)}`, + ); + } + + return response; + } + private async requestTemplate(data: any, method: string) { try { let urlServer = this.configService.get('WA_BUSINESS').URL; @@ -116,4 +187,38 @@ export class TemplateService { throw new Error(`Connection error: ${e.message}`); } } + + private async requestEditTemplate(templateId: string, data: any) { + try { + let urlServer = this.configService.get('WA_BUSINESS').URL; + const version = this.configService.get('WA_BUSINESS').VERSION; + urlServer = `${urlServer}/${version}/${templateId}`; + const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` }; + const result = await axios.post(urlServer, data, { headers }); + return result.data; + } catch (e) { + this.logger.error( + 'WhatsApp API request error: ' + (e.response?.data ? JSON.stringify(e.response?.data) : e.message), + ); + if (e.response?.data) return e.response.data; + throw new Error(`Connection error: ${e.message}`); + } + } + + private async requestDeleteTemplate(params: { name: string; hsm_id?: string }) { + try { + let urlServer = this.configService.get('WA_BUSINESS').URL; + const version = this.configService.get('WA_BUSINESS').VERSION; + urlServer = `${urlServer}/${version}/${this.businessId}/message_templates`; + const headers = { Authorization: `Bearer ${this.token}` }; + const result = await axios.delete(urlServer, { headers, params }); + return result.data; + } catch (e) { + this.logger.error( + 'WhatsApp API request error: ' + (e.response?.data ? JSON.stringify(e.response?.data) : e.message), + ); + if (e.response?.data) return e.response.data; + throw new Error(`Connection error: ${e.message}`); + } + } } diff --git a/src/validate/templateDelete.schema.ts b/src/validate/templateDelete.schema.ts new file mode 100644 index 000000000..70b3aefff --- /dev/null +++ b/src/validate/templateDelete.schema.ts @@ -0,0 +1,32 @@ +import { JSONSchema7 } from 'json-schema'; +import { v4 } from 'uuid'; + +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties: Record = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + } as JSONSchema7; +}; + +export const templateDeleteSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + name: { type: 'string' }, + hsmId: { type: 'string' }, + }, + required: ['name'], + ...isNotEmpty('name'), +}; diff --git a/src/validate/templateEdit.schema.ts b/src/validate/templateEdit.schema.ts new file mode 100644 index 000000000..0b03e32d5 --- /dev/null +++ b/src/validate/templateEdit.schema.ts @@ -0,0 +1,35 @@ +import { JSONSchema7 } from 'json-schema'; +import { v4 } from 'uuid'; + +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties: Record = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + } as JSONSchema7; +}; + +export const templateEditSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + templateId: { type: 'string' }, + category: { type: 'string', enum: ['AUTHENTICATION', 'MARKETING', 'UTILITY'] }, + allowCategoryChange: { type: 'boolean' }, + ttl: { type: 'number' }, + components: { type: 'array' }, + }, + required: ['templateId'], + ...isNotEmpty('templateId'), +}; diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 4577eae3f..69e0090d8 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -8,5 +8,7 @@ export * from './message.schema'; export * from './proxy.schema'; export * from './settings.schema'; export * from './template.schema'; +export * from './templateDelete.schema'; +export * from './templateEdit.schema'; export * from '@api/integrations/chatbot/chatbot.schema'; export * from '@api/integrations/event/event.schema'; From 3b0432dd9fd202173abe4d7a3f3768d735e24c06 Mon Sep 17 00:00:00 2001 From: ricael Date: Thu, 30 Oct 2025 16:36:01 -0300 Subject: [PATCH 2/3] refactor(utils): lint --- .../whatsapp/whatsapp.baileys.service.ts | 2 +- src/utils/makeProxyAgent.ts | 35 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 2636adbda..937841cd5 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2'; import { Instance, Message } from '@prisma/client'; import { createJid } from '@utils/createJid'; import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion'; -import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent'; +import { makeProxyAgent, makeProxyAgentUndici } from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; import { status } from '@utils/renderStatus'; import { sendTelemetry } from '@utils/sendTelemetry'; diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index e882876ef..ac64b9dec 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,7 +1,6 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; - -import { ProxyAgent } from 'undici' +import { ProxyAgent } from 'undici'; type Proxy = { host: string; @@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | } export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { - let proxyUrl: string - let protocol: string + let proxyUrl: string; + let protocol: string; if (typeof proxy === 'string') { - const url = new URL(proxy) - protocol = url.protocol.replace(':', '') - proxyUrl = proxy + const url = new URL(proxy); + protocol = url.protocol.replace(':', ''); + proxyUrl = proxy; } else { - const { host, password, port, protocol: proto, username } = proxy - protocol = (proto || 'http').replace(':', '') + const { host, password, port, protocol: proto, username } = proxy; + protocol = (proto || 'http').replace(':', ''); if (protocol === 'socks') { - protocol = 'socks5' + protocol = 'socks5'; } - const auth = username && password ? `${username}:${password}@` : '' - proxyUrl = `${protocol}://${auth}${host}:${port}` + const auth = username && password ? `${username}:${password}@` : ''; + proxyUrl = `${protocol}://${auth}${host}:${port}`; } - const PROXY_HTTP_PROTOCOL = 'http' - const PROXY_HTTPS_PROTOCOL = 'https' - const PROXY_SOCKS4_PROTOCOL = 'socks4' - const PROXY_SOCKS5_PROTOCOL = 'socks5' + const PROXY_HTTP_PROTOCOL = 'http'; + const PROXY_HTTPS_PROTOCOL = 'https'; + const PROXY_SOCKS4_PROTOCOL = 'socks4'; + const PROXY_SOCKS5_PROTOCOL = 'socks5'; switch (protocol) { case PROXY_HTTP_PROTOCOL: case PROXY_HTTPS_PROTOCOL: case PROXY_SOCKS4_PROTOCOL: case PROXY_SOCKS5_PROTOCOL: - return new ProxyAgent(proxyUrl) + return new ProxyAgent(proxyUrl); default: - throw new Error(`Unsupported proxy protocol: ${protocol}`) + throw new Error(`Unsupported proxy protocol: ${protocol}`); } } From 1aaad541adb48ba2a6653052c6019cce1988c39f Mon Sep 17 00:00:00 2001 From: ricael Date: Thu, 30 Oct 2025 16:59:30 -0300 Subject: [PATCH 3/3] chore(dto): remove unused template edit/delete DTOs --- src/api/dto/templateDelete.dto.ts | 0 src/api/dto/templateEdit.dto.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/api/dto/templateDelete.dto.ts delete mode 100644 src/api/dto/templateEdit.dto.ts diff --git a/src/api/dto/templateDelete.dto.ts b/src/api/dto/templateDelete.dto.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/api/dto/templateEdit.dto.ts b/src/api/dto/templateEdit.dto.ts deleted file mode 100644 index e69de29bb..000000000