n8n-nodes-bitrix
Version:
n8n node for bitrix rest api
448 lines • 16.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fields = exports.operations = void 0;
exports.execute = execute;
const n8n_workflow_1 = require("n8n-workflow");
const n8n_workflow_2 = require("n8n-workflow");
const GenericFunctions_1 = require("./GenericFunctions");
exports.operations = [
{
displayName: 'Entity Type Name or ID',
name: 'entityTypeId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEntityTypes',
},
required: true,
default: '',
displayOptions: {
show: {
resource: ['crm.item'],
},
},
description: 'Identifier of the system or user-defined type. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
placeholder: 'e.g. 2',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['crm.company', 'crm.contact', 'crm.deal', 'crm.item', 'crm.lead'],
},
},
options: [
{
name: 'Create',
value: 'add',
description: 'Create a new entity record',
action: 'Create a record',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete an entity record by ID',
action: 'Delete a record',
},
{
name: 'Get',
value: 'get',
description: 'Retrieve a specific record by ID',
action: 'Get a record',
},
{
name: 'Get Fields Description',
value: 'fields',
description: 'Retrieve metadata about all available fields',
action: 'Get field metadata',
},
{
name: 'List',
value: 'list',
description: 'Retrieve filtered list of records',
action: 'List records',
},
{
name: 'Update',
value: 'update',
description: 'Modify an existing record by ID',
action: 'Update a record',
},
],
default: 'add',
},
];
exports.fields = [
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
operation: ['get', 'update', 'delete'],
},
},
description: 'The ID of the record to operate on',
placeholder: 'e.g. 12345',
},
{
displayName: 'Input Type',
name: 'inputType',
type: 'options',
options: [
{
name: 'Using Fields',
value: 'fields',
description: 'Fill fields individually',
},
{
name: 'Using JSON',
value: 'json',
description: 'Provide raw JSON data',
},
],
default: 'fields',
displayOptions: {
show: {
operation: ['add', 'update', 'list'],
},
},
description: 'Choose how to provide input data',
},
{
displayName: 'Fields',
name: 'fields',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
loadOptionsMethod: 'getEntityFields',
loadOptionsDependsOn: ['resource'],
},
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: ['add', 'update'],
inputType: ['fields'],
},
},
description: 'Field values to set',
options: [
{
displayName: 'Values',
name: 'values',
values: [
{
displayName: 'Name or ID',
name: 'name',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEntityFields',
loadOptionsDependsOn: ['resource'],
},
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Field value',
},
],
},
],
},
{
displayName: 'JSON Body',
name: 'jsonBody',
type: 'json',
default: '',
description: 'Provide complete data in JSON format',
displayOptions: {
show: {
operation: ['add', 'update', 'list'],
inputType: ['json'],
},
},
},
{
displayName: 'Parameters',
name: 'parameters',
type: 'collection',
placeholder: 'Add Parameter',
default: {},
displayOptions: {
show: {
operation: ['list'],
inputType: ['fields'],
},
},
options: [
{
displayName: 'Select Names or IDs',
name: 'select',
type: 'multiOptions',
default: [],
description: 'Fields to include in the response. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
typeOptions: {
loadOptionsMethod: 'getEntityFields',
loadOptionsDependsOn: ['resource'],
},
},
{
displayName: 'Filter Conditions',
name: 'filter',
type: 'fixedCollection',
placeholder: 'Add Filter',
typeOptions: {
multipleValues: true,
sortable: true,
},
default: {},
options: [
{
displayName: 'Field',
name: 'property',
values: [
{
displayName: 'Name or ID',
name: 'field',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEntityFields',
loadOptionsDependsOn: ['resource'],
},
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{ name: 'Contains', value: '%' },
{ name: 'Ends With', value: '%=' },
{ name: 'Equals', value: '=' },
{ name: 'Greater Than', value: '>' },
{ name: 'Greater Than Or Equal', value: '>=' },
{ name: 'In List', value: '@' },
{ name: 'Less Than', value: '<' },
{ name: 'Less Than Or Equal', value: '<=' },
{ name: 'Not Contains', value: '!%' },
{ name: 'Not Equals', value: '!=' },
{ name: 'Not In List', value: '!@' },
{ name: 'Starts With', value: '=%' },
],
default: '=',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Value to compare with',
},
],
},
],
},
{
displayName: 'Order',
name: 'order',
type: 'fixedCollection',
placeholder: 'Add Order',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
displayName: 'Property',
name: 'property',
values: [
{
displayName: 'Field Name or ID',
name: 'field',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEntityFields',
loadOptionsDependsOn: ['resource'],
},
default: '',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
},
{
displayName: 'Direction',
name: 'direction',
type: 'options',
options: [
{
name: 'ASC',
value: 'ASC',
},
{
name: 'DESC',
value: 'DESC',
},
],
default: 'ASC',
description: 'Sort direction',
},
],
},
],
},
{
displayName: 'Start',
name: 'start',
type: 'number',
default: 0,
},
]
},
{
displayName: 'Use Original UF Names',
name: 'useOriginalUfNames',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: ['crm.item'],
},
hide: {
operation: ['delete'],
},
},
description: 'Whether to use original UF_CRM prefix for custom field names',
},
];
async function execute(index) {
var _a, _b;
const operation = this.getNodeParameter('operation', index);
const resource = this.getNodeParameter('resource', index);
let requestMethod = 'POST';
let endpoint;
let body = {};
let qs = {};
let inputType;
endpoint = `${resource}.${operation}`;
if (resource === 'crm.item') {
const entityTypeId = this.getNodeParameter('entityTypeId', index);
if (!entityTypeId)
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Missing Entity Type ID parameter');
qs.entityTypeId = entityTypeId;
if (operation !== 'delete') {
qs.useOriginalUfNames = this.getNodeParameter('useOriginalUfNames', index, false) ? 'Y' : 'N';
}
}
switch (operation) {
case 'get':
case 'delete': {
requestMethod = 'GET';
const id = this.getNodeParameter('id', index);
if (!id)
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Missing ID parameter');
qs.id = id;
break;
}
case 'list': {
inputType = this.getNodeParameter('inputType', index);
if (inputType === 'json') {
const jsonBody = this.getNodeParameter('jsonBody', index, '{}');
try {
Object.assign(body, (0, n8n_workflow_1.jsonParse)(jsonBody));
}
catch (err) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Invalid JSON query format', {
itemIndex: index,
description: 'Please provide valid JSON. Error:' + err.message,
});
}
}
else {
requestMethod = 'GET';
const parameters = this.getNodeParameter('parameters', index, {});
if (parameters.select) {
qs.select = parameters.select;
}
if ((_a = parameters.filter) === null || _a === void 0 ? void 0 : _a.property) {
qs.filter = {};
parameters.filter.property.forEach(condition => {
const filterKey = `${condition.operation}${condition.field}`;
if (condition.operation === '@' || condition.operation === '!@') {
qs.filter[filterKey] =
condition.value.split(',').map(v => v.trim());
}
else {
qs.filter[filterKey] = condition.value;
}
});
}
if ((_b = parameters.order) === null || _b === void 0 ? void 0 : _b.property) {
qs.order = {};
parameters.order.property.forEach(condition => {
qs.order[condition.field] = condition.direction;
});
}
if (parameters.start !== undefined) {
qs.start = parameters.start;
}
}
break;
}
case 'add':
case 'update': {
inputType = this.getNodeParameter('inputType', index);
let fields = {};
if (inputType === 'json') {
const jsonBody = this.getNodeParameter('jsonBody', index, '{}');
try {
fields = (0, n8n_workflow_1.jsonParse)(jsonBody);
}
catch (err) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Invalid JSON data', {
itemIndex: index,
description: 'Please provide valid JSON.',
});
}
}
else {
const fieldsData = this.getNodeParameter('fields', index, {});
fields = {};
if (fieldsData.values) {
fieldsData.values.forEach(({ name, value }) => {
if (name)
fields[name] = value;
});
}
}
if (!fields || Object.keys(fields).length === 0) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'Fields cannot be empty');
}
body.fields = fields;
if (operation === 'update') {
const id = this.getNodeParameter('id', index);
if (!id)
throw new n8n_workflow_2.NodeOperationError(this.getNode(), 'ID parameter is required for update', { itemIndex: index });
qs.id = id;
}
break;
}
case 'fields': {
body = {};
break;
}
default:
throw new n8n_workflow_2.NodeOperationError(this.getNode(), `Unsupported operation: ${operation}`);
}
const responseData = await GenericFunctions_1.bitrixApiRequest.call(this, requestMethod, endpoint, body, qs);
return this.helpers.returnJsonArray(responseData);
}
//# sourceMappingURL=Description.js.map