n8n-nodes-espocrm
Version:
n8n Community Node for EspoCRM
134 lines (133 loc) • 5.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpportunityOperations = void 0;
const BaseOperation_1 = require("./BaseOperation");
class OpportunityOperations extends BaseOperation_1.BaseOperation {
async execute(itemIndex) {
const operation = this.context.getNodeParameter('operation', itemIndex);
switch (operation) {
case 'get':
return await this.getOpportunity(itemIndex);
case 'getAll':
return await this.getAllOpportunities(itemIndex);
case 'create':
return await this.createOpportunity(itemIndex);
case 'update':
return await this.updateOpportunity(itemIndex);
case 'delete':
return await this.deleteOpportunity(itemIndex);
default:
throw new Error(`Unknown operation: ${operation}`);
}
}
async getOpportunity(itemIndex) {
const opportunityId = this.context.getNodeParameter('opportunityId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.get(`Opportunity/${opportunityId}`);
}
catch (error) {
this.handleError(error, 'Get Opportunity');
}
}
async getAllOpportunities(itemIndex) {
const limit = this.context.getNodeParameter('limit', itemIndex, 10);
const searchQuery = this.context.getNodeParameter('searchQuery', itemIndex, '');
const stageFilter = this.context.getNodeParameter('stageFilter', itemIndex, '');
try {
await this.initializeClient();
const params = { maxSize: limit };
const whereConditions = [];
if (searchQuery.trim()) {
whereConditions.push({
type: 'contains',
attribute: 'name',
value: searchQuery.trim()
});
}
if (stageFilter) {
whereConditions.push({
type: 'equals',
attribute: 'stage',
value: stageFilter
});
}
if (whereConditions.length > 0) {
params.where = JSON.stringify(whereConditions.length === 1 ? whereConditions[0] : {
type: 'and',
value: whereConditions
});
}
return await this.apiClient.get('Opportunity', params);
}
catch (error) {
this.handleError(error, 'Get All Opportunities');
}
}
async createOpportunity(itemIndex) {
const name = this.context.getNodeParameter('name', itemIndex);
const accountId = this.context.getNodeParameter('accountId', itemIndex, '');
const contactId = this.context.getNodeParameter('contactId', itemIndex, '');
const stage = this.context.getNodeParameter('stage', itemIndex, 'Prospecting');
const amount = this.context.getNodeParameter('amount', itemIndex, 0);
const probability = this.context.getNodeParameter('probability', itemIndex, 0);
const closeDate = this.context.getNodeParameter('closeDate', itemIndex, '');
const leadSource = this.context.getNodeParameter('leadSource', itemIndex, 'Web Site');
const opportunityData = {
name,
accountId,
contactId,
stage,
amount,
probability,
closeDate,
leadSource,
};
try {
await this.initializeClient();
return await this.apiClient.post('Opportunity', opportunityData);
}
catch (error) {
this.handleError(error, 'Create Opportunity');
}
}
async updateOpportunity(itemIndex) {
const opportunityId = this.context.getNodeParameter('opportunityId', itemIndex);
const name = this.context.getNodeParameter('name', itemIndex);
const accountId = this.context.getNodeParameter('accountId', itemIndex, '');
const contactId = this.context.getNodeParameter('contactId', itemIndex, '');
const stage = this.context.getNodeParameter('stage', itemIndex, 'Prospecting');
const amount = this.context.getNodeParameter('amount', itemIndex, 0);
const probability = this.context.getNodeParameter('probability', itemIndex, 0);
const closeDate = this.context.getNodeParameter('closeDate', itemIndex, '');
const leadSource = this.context.getNodeParameter('leadSource', itemIndex, 'Web Site');
const opportunityData = {
name,
accountId,
contactId,
stage,
amount,
probability,
closeDate,
leadSource,
};
try {
await this.initializeClient();
return await this.apiClient.put(`Opportunity/${opportunityId}`, opportunityData);
}
catch (error) {
this.handleError(error, 'Update Opportunity');
}
}
async deleteOpportunity(itemIndex) {
const opportunityId = this.context.getNodeParameter('opportunityId', itemIndex);
try {
await this.initializeClient();
return await this.apiClient.delete(`Opportunity/${opportunityId}`);
}
catch (error) {
this.handleError(error, 'Delete Opportunity');
}
}
}
exports.OpportunityOperations = OpportunityOperations;