n8n-nodes-espocrm
Version:
n8n Community Node for EspoCRM
128 lines (127 loc) • 4.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountOperations = void 0;
const BaseOperation_1 = require("./BaseOperation");
class AccountOperations extends BaseOperation_1.BaseOperation {
async execute(itemIndex) {
const operation = this.context.getNodeParameter('operation', itemIndex);
switch (operation) {
case 'get':
return await this.getAccount(itemIndex);
case 'getAll':
return await this.getAllAccounts(itemIndex);
case 'create':
return await this.createAccount(itemIndex);
case 'update':
return await this.updateAccount(itemIndex);
case 'delete':
return await this.deleteAccount(itemIndex);
default:
throw new Error(`Unknown operation: ${operation}`);
}
}
async createAccount(itemIndex) {
const name = this.context.getNodeParameter('name', itemIndex);
const type = this.context.getNodeParameter('type', itemIndex);
// Additional Fields richtig laden
let additionalFields = {};
try {
additionalFields = this.context.getNodeParameter('additionalFields', itemIndex, {});
}
catch (error) {
// Fallback wenn additionalFields nicht existiert
additionalFields = {};
}
const accountData = {
name,
type,
...additionalFields,
};
// Leere Felder entfernen
Object.keys(accountData).forEach(key => {
if (accountData[key] === '' || accountData[key] === null) {
delete accountData[key];
}
});
try {
await this.initializeClient();
return await this.apiClient.post('Account', accountData);
}
catch (error) {
this.handleError(error, 'Create Account');
}
}
async updateAccount(itemIndex) {
const accountId = this.context.getNodeParameter('accountId', itemIndex);
const name = this.context.getNodeParameter('name', itemIndex);
const type = this.context.getNodeParameter('type', itemIndex);
// Additional Fields richtig laden
let additionalFields = {};
try {
additionalFields = this.context.getNodeParameter('additionalFields', itemIndex, {});
}
catch (error) {
additionalFields = {};
}
const accountData = {
name,
type,
...additionalFields,
};
// Leere Felder entfernen
Object.keys(accountData).forEach(key => {
if (accountData[key] === '' || accountData[key] === null) {
delete accountData[key];
}
});
try {
await this.initializeClient();
return await this.apiClient.put(`Account/${accountId}`, accountData);
}
catch (error) {
this.handleError(error, 'Update Account');
}
}
async getAccount(itemIndex) {
const accountId = this.context.getNodeParameter('accountId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.get(`Account/${accountId}`);
}
catch (error) {
this.handleError(error, 'Get Account');
}
}
async getAllAccounts(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 && searchQuery.trim()) {
params.where = JSON.stringify([
{
type: 'contains',
attribute: 'name',
value: searchQuery.trim()
}
]);
}
return await this.apiClient.get('Account', params);
}
catch (error) {
this.handleError(error, 'Get All Accounts');
}
}
async deleteAccount(itemIndex) {
const accountId = this.context.getNodeParameter('accountId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.delete(`Account/${accountId}`);
}
catch (error) {
this.handleError(error, 'Delete Account');
}
}
}
exports.AccountOperations = AccountOperations;