UNPKG

n8n-nodes-espocrm

Version:

n8n Community Node for EspoCRM

156 lines (155 loc) 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LeadOperations = void 0; const BaseOperation_1 = require("./BaseOperation"); class LeadOperations extends BaseOperation_1.BaseOperation { async execute(itemIndex) { const operation = this.context.getNodeParameter('operation', itemIndex); switch (operation) { case 'get': return await this.getLead(itemIndex); case 'getAll': return await this.getAllLeads(itemIndex); case 'create': return await this.createLead(itemIndex); case 'update': return await this.updateLead(itemIndex); case 'delete': return await this.deleteLead(itemIndex); case 'convert': return await this.convertLead(itemIndex); default: throw new Error(`Unknown operation: ${operation}`); } } async getLead(itemIndex) { const leadId = this.context.getNodeParameter('leadId', itemIndex); try { await this.initializeClient(); return await this.apiClient.get(`Lead/${leadId}`); } catch (error) { this.handleError(error, 'Get Lead'); } } async getAllLeads(itemIndex) { const limit = this.context.getNodeParameter('limit', itemIndex, 10); const searchQuery = this.context.getNodeParameter('searchQuery', itemIndex, ''); const statusFilter = this.context.getNodeParameter('statusFilter', itemIndex, ''); try { await this.initializeClient(); const params = { maxSize: limit }; const whereConditions = []; if (searchQuery.trim()) { whereConditions.push({ type: 'or', value: [ { type: 'contains', attribute: 'name', value: searchQuery.trim() }, { type: 'contains', attribute: 'emailAddress', value: searchQuery.trim() } ] }); } if (statusFilter) { whereConditions.push({ type: 'equals', attribute: 'status', value: statusFilter }); } if (whereConditions.length > 0) { params.where = JSON.stringify(whereConditions.length === 1 ? whereConditions[0] : { type: 'and', value: whereConditions }); } return await this.apiClient.get('Lead', params); } catch (error) { this.handleError(error, 'Get All Leads'); } } async createLead(itemIndex) { const firstName = this.context.getNodeParameter('firstName', itemIndex); const lastName = this.context.getNodeParameter('lastName', itemIndex); const emailAddress = this.context.getNodeParameter('emailAddress', itemIndex, ''); const phoneNumber = this.context.getNodeParameter('phoneNumber', itemIndex, ''); const status = this.context.getNodeParameter('status', itemIndex, 'New'); const source = this.context.getNodeParameter('source', itemIndex, 'Web Site'); const industry = this.context.getNodeParameter('industry', itemIndex, ''); const website = this.context.getNodeParameter('website', itemIndex, ''); const leadData = { firstName, lastName, emailAddress, phoneNumber, status, source, industry, website, }; try { await this.initializeClient(); return await this.apiClient.post('Lead', leadData); } catch (error) { this.handleError(error, 'Create Lead'); } } async updateLead(itemIndex) { const leadId = this.context.getNodeParameter('leadId', itemIndex); const firstName = this.context.getNodeParameter('firstName', itemIndex); const lastName = this.context.getNodeParameter('lastName', itemIndex); const emailAddress = this.context.getNodeParameter('emailAddress', itemIndex, ''); const phoneNumber = this.context.getNodeParameter('phoneNumber', itemIndex, ''); const status = this.context.getNodeParameter('status', itemIndex, 'New'); const source = this.context.getNodeParameter('source', itemIndex, 'Web Site'); const industry = this.context.getNodeParameter('industry', itemIndex, ''); const website = this.context.getNodeParameter('website', itemIndex, ''); const leadData = { firstName, lastName, emailAddress, phoneNumber, status, source, industry, website, }; try { await this.initializeClient(); return await this.apiClient.put(`Lead/${leadId}`, leadData); } catch (error) { this.handleError(error, 'Update Lead'); } } async deleteLead(itemIndex) { const leadId = this.context.getNodeParameter('leadId', itemIndex); try { await this.initializeClient(); return await this.apiClient.delete(`Lead/${leadId}`); } catch (error) { this.handleError(error, 'Delete Lead'); } } async convertLead(itemIndex) { const leadId = this.context.getNodeParameter('leadId', itemIndex); try { await this.initializeClient(); return await this.apiClient.post(`Lead/${leadId}/convert`, {}); } catch (error) { this.handleError(error, 'Convert Lead'); } } } exports.LeadOperations = LeadOperations;