n8n-nodes-gohighlevel
Version:
n8n node for GoHighLevel API v2
90 lines • 3.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContactOperations = void 0;
const gohighlevel_1 = require("../../utils/gohighlevel");
function formatCustomFields(data) {
if (data.customFields) {
data.customFields = data.customFields.values;
}
}
async function handleContactOperations(operation, i) {
const GHLEngine = await gohighlevel_1.GoHighLevelManager.getInstance(this);
if (operation === 'create') {
const data = {
email: this.getNodeParameter('email', i),
phone: this.getNodeParameter('phone', i),
...this.getNodeParameter('additionalFields', i, {}),
};
return GHLEngine.createContact(data);
}
if (operation === 'update') {
const contactId = this.getNodeParameter('contactId', i);
const data = {
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
return GHLEngine.updateContact(contactId, data);
}
if (operation === 'get') {
const contactId = this.getNodeParameter('contactId', i);
return GHLEngine.getContact(contactId);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i);
const filters = this.getNodeParameter('filters', i);
if (returnAll) {
return GHLEngine.getContacts({ params: filters });
}
else {
const limit = this.getNodeParameter('limit', i);
return GHLEngine.getContacts({ params: { ...filters, limit } });
}
}
if (operation === 'delete') {
const contactId = this.getNodeParameter('contactId', i);
return GHLEngine.deleteContact(contactId);
}
if (operation === 'addTag') {
const contactId = this.getNodeParameter('contactId', i);
const tags = this.getNodeParameter('tags', i).split(',').map(tag => tag.trim());
return GHLEngine.addTagToContact(contactId, tags);
}
if (operation === 'removeTag') {
const contactId = this.getNodeParameter('contactId', i);
const tags = this.getNodeParameter('tags', i).split(',').map(tag => tag.trim());
return GHLEngine.removeTagFromContact(contactId, tags);
}
if (operation === 'find') {
const email = this.getNodeParameter('email', i);
return await GHLEngine.findContactByEmail(email);
}
if (operation === 'findOrCreate') {
const email = this.getNodeParameter('email', i);
const contact = await GHLEngine.findContactByEmail(email);
if (!contact) {
const data = {
email,
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
return GHLEngine.createContact(data);
}
return contact;
}
if (operation === 'updateOrCreate') {
const email = this.getNodeParameter('email', i);
const data = {
email,
...this.getNodeParameter('additionalFields', i, {}),
};
formatCustomFields(data);
const contact = await GHLEngine.findContactByEmail(email);
if (!contact?.id) {
return GHLEngine.createContact(data);
}
return GHLEngine.updateContact(contact.id, data);
}
throw new Error(`Unknown operation: ${operation}`);
}
exports.handleContactOperations = handleContactOperations;
//# sourceMappingURL=contact.handler.js.map