n8n-nodes-clint
Version:
Custom Clint CRM nodes for n8n
143 lines (142 loc) • 5.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClintDeal = void 0;
const axios_1 = __importDefault(require("axios"));
class ClintDeal {
constructor() {
this.description = {
displayName: 'Clint Deal',
name: 'clintDeal',
group: ['transform'],
version: 1,
description: 'Gerencia negócios (deals) da API Clint',
defaults: {
name: 'Clint Deal',
color: '#ff9900',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'clintApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{ name: 'List Deals', value: 'list' },
{ name: 'Get Deal', value: 'get' },
{ name: 'Create Deal', value: 'create' },
{ name: 'Update Deal', value: 'update' },
{ name: 'Delete Deal', value: 'delete' },
],
default: 'list',
},
{
displayName: 'Deal ID',
name: 'dealId',
type: 'string',
default: '',
displayOptions: {
show: { operation: ['get', 'update', 'delete'] },
},
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
displayOptions: { show: { operation: ['create', 'update'] } },
},
{
displayName: 'Contact ID',
name: 'contactId',
type: 'string',
default: '',
displayOptions: { show: { operation: ['create', 'update'] } },
},
{
displayName: 'Value',
name: 'value',
type: 'number',
default: 0,
displayOptions: { show: { operation: ['create', 'update'] } },
},
{
displayName: 'Group ID',
name: 'groupId',
type: 'string',
default: '',
displayOptions: { show: { operation: ['create', 'update'] } },
},
{
displayName: 'Origin ID',
name: 'originId',
type: 'string',
default: '',
displayOptions: { show: { operation: ['create', 'update'] } },
},
],
};
}
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 baseUrl = 'https://api.clint.digital/v1/deals';
const dealId = this.getNodeParameter('dealId', i, '');
const title = this.getNodeParameter('title', i, '');
const contactId = this.getNodeParameter('contactId', i, '');
const value = this.getNodeParameter('value', i, 0);
const groupId = this.getNodeParameter('groupId', i, '');
const originId = this.getNodeParameter('originId', i, '');
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}/${dealId}`, { headers });
break;
case 'create':
response = await axios_1.default.post(baseUrl, { title, contact_id: contactId, value, group_id: groupId, origin_id: originId }, { headers });
break;
case 'update':
response = await axios_1.default.post(`${baseUrl}/${dealId}`, { title, contact_id: contactId, value, group_id: groupId, origin_id: originId }, { headers });
break;
case 'delete':
response = await axios_1.default.delete(`${baseUrl}/${dealId}`, { headers });
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.ClintDeal = ClintDeal;