@poli-digital/n8n-nodes-poli
Version:
Nó para interagir com a API da Poli
389 lines • 18.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Poli = void 0;
const parameterUtils_1 = require("./utils/parameterUtils");
const ListContacts_operation_1 = require("./ListContacts.operation");
const CreateContact_operation_1 = require("./CreateContact.operation");
const UpdateContact_operation_1 = require("./UpdateContact.operation");
const AddTagToContact_operation_1 = require("./AddTagToContact.operation");
const ForwardContact_operation_1 = require("./ForwardContact.operation");
const ListApps_operation_1 = require("./ListApps.operation");
const CreateApp_operation_1 = require("./CreateApp.operation");
const GetMe_operation_1 = require("./GetMe.operation");
const ListAccounts_operation_1 = require("./ListAccounts.operation");
const ListChannels_operation_1 = require("./ListChannels.operation");
const GetChannel_operation_1 = require("./GetChannel.operation");
const ListChats_operation_1 = require("./ListChats.operation");
const SendMessageByContactId_operation_1 = require("./SendMessageByContactId.operation");
const SendMessageByPhoneNumber_operation_1 = require("./SendMessageByPhoneNumber.operation");
const SendMediaByContactId_operation_1 = require("./SendMediaByContactId.operation");
const ListMessagesFromContact_operation_1 = require("./ListMessagesFromContact.operation");
const ListTemplates_operation_1 = require("./ListTemplates.operation");
const SendTemplateByContactId_node_1 = require("./SendTemplateByContactId.node");
const SendTemplateByPhoneNumber_operation_1 = require("./SendTemplateByPhoneNumber.operation");
const ListTags_operation_1 = require("./ListTags.operation");
const CreateTag_operation_1 = require("./CreateTag.operation");
const ListUsers_operation_1 = require("./ListUsers.operation");
const GetUser_operation_1 = require("./GetUser.operation");
const ListTeams_operation_1 = require("./ListTeams.operation");
const GetTeam_operation_1 = require("./GetTeam.operation");
const ListWebhooks_operation_1 = require("./ListWebhooks.operation");
const CreateWebhook_operation_1 = require("./CreateWebhook.operation");
class Poli {
constructor() {
const nodes = {
app: {
list: new ListApps_operation_1.ListApps(),
create: new CreateApp_operation_1.CreateApp(),
getData: new GetMe_operation_1.GetMe(),
},
account: {
list: new ListAccounts_operation_1.ListAccounts(),
},
channel: {
list: new ListChannels_operation_1.ListChannels(),
get: new GetChannel_operation_1.GetChannel(),
},
chat: {
list: new ListChats_operation_1.ListChats(),
},
message: {
sendByContactId: new SendMessageByContactId_operation_1.SendMessageByContactId(),
sendByPhone: new SendMessageByPhoneNumber_operation_1.SendMessageByPhoneNumber(),
sendMediaByContactId: new SendMediaByContactId_operation_1.SendMediaByContactId(),
},
template: {
list: new ListTemplates_operation_1.ListTemplates(),
sendByContactId: new SendTemplateByContactId_node_1.SendTemplateByContactId(),
sendByPhone: new SendTemplateByPhoneNumber_operation_1.SendTemplateByPhoneNumber(),
},
contact: {
list: new ListContacts_operation_1.ListContacts(),
create: new CreateContact_operation_1.CreateContact(),
update: new UpdateContact_operation_1.UpdateContact(),
addTag: new AddTagToContact_operation_1.AddTagToContact(),
forward: new ForwardContact_operation_1.ForwardContact(),
listMessages: new ListMessagesFromContact_operation_1.ListMessagesFromContact(),
},
tag: {
list: new ListTags_operation_1.ListTags(),
create: new CreateTag_operation_1.CreateTag(),
},
user: {
list: new ListUsers_operation_1.ListUsers(),
get: new GetUser_operation_1.GetUser(),
},
team: {
list: new ListTeams_operation_1.ListTeams(),
get: new GetTeam_operation_1.GetTeam(),
},
webhook: {
list: new ListWebhooks_operation_1.ListWebhooks(),
create: new CreateWebhook_operation_1.CreateWebhook(),
},
};
const collectProperties = () => {
const baseProperties = [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{ name: 'App', value: 'app' },
{ name: 'Account', value: 'account' },
{ name: 'Channel', value: 'channel' },
{ name: 'Message', value: 'message' },
{ name: 'Template', value: 'template' },
{ name: 'Contact', value: 'contact' },
{ name: 'Chat', value: 'chat' },
{ name: 'Tag', value: 'tag' },
{ name: 'User', value: 'user' },
{ name: 'Team', value: 'team' },
{ name: 'Webhook', value: 'webhook' },
],
default: 'contact',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['app'] } },
options: [
{ name: 'List Apps', value: 'list', action: 'List Apps' },
{ name: 'Create App', value: 'create', action: 'Create App' },
{ name: 'Get App Data', value: 'getData', action: 'Get App Data' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['account'] } },
options: [{ name: 'List Accounts', value: 'list', action: 'List Accounts' }],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['channel'] } },
options: [
{ name: 'List Channels', value: 'list', action: 'List Channels' },
{ name: 'Get Channel', value: 'get', action: 'Get Channel' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['message'] } },
options: [
{ name: 'Send Message By Contact ID', value: 'sendByContactId', action: 'Send Message By Contact ID' },
{ name: 'Send Message By Phone', value: 'sendByPhone', action: 'Send Message By Phone' },
{ name: 'Send Media By Contact UUID', value: 'sendMediaByContactId', action: 'Send Media By Contact UUID' },
],
default: 'sendByContactId',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['template'] } },
options: [
{ name: 'List Templates', value: 'list', action: 'List Templates' },
{ name: 'Send Template Message By Contact ID', value: 'sendByContactId', action: 'Send Template Message By Contact ID' },
{ name: 'Send Template Message By Phone Number', value: 'sendByPhone', action: 'Send Template Message By Phone Number' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['contact'] } },
options: [
{ name: 'List and Search Contacts', value: 'list', action: 'List and Search Contacts' },
{ name: 'Create Contact', value: 'create', action: 'Create Contact' },
{ name: 'Update Contact', value: 'update', action: 'Update Contact' },
{ name: 'Add Tag to Contact', value: 'addTag', action: 'Add Tag to Contact' },
{ name: 'Forward Contact', value: 'forward', action: 'Forward Contact' },
{ name: 'List Messages from Contact ID', value: 'listMessages', action: 'List Messages from Contact ID' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['chat'] } },
options: [
{ name: 'List Chats', value: 'list', action: 'List Chats' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['tag'] } },
options: [
{ name: 'List Tags', value: 'list', action: 'List Tags' },
{ name: 'Create Tags', value: 'create', action: 'Create Tags' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['user'] } },
options: [
{ name: 'List and Search Users', value: 'list', action: 'List and Search Users' },
{ name: 'Get User', value: 'get', action: 'Get User' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['team'] } },
options: [
{ name: 'List Teams', value: 'list', action: 'List Teams' },
{ name: 'Get Team', value: 'get', action: 'Get Team' },
],
default: 'list',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: { show: { resource: ['webhook'] } },
options: [
{ name: 'List Webhooks', value: 'list', action: 'List Webhooks' },
{ name: 'Create Webhook', value: 'create', action: 'Create Webhook' },
],
default: 'list',
},
];
const allProperties = [...baseProperties];
Object.entries(nodes).forEach(([resource, operations]) => {
Object.entries(operations).forEach(([operation, nodeInstance]) => {
var _a;
const nodeProperties = ((_a = nodeInstance.description) === null || _a === void 0 ? void 0 : _a.properties) || [];
const specificProperties = nodeProperties.filter((prop) => prop.name !== 'resource' && prop.name !== 'operation');
specificProperties.forEach((prop) => {
var _a;
const modifiedProp = {
...prop,
displayOptions: {
show: {
resource: [resource],
operation: [operation],
...(((_a = prop.displayOptions) === null || _a === void 0 ? void 0 : _a.show) || {}),
},
},
};
allProperties.push(modifiedProp);
});
});
});
return allProperties;
};
this.description = {
displayName: 'Poli',
name: 'poli',
icon: 'file:poli.svg',
group: ['output'],
version: 1,
description: 'Node principal para interagir com a API da Poli',
defaults: { name: 'Poli' },
inputs: ['main'],
outputs: ['main'],
credentials: [{ name: 'poliApi', required: true }],
properties: collectProperties(),
};
}
async execute() {
let resource;
let operation;
try {
resource = (0, parameterUtils_1.getParameterSafe)(this, 'resource', 0, 'contact');
}
catch (error) {
console.warn('⚠️ Parâmetro "resource" não encontrado, usando fallback: "contact"');
resource = 'contact';
}
try {
operation = (0, parameterUtils_1.getParameterSafe)(this, 'operation', 0, 'list');
}
catch (error) {
console.warn(`⚠️ Parâmetro "operation" não encontrado para resource "${resource}", usando fallback baseado no resource`);
const defaultOperations = {
app: 'list',
account: 'list',
channel: 'list',
chat: 'list',
message: 'sendByContactId',
template: 'list',
contact: 'list',
tag: 'list',
user: 'list',
team: 'list',
webhook: 'list',
};
operation = defaultOperations[resource] || 'list';
}
if (!resource || typeof resource !== 'string') {
throw new Error('❌ Parâmetro "resource" é obrigatório e deve ser uma string válida');
}
if (!operation || typeof operation !== 'string') {
throw new Error('❌ Parâmetro "operation" é obrigatório e deve ser uma string válida');
}
const nodeMap = {
app: {
list: new ListApps_operation_1.ListApps(),
create: new CreateApp_operation_1.CreateApp(),
getData: new GetMe_operation_1.GetMe(),
},
account: {
list: new ListAccounts_operation_1.ListAccounts(),
},
channel: {
list: new ListChannels_operation_1.ListChannels(),
get: new GetChannel_operation_1.GetChannel(),
},
chat: {
list: new ListChats_operation_1.ListChats(),
},
message: {
sendByContactId: new SendMessageByContactId_operation_1.SendMessageByContactId(),
sendByPhone: new SendMessageByPhoneNumber_operation_1.SendMessageByPhoneNumber(),
sendMediaByContactId: new SendMediaByContactId_operation_1.SendMediaByContactId(),
},
template: {
list: new ListTemplates_operation_1.ListTemplates(),
sendByContactId: new SendTemplateByContactId_node_1.SendTemplateByContactId(),
sendByPhone: new SendTemplateByPhoneNumber_operation_1.SendTemplateByPhoneNumber(),
},
contact: {
list: new ListContacts_operation_1.ListContacts(),
create: new CreateContact_operation_1.CreateContact(),
update: new UpdateContact_operation_1.UpdateContact(),
addTag: new AddTagToContact_operation_1.AddTagToContact(),
forward: new ForwardContact_operation_1.ForwardContact(),
listMessages: new ListMessagesFromContact_operation_1.ListMessagesFromContact(),
},
tag: {
list: new ListTags_operation_1.ListTags(),
create: new CreateTag_operation_1.CreateTag(),
},
user: {
list: new ListUsers_operation_1.ListUsers(),
get: new GetUser_operation_1.GetUser(),
},
team: {
list: new ListTeams_operation_1.ListTeams(),
get: new GetTeam_operation_1.GetTeam(),
},
webhook: {
list: new ListWebhooks_operation_1.ListWebhooks(),
create: new CreateWebhook_operation_1.CreateWebhook(),
},
};
const resourceNodes = nodeMap[resource];
if (!resourceNodes) {
const availableResources = Object.keys(nodeMap).join(', ');
throw new Error(`❌ Resource '${resource}' não encontrado. Resources disponíveis: ${availableResources}`);
}
const targetNode = resourceNodes[operation];
if (!targetNode) {
const availableOperations = Object.keys(resourceNodes).join(', ');
throw new Error(`❌ Operação '${operation}' não encontrada para o resource '${resource}'. Operações disponíveis: ${availableOperations}`);
}
console.log(`✅ Executando: resource="${resource}", operation="${operation}"`);
try {
return await targetNode.execute.call(this);
}
catch (error) {
console.error(`❌ Erro na execução de ${resource}.${operation}:`, error);
throw new Error(`Erro na execução de ${resource}.${operation}: ${error.message || error}`);
}
}
}
exports.Poli = Poli;
//# sourceMappingURL=Poli.node.js.map