UNPKG

n8n-nodes-gohighlevel

Version:
347 lines 15.1 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _GoHighLevelManager_instances, _GoHighLevelManager_enrichContact, _GoHighLevelManager_enrichOpportunity; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoHighLevelManager = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const pretty_log_1 = require("./pretty-log"); class GoHighLevelManager { constructor(n8n, locationId) { _GoHighLevelManager_instances.add(this); this.baseURL = 'https://services.leadconnectorhq.com'; this.n8n = n8n; this.locationId = locationId; } static async getInstance(n8n) { const credentials = await n8n.getCredentials('goHighLevelOAuth2Api'); (0, pretty_log_1.prettyLog)({ credentials }); const instance = new GoHighLevelManager(n8n, String(credentials.oauthTokenData.locationId)); return instance; } async request({ method, path, body, params }) { const credentials = (await this.n8n.getCredentials('goHighLevelOAuth2Api')); const accessToken = credentials.oauthTokenData.access_token; const url = path.startsWith('/') ? `${this.baseURL}${path}` : path; const urlWithParams = new URL(url); if (params) { Object.entries(params).forEach(([key, value]) => { urlWithParams.searchParams.set(key, String(value)); }); } const formattedBody = body ? (body instanceof FormData || body instanceof Buffer ? body : JSON.stringify(body)) : undefined; console.log(`curl -X ${method} ${urlWithParams.toString()}\n${body ? ` -d '${JSON.stringify(body, null, 2)}'` : ''}`); const response = await (0, node_fetch_1.default)(urlWithParams.toString(), { method, body: formattedBody, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${accessToken}`, 'Version': '2021-07-28', }, }); console.log('response infos', response.status, response.statusText); const data = await response.json(); console.log('response', data); return data; } async httpRequestWithAuthentication({ method, path, body, params, verbose = false }) { const url = path.startsWith('/') ? `${this.baseURL}${path}` : path; const options = { method, body, headers: { 'Content-Type': 'application/json', 'Version': '2021-07-28', }, qs: params, url: url, returnFullResponse: true }; if (verbose) { console.log(`curl -X ${method} ${url}\n${body ? ` -d '${JSON.stringify(body, null, 2)}'` : ''}`); (0, pretty_log_1.prettyLog)(options); } const response = (await this.n8n.helpers.httpRequestWithAuthentication.call(this.n8n, 'goHighLevelOAuth2Api', options)); if (verbose) { (0, pretty_log_1.prettyLog)({ infos: { statusCode: response.statusCode, statusMessage: response.statusMessage, }, body: response.body, }); } return response; } async get(path, options) { const response = await this.httpRequestWithAuthentication({ method: 'GET', path, ...options, verbose: false }); return response.body; } async post(path, options) { const response = await this.httpRequestWithAuthentication({ method: 'POST', path, ...options, verbose: true }); return response.body; } async put(path, options) { const response = await this.httpRequestWithAuthentication({ method: 'PUT', path, ...options, verbose: true, }); return response.body; } async delete(path, options) { const response = await this.httpRequestWithAuthentication({ method: 'DELETE', path, ...options, }); return response.body; } async findContact(params) { const { contacts } = await this.post('/contacts/search', { body: params, }); return contacts[0]; } async findContactByEmail(email) { const contact = await this.findContact({ locationId: this.locationId, page: 1, pageLimit: 50, filters: [{ field: 'email', operator: 'eq', value: email, }], }); return contact; } async createContact(contact) { const { contact: createdContact } = await this.post('/contacts', { body: { locationId: this.locationId, ...contact }, }); const customFields = await this.getCustomFields('contact'); const enrichedContact = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichContact).call(this, createdContact, customFields); return enrichedContact; } async getContacts(options) { const locationId = this.locationId; const { contacts } = await this.get('/contacts', { params: { locationId, ...options?.params, }, }); const customFields = await this.getCustomFields('contact'); const enrichedContacts = contacts.map((contact) => __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichContact).call(this, contact, customFields)); return enrichedContacts; } async getContact(contactId) { const { contact } = await this.get(`/contacts/${contactId}`); const customFields = await this.getCustomFields('contact'); return __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichContact).call(this, contact, customFields); } async updateContact(contactId, contact) { const customFields = await this.getCustomFields('contact'); const { contact: updatedContact } = await this.put(`/contacts/${contactId}`, { body: contact, }); const enrichedContact = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichContact).call(this, updatedContact, customFields); return enrichedContact; } async upsertContact(contact) { const { contact: upsertedContact } = await this.post('/contacts/upsert', { body: contact, }); const customFields = await this.getCustomFields('contact'); const enrichedContact = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichContact).call(this, upsertedContact, customFields); return enrichedContact; } async deleteContact(contactId) { const { contact } = await this.delete(`/contacts/${contactId}`); return contact; } async addTagToContact(contactId, tags) { const { contact: updatedContact } = await this.post(`/contacts/${contactId}/tags`, { body: { tags }, }); return updatedContact; } async removeTagFromContact(contactId, tags) { const { contact: updatedContact } = await this.delete(`/contacts/${contactId}/tags`, { body: { tags }, }); return updatedContact; } async findDuplicateContacts(contact) { const { contacts } = await this.post('/contacts/search/duplicate', { body: contact, }); return contacts; } async createCompany(company) { const { company: createdCompany } = await this.post('/companies', { body: company, }); return createdCompany; } async getCompany(companyId) { const { company } = await this.get(`/companies/${companyId}`); return company; } async getCompanies(options) { const { companies } = await this.get('/companies', options); return companies; } async updateCompany(companyId, company) { const { company: updatedCompany } = await this.put(`/companies/${companyId}`, { body: company, }); return updatedCompany; } async deleteCompany(companyId) { const { company } = await this.delete(`/companies/${companyId}`); return company; } async addTagsToCompany(companyId, tags) { const { company: updatedCompany } = await this.post(`/companies/${companyId}/tags`, { body: { tags }, }); return updatedCompany; } async removeTagsFromCompany(companyId, tags) { const { company: updatedCompany } = await this.delete(`/companies/${companyId}/tags`, { body: { tags }, }); return updatedCompany; } async findOpportunity(params) { const locationId = this.locationId; const { opportunities } = await this.post('/opportunities/search', { body: { location_id: locationId, ...params }, }); const opportunity = opportunities[0]; const customFields = await this.getCustomFields('opportunity'); const enrichedOpportunity = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichOpportunity).call(this, opportunity, customFields); return enrichedOpportunity; } async createOpportunity(opportunity) { const { opportunity: createdOpportunity } = await this.post('/opportunities/', { body: { locationId: this.locationId, ...opportunity }, }); const customFields = await this.getCustomFields('opportunity'); const enrichedOpportunity = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichOpportunity).call(this, createdOpportunity, customFields); return enrichedOpportunity; } async getOpportunity(opportunityId) { const { opportunity } = await this.get(`/opportunities/${opportunityId}`); const customFields = await this.getCustomFields('opportunity'); return __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichOpportunity).call(this, opportunity, customFields); } async getOpportunities(options) { const response = await this.get('/opportunities/search', { params: { location_id: this.locationId, ...options?.params, }, }); const customFields = await this.getCustomFields('opportunity'); console.log('response', response); const opportunities = response.opportunities || []; return opportunities.map((opportunity) => __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichOpportunity).call(this, opportunity, customFields)); } async updateOpportunity(opportunityId, opportunity) { const customFields = await this.getCustomFields('opportunity'); const { opportunity: updatedOpportunity } = await this.put(`/opportunities/${opportunityId}`, { body: opportunity, }); const enrichedOpportunity = __classPrivateFieldGet(this, _GoHighLevelManager_instances, "m", _GoHighLevelManager_enrichOpportunity).call(this, updatedOpportunity, customFields); return enrichedOpportunity; } async deleteOpportunity(opportunityId) { const { opportunity } = await this.delete(`/opportunities/${opportunityId}`); return opportunity; } async updateOpportunityStage(opportunityId, stageId) { const { opportunity: updatedOpportunity } = await this.put(`/opportunities/${opportunityId}/stage`, { body: { stageId }, }); return updatedOpportunity; } async updateOpportunityStatus(opportunityId, status) { const { opportunity: updatedOpportunity } = await this.put(`/opportunities/${opportunityId}/status`, { body: { status }, }); return updatedOpportunity; } async getCustomFields(model = 'all') { const { customFields } = await this.get(`/locations/${this.locationId}/customFields`, { params: { model, }, }); return customFields; } async createCustomField(model, customField) { const { customField: createdCustomField } = await this.post(`/locations/${this.locationId}/customFields`, { body: { ...customField, model }, }); return createdCustomField; } async getCustomField(customFieldId) { const { customField } = await this.get(`/locations/${this.locationId}/customFields/${customFieldId}`); return customField; } async updateCustomField(customFieldId, customField) { const { customField: updatedCustomField } = await this.put(`/locations/${this.locationId}/customFields/${customFieldId}`, { body: customField, }); return updatedCustomField; } async deleteCustomField(customFieldId) { const { customField } = await this.delete(`/locations/${this.locationId}/customFields/${customFieldId}`); return customField; } } exports.GoHighLevelManager = GoHighLevelManager; _GoHighLevelManager_instances = new WeakSet(), _GoHighLevelManager_enrichContact = function _GoHighLevelManager_enrichContact(contact, customFields) { const customFieldsMap = new Map(customFields.map((customField) => [customField.id, customField.name])); return { ...contact, customFields: contact.customFields?.map((customField) => ({ name: customFieldsMap.get(customField.id), ...customField, })), }; }, _GoHighLevelManager_enrichOpportunity = function _GoHighLevelManager_enrichOpportunity(opportunity, customFields) { const customFieldsMap = new Map(customFields.map((customField) => [customField.id, customField.name])); return { ...opportunity, customFields: opportunity.customFields?.map((customField) => ({ name: customFieldsMap.get(customField.id), ...customField, })), }; }; //# sourceMappingURL=gohighlevel.js.map