n8n-nodes-espocrm
Version:
n8n Community Node for EspoCRM
123 lines (122 loc) • 4.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContactOperations = void 0;
const BaseOperation_1 = require("./BaseOperation");
class ContactOperations extends BaseOperation_1.BaseOperation {
async execute(itemIndex) {
const operation = this.context.getNodeParameter('operation', itemIndex);
switch (operation) {
case 'get':
return await this.getContact(itemIndex);
case 'getAll':
return await this.getAllContacts(itemIndex);
case 'create':
return await this.createContact(itemIndex);
case 'update':
return await this.updateContact(itemIndex);
case 'delete':
return await this.deleteContact(itemIndex);
default:
throw new Error(`Unknown operation: ${operation}`);
}
}
async getContact(itemIndex) {
const contactId = this.context.getNodeParameter('contactId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.get(`Contact/${contactId}`);
}
catch (error) {
this.handleError(error, 'Get Contact');
}
}
async getAllContacts(itemIndex) {
const limit = this.context.getNodeParameter('limit', itemIndex, 10);
const searchQuery = this.context.getNodeParameter('searchQuery', itemIndex, '');
try {
await this.initializeClient();
const params = { maxSize: limit };
if (searchQuery.trim()) {
params.where = JSON.stringify([
{
type: 'or',
value: [
{
type: 'contains',
attribute: 'name',
value: searchQuery.trim()
},
{
type: 'contains',
attribute: 'emailAddress',
value: searchQuery.trim()
}
]
}
]);
}
return await this.apiClient.get('Contact', params);
}
catch (error) {
this.handleError(error, 'Get All Contacts');
}
}
async createContact(itemIndex) {
const firstName = this.context.getNodeParameter('firstName', itemIndex);
const lastName = this.context.getNodeParameter('lastName', itemIndex);
const emailAddress = this.context.getNodeParameter('emailAddress', itemIndex, '');
const phoneNumber = this.context.getNodeParameter('phoneNumber', itemIndex, '');
const accountId = this.context.getNodeParameter('accountId', itemIndex, '');
const title = this.context.getNodeParameter('title', itemIndex, '');
const contactData = {
firstName,
lastName,
emailAddress,
phoneNumber,
accountId,
title,
};
try {
await this.initializeClient();
return await this.apiClient.post('Contact', contactData);
}
catch (error) {
this.handleError(error, 'Create Contact');
}
}
async updateContact(itemIndex) {
const contactId = this.context.getNodeParameter('contactId', itemIndex);
const firstName = this.context.getNodeParameter('firstName', itemIndex);
const lastName = this.context.getNodeParameter('lastName', itemIndex);
const emailAddress = this.context.getNodeParameter('emailAddress', itemIndex, '');
const phoneNumber = this.context.getNodeParameter('phoneNumber', itemIndex, '');
const accountId = this.context.getNodeParameter('accountId', itemIndex, '');
const title = this.context.getNodeParameter('title', itemIndex, '');
const contactData = {
firstName,
lastName,
emailAddress,
phoneNumber,
accountId,
title,
};
try {
await this.initializeClient();
return await this.apiClient.put(`Contact/${contactId}`, contactData);
}
catch (error) {
this.handleError(error, 'Update Contact');
}
}
async deleteContact(itemIndex) {
const contactId = this.context.getNodeParameter('contactId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.delete(`Contact/${contactId}`);
}
catch (error) {
this.handleError(error, 'Delete Contact');
}
}
}
exports.ContactOperations = ContactOperations;