n8n-nodes-clint
Version:
Custom Clint CRM nodes for n8n
177 lines (176 loc) • 7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClintContact = void 0;
const axios_1 = __importDefault(require("axios"));
class ClintContact {
constructor() {
this.description = {
displayName: 'Clint Contact',
name: 'clintContact',
group: ['transform'],
version: 1,
description: 'Gerencia contatos da API Clint',
defaults: {
name: 'Clint Contact',
color: '#00aaff',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'clintApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{ name: 'List Contacts', value: 'list' },
{ name: 'Get Contact', value: 'get' },
{ name: 'Create Contact', value: 'create' },
{ name: 'Update Contact', value: 'update' },
{ name: 'Delete Contact', value: 'delete' },
{ name: 'Add Tags', value: 'addTags' },
{ name: 'Remove Tags', value: 'removeTags' },
],
default: 'list',
},
{
displayName: 'Contact ID',
name: 'contactId',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['get', 'update', 'delete', 'addTags', 'removeTags'],
},
},
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['create', 'update'],
},
},
},
{
displayName: 'DDI',
name: 'ddi',
type: 'string',
default: '55',
displayOptions: {
show: {
operation: ['create', 'update'],
},
},
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['create', 'update'],
},
},
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
displayOptions: {
show: {
operation: ['create', 'update'],
},
},
},
{
displayName: 'Tag IDs',
name: 'tags',
type: 'string',
default: '',
description: 'IDs separados por vírgula',
displayOptions: {
show: {
operation: ['addTags', 'removeTags'],
},
},
},
],
};
}
async execute() {
var _a, _b;
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('clintApi');
const token = credentials.apiToken;
const headers = {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
};
for (let i = 0; i < items.length; i++) {
const operation = this.getNodeParameter('operation', i);
const contactId = this.getNodeParameter('contactId', i, '');
const name = this.getNodeParameter('name', i, '');
const ddi = this.getNodeParameter('ddi', i, '');
const phone = this.getNodeParameter('phone', i, '');
const email = this.getNodeParameter('email', i, '');
const tags = this.getNodeParameter('tags', i, '');
const baseUrl = 'https://api.clint.digital/v1/contacts';
try {
let response;
switch (operation) {
case 'list':
response = await axios_1.default.get(baseUrl, { headers });
break;
case 'get':
response = await axios_1.default.get(`${baseUrl}/${contactId}`, { headers });
break;
case 'create':
response = await axios_1.default.post(baseUrl, { name, ddi, phone, email }, { headers });
break;
case 'update':
response = await axios_1.default.post(`${baseUrl}/${contactId}`, { name, ddi, phone, email }, { headers });
break;
case 'delete':
response = await axios_1.default.delete(`${baseUrl}/${contactId}`, { headers });
break;
case 'addTags':
response = await axios_1.default.post(`${baseUrl}/${contactId}/tags`, { tags: tags.split(',').map(tag => tag.trim()) }, { headers });
break;
case 'removeTags':
response = await axios_1.default.delete(`${baseUrl}/${contactId}/tags`, {
headers,
data: { tags: tags.split(',').map(tag => tag.trim()) },
});
break;
}
returnData.push({ json: (_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : { success: true } });
}
catch (error) {
returnData.push({
json: {
error: error.message,
details: (_b = error.response) === null || _b === void 0 ? void 0 : _b.data,
},
});
}
}
return [returnData];
}
}
exports.ClintContact = ClintContact;