diff --git a/src/api/dto/sendMessage.dto.ts b/src/api/dto/sendMessage.dto.ts index 1a6b4db5..cfd9d47d 100644 --- a/src/api/dto/sendMessage.dto.ts +++ b/src/api/dto/sendMessage.dto.ts @@ -6,8 +6,8 @@ export class Quoted { } export class Mentions { - everyOne: boolean; - mentioned: string[]; + everyOne?: boolean; + mentioned?: string[]; } export class Options { @@ -44,8 +44,8 @@ export class Metadata { delay?: number; quoted?: Quoted; linkPreview?: boolean; - everyOne: boolean; - mentioned: string[]; + everyOne?: boolean; + mentioned?: string[]; encoding?: boolean; } diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index 1a3358c1..0a89fdaf 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -972,14 +972,9 @@ export class ChatwootService { if (type === 'audio') { const data: SendAudioDto = { number: number, - audioMessage: { - audio: media, - }, - options: { - delay: 1200, - presence: 'recording', - ...options, - }, + audio: media, + delay: 1200, + quoted: options?.quoted, }; const messageSent = await waInstance?.audioWhatsapp(data, true); @@ -993,20 +988,15 @@ export class ChatwootService { const data: SendMediaDto = { number: number, - mediaMessage: { - mediatype: type as any, - fileName: fileName, - media: media, - }, - options: { - delay: 1200, - presence: 'composing', - ...options, - }, + mediatype: type as any, + fileName: fileName, + media: media, + delay: 1200, + quoted: options?.quoted, }; if (caption) { - data.mediaMessage.caption = caption; + data.caption = caption; } const messageSent = await waInstance?.mediaMessage(data, true); @@ -1232,14 +1222,9 @@ export class ChatwootService { } else { const data: SendTextDto = { number: chatId, - textMessage: { - text: formatText, - }, - options: { - delay: 1200, - presence: 'composing', - quoted: await this.getQuotedMessage(body, instance), - }, + text: formatText, + delay: 1200, + quoted: await this.getQuotedMessage(body, instance), }; let messageSent: any; @@ -1350,13 +1335,8 @@ export class ChatwootService { if (body.message_type === 'template' && body.event === 'message_created') { const data: SendTextDto = { number: chatId, - textMessage: { - text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'), - }, - options: { - delay: 1200, - presence: 'composing', - }, + text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'), + delay: 1200, }; await waInstance?.textMessage(data); diff --git a/src/api/integrations/typebot/services/typebot.service.ts b/src/api/integrations/typebot/services/typebot.service.ts index de2205c9..37b9ac66 100644 --- a/src/api/integrations/typebot/services/typebot.service.ts +++ b/src/api/integrations/typebot/services/typebot.service.ts @@ -683,13 +683,8 @@ export class TypebotService { if (unknownMessage) { this.waMonitor.waInstances[instance.instanceName].textMessage({ number: remoteJid.split('@')[0], - options: { - delay: delayMessage || 1000, - presence: 'composing', - }, - textMessage: { - text: unknownMessage, - }, + delay: delayMessage || 1000, + text: unknownMessage, }); } return; @@ -782,13 +777,8 @@ export class TypebotService { if (unknownMessage) { this.waMonitor.waInstances[instance.instanceName].textMessage({ number: remoteJid.split('@')[0], - options: { - delay: delayMessage || 1000, - presence: 'composing', - }, - textMessage: { - text: unknownMessage, - }, + delay: delayMessage || 1000, + text: unknownMessage, }); } return; @@ -874,13 +864,8 @@ export class TypebotService { if (unknownMessage) { this.waMonitor.waInstances[instance.instanceName].textMessage({ number: remoteJid.split('@')[0], - options: { - delay: delayMessage || 1000, - presence: 'composing', - }, - textMessage: { - text: unknownMessage, - }, + delay: delayMessage || 1000, + text: unknownMessage, }); } return; diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index 00c8c544..df5d107d 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -838,9 +838,18 @@ export class BusinessStartupService extends ChannelStartupService { const res = await this.sendMessageWithTyping( data.number, { - conversation: data.textMessage.text, + conversation: data.text, + }, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, }, - data?.options, isChatwoot, ); return res; @@ -915,9 +924,23 @@ export class BusinessStartupService extends ChannelStartupService { } public async mediaMessage(data: SendMediaDto, isChatwoot = false) { - const message = await this.prepareMediaMessage(data.mediaMessage); + const message = await this.prepareMediaMessage(data); - return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot); + return await this.sendMessageWithTyping( + data.number, + { ...message }, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, + isChatwoot, + ); } public async processAudio(audio: string, number: string) { @@ -949,22 +972,27 @@ export class BusinessStartupService extends ChannelStartupService { } public async audioWhatsapp(data: SendAudioDto, isChatwoot = false) { - const message = await this.processAudio(data.audioMessage.audio, data.number); + const message = await this.processAudio(data.audio, data.number); - return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot); + return await this.sendMessageWithTyping( + data.number, + { ...message }, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, + isChatwoot, + ); } public async buttonMessage(data: SendButtonDto) { const embeddedMedia: any = {}; - let mediatype = 'TEXT'; - - if (data.buttonMessage?.mediaMessage) { - mediatype = data.buttonMessage.mediaMessage?.mediatype.toUpperCase() ?? 'TEXT'; - embeddedMedia.mediaKey = mediatype.toLowerCase() + 'Message'; - const generate = await this.prepareMediaMessage(data.buttonMessage.mediaMessage); - embeddedMedia.message = generate.message[embeddedMedia.mediaKey]; - embeddedMedia.contentText = `*${data.buttonMessage.title}*\n\n${data.buttonMessage.description}`; - } const btnItems = { text: data.buttonMessage.buttons.map((btn) => btn.buttonText), @@ -990,7 +1018,16 @@ export class BusinessStartupService extends ChannelStartupService { }), [embeddedMedia?.mediaKey]: embeddedMedia?.message, }, - data?.options, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, ); } @@ -999,19 +1036,28 @@ export class BusinessStartupService extends ChannelStartupService { data.number, { locationMessage: { - degreesLatitude: data.locationMessage.latitude, - degreesLongitude: data.locationMessage.longitude, - name: data.locationMessage?.name, - address: data.locationMessage?.address, + degreesLatitude: data.latitude, + degreesLongitude: data.longitude, + name: data?.name, + address: data?.address, + }, + }, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, }, }, - data?.options, ); } public async listMessage(data: SendListDto) { const sectionsItems = { - title: data.listMessage.sections.map((list) => list.title), + title: data.sections.map((list) => list.title), }; if (!arrayUnique(sectionsItems.title)) { @@ -1021,11 +1067,11 @@ export class BusinessStartupService extends ChannelStartupService { return await this.sendMessageWithTyping( data.number, { - title: data.listMessage.title, - text: data.listMessage.description, - footerText: data.listMessage?.footerText, - buttonText: data.listMessage?.buttonText, - sections: data.listMessage.sections.map((section) => { + title: data.title, + text: data.description, + footerText: data?.footerText, + buttonText: data?.buttonText, + sections: data.sections.map((section) => { return { title: section.title, rows: section.rows.map((row) => { @@ -1038,7 +1084,16 @@ export class BusinessStartupService extends ChannelStartupService { }; }), }, - data?.options, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, ); } @@ -1052,7 +1107,16 @@ export class BusinessStartupService extends ChannelStartupService { components: data.components, }, }, - data?.options, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, isChatwoot, ); return res; @@ -1085,15 +1149,15 @@ export class BusinessStartupService extends ChannelStartupService { return result; }; - if (data.contactMessage.length === 1) { - message.contactMessage = { - displayName: data.contactMessage[0].fullName, - vcard: vcard(data.contactMessage[0]), + if (data.contact.length === 1) { + message.contact = { + displayName: data.contact[0].fullName, + vcard: vcard(data.contact[0]), }; } else { message.contactsArrayMessage = { - displayName: `${data.contactMessage.length} contacts`, - contacts: data.contactMessage.map((contact) => { + displayName: `${data.contact.length} contacts`, + contacts: data.contact.map((contact) => { return { displayName: contact.fullName, vcard: vcard(contact), @@ -1104,7 +1168,7 @@ export class BusinessStartupService extends ChannelStartupService { return await this.sendMessageWithTyping( data.number, { - contacts: data.contactMessage.map((contact) => { + contacts: data.contact.map((contact) => { return { name: { formatted_name: contact.fullName, first_name: contact.fullName }, phones: [{ phone: contact.phoneNumber }], @@ -1115,15 +1179,24 @@ export class BusinessStartupService extends ChannelStartupService { }), message, }, - data?.options, + { + delay: data?.delay, + presence: 'composing', + quoted: data?.quoted, + linkPreview: data?.linkPreview, + mentions: { + everyOne: data?.everyOne, + mentioned: data?.mentioned, + }, + }, ); } public async reactionMessage(data: SendReactionDto) { - return await this.sendMessageWithTyping(data.reactionMessage.key.remoteJid, { + return await this.sendMessageWithTyping(data.key.remoteJid, { reactionMessage: { - key: data.reactionMessage.key, - text: data.reactionMessage.reaction, + key: data.key, + text: data.reaction, }, }); } diff --git a/src/validate/chat.schema.ts b/src/validate/chat.schema.ts index 7ae927a3..5ef1c93d 100644 --- a/src/validate/chat.schema.ts +++ b/src/validate/chat.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; const numberDefinition: JSONSchema7Definition = { type: 'string', diff --git a/src/validate/group.schema.ts b/src/validate/group.schema.ts index 2e8f35f0..8da188d8 100644 --- a/src/validate/group.schema.ts +++ b/src/validate/group.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const createGroupSchema: JSONSchema7 = { $id: v4(), diff --git a/src/validate/instance.schema.ts b/src/validate/instance.schema.ts index 7422e270..41b13570 100644 --- a/src/validate/instance.schema.ts +++ b/src/validate/instance.schema.ts @@ -2,7 +2,26 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; import { Integration } from '../api/types/wa.types'; -import { Events, isNotEmpty } from './validate.schema'; +import { Events } from './validate.schema'; + +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const instanceSchema: JSONSchema7 = { $id: v4(), diff --git a/src/validate/label.schema.ts b/src/validate/label.schema.ts index c7bfae37..eb5dc5a5 100644 --- a/src/validate/label.schema.ts +++ b/src/validate/label.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; const numberDefinition: JSONSchema7Definition = { type: 'string', diff --git a/src/validate/message.schema.ts b/src/validate/message.schema.ts index 0e2de95c..1f7548b1 100644 --- a/src/validate/message.schema.ts +++ b/src/validate/message.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; const numberDefinition: JSONSchema7Definition = { type: 'string', diff --git a/src/validate/proxy.schema.ts b/src/validate/proxy.schema.ts index 9985c070..564b42d5 100644 --- a/src/validate/proxy.schema.ts +++ b/src/validate/proxy.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const proxySchema: JSONSchema7 = { $id: v4(), diff --git a/src/validate/settings.schema.ts b/src/validate/settings.schema.ts index 98b1aa93..b5c05601 100644 --- a/src/validate/settings.schema.ts +++ b/src/validate/settings.schema.ts @@ -1,7 +1,24 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -import { isNotEmpty } from './validate.schema'; +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const settingsSchema: JSONSchema7 = { $id: v4(), diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index b2771e1f..263b2362 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -1,5 +1,3 @@ -import { JSONSchema7 } from 'json-schema'; - // Integrations Schema // TODO: rever todas as integrações e garantir o funcionamento perfeito export * from '../api/integrations/chatwoot/validate/chatwoot.schema'; @@ -18,25 +16,6 @@ export * from './settings.schema'; export * from './webhook.schema'; export * from './websocket.schema'; -export const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { - const properties = {}; - propertyNames.forEach( - (property) => - (properties[property] = { - minLength: 1, - description: `The "${property}" cannot be empty`, - }), - ); - return { - if: { - propertyNames: { - enum: [...propertyNames], - }, - }, - then: { properties }, - }; -}; - export const Events = [ 'APPLICATION_STARTUP', 'QRCODE_UPDATED', diff --git a/src/validate/webhook.schema.ts b/src/validate/webhook.schema.ts index 0ecd1df8..05e5d742 100644 --- a/src/validate/webhook.schema.ts +++ b/src/validate/webhook.schema.ts @@ -1,7 +1,26 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -import { Events, isNotEmpty } from './validate.schema'; +import { Events } from './validate.schema'; + +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const webhookSchema: JSONSchema7 = { $id: v4(), diff --git a/src/validate/websocket.schema.ts b/src/validate/websocket.schema.ts index 3cdb2869..871d18ab 100644 --- a/src/validate/websocket.schema.ts +++ b/src/validate/websocket.schema.ts @@ -1,7 +1,26 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -import { Events, isNotEmpty } from './validate.schema'; +import { Events } from './validate.schema'; + +const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { + const properties = {}; + propertyNames.forEach( + (property) => + (properties[property] = { + minLength: 1, + description: `The "${property}" cannot be empty`, + }), + ); + return { + if: { + propertyNames: { + enum: [...propertyNames], + }, + }, + then: { properties }, + }; +}; export const websocketSchema: JSONSchema7 = { $id: v4(),