n8n-nodes-gohighlevel
Version:
n8n node for GoHighLevel API v2
103 lines • 4.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleOpportunityOperations = void 0;
const gohighlevel_1 = require("../../utils/gohighlevel");
function formatCustomFields(data) {
if (data.customFields) {
data.customFields = data.customFields.values;
}
}
async function handleOpportunityOperations(operation, i) {
const GHLEngine = await gohighlevel_1.GoHighLevelManager.getInstance(this);
if (operation === 'create') {
const data = {
name: this.getNodeParameter('name', i),
pipelineId: this.getNodeParameter('pipelineId', i),
pipelineStageId: this.getNodeParameter('pipelineStageId', i),
status: this.getNodeParameter('status', i),
contactId: this.getNodeParameter('contactId', i),
...this.getNodeParameter('additionalFields', i, {}),
};
return GHLEngine.createOpportunity(data);
}
if (operation === 'update') {
const opportunityId = this.getNodeParameter('opportunityId', i);
const data = {
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
return GHLEngine.updateOpportunity(opportunityId, data);
}
if (operation === 'get') {
const opportunityId = this.getNodeParameter('opportunityId', i);
return GHLEngine.getOpportunity(opportunityId);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i);
const filters = this.getNodeParameter('filters', i);
if (returnAll) {
return GHLEngine.getOpportunities();
}
else {
const limit = this.getNodeParameter('limit', i);
return GHLEngine.getOpportunities({ params: { ...filters, limit } });
}
}
if (operation === 'delete') {
const opportunityId = this.getNodeParameter('opportunityId', i);
return GHLEngine.deleteOpportunity(opportunityId);
}
if (operation === 'updateStage') {
const opportunityId = this.getNodeParameter('opportunityId', i);
const stageId = this.getNodeParameter('stageId', i);
return GHLEngine.updateOpportunityStage(opportunityId, stageId);
}
if (operation === 'updateStatus') {
const opportunityId = this.getNodeParameter('opportunityId', i);
const status = this.getNodeParameter('status', i);
return GHLEngine.updateOpportunityStatus(opportunityId, status);
}
if (operation === 'updateOrCreate') {
const contactId = this.getNodeParameter('contactId', i);
const name = this.getNodeParameter('name', i);
const data = {
name,
contactId,
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
const opportunity = await GHLEngine.findOpportunity({ contact_id: contactId });
if (opportunity?.id) {
return GHLEngine.updateOpportunity(opportunity.id, data);
}
return GHLEngine.createOpportunity(data);
}
if (operation === 'findOrCreate') {
const contactId = this.getNodeParameter('contactId', i);
const name = this.getNodeParameter('name', i);
const data = {
name,
contactId,
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
const opportunity = await GHLEngine.findOpportunity({ contact_id: contactId });
if (opportunity?.id) {
return GHLEngine.updateOpportunity(opportunity.id, data);
}
return GHLEngine.createOpportunity(data);
}
if (operation === 'updateStage') {
const opportunityId = this.getNodeParameter('opportunityId', i);
const stageId = this.getNodeParameter('stageId', i);
return GHLEngine.updateOpportunityStage(opportunityId, stageId);
}
if (operation === 'updateStatus') {
const opportunityId = this.getNodeParameter('opportunityId', i);
const status = this.getNodeParameter('status', i);
return GHLEngine.updateOpportunityStatus(opportunityId, status);
}
throw new Error(`Unknown operation: ${operation}`);
}
exports.handleOpportunityOperations = handleOpportunityOperations;
//# sourceMappingURL=opportunity.handler.js.map