UNPKG

@traien/n8n-nodes-espocrm

Version:
86 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpportunityHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../GenericFunctions"); const Utils_1 = require("./Utils"); class OpportunityHandler { async create(index) { const entityData = {}; entityData.name = this.getNodeParameter('name', index); entityData.amount = this.getNodeParameter('amount', index); entityData.closeDate = (0, Utils_1.toEspoDate)(this.getNodeParameter('closeDate', index)); const additionalFields = this.getNodeParameter('additionalFields', index, {}); Object.assign(entityData, additionalFields); const endpoint = '/opportunity'; const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'POST', endpoint, entityData); return responseData; } async get(index) { const id = this.getNodeParameter('opportunityId', index); const endpoint = `/opportunity/${id}`; const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'GET', endpoint); return responseData; } async update(index) { const id = this.getNodeParameter('opportunityId', index); const updateFields = this.getNodeParameter('updateFields', index, {}); if (updateFields.closeDate) { updateFields.closeDate = (0, Utils_1.toEspoDate)(updateFields.closeDate); } const endpoint = `/opportunity/${id}`; const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'PATCH', endpoint, updateFields); return responseData; } async delete(index) { const id = this.getNodeParameter('opportunityId', index); const endpoint = `/opportunity/${id}`; await GenericFunctions_1.espoApiRequest.call(this, 'DELETE', endpoint); return { success: true, entityType: 'opportunity', id, message: 'Opportunity deleted successfully' }; } async getAll(index) { const returnAll = this.getNodeParameter('returnAll', index); const endpoint = '/opportunity'; const qs = {}; const filterOptions = this.getNodeParameter('filterOptions', index, {}); if (filterOptions.where) { if (typeof filterOptions.where === 'string') { try { qs.where = JSON.parse(filterOptions.where); } catch (e) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON in 'where' parameter: ${e.message}`); } } else { qs.where = filterOptions.where; } } if (filterOptions.orderBy) qs.orderBy = filterOptions.orderBy; if (filterOptions.order) qs.order = filterOptions.order; if (filterOptions.select) qs.select = filterOptions.select; if (filterOptions.offset) qs.offset = filterOptions.offset; if (filterOptions.boolFilterList) qs.boolFilterList = filterOptions.boolFilterList; if (filterOptions.primaryFilter) qs.primaryFilter = filterOptions.primaryFilter; const headers = {}; if (filterOptions.skipTotalCount === true) headers['X-No-Total'] = 'true'; if (returnAll === true) { return await GenericFunctions_1.espoApiRequestAllItems.call(this, 'GET', endpoint, {}, qs); } else { const limit = this.getNodeParameter('limit', index); qs.maxSize = limit; const response = await GenericFunctions_1.espoApiRequest.call(this, 'GET', endpoint, {}, qs, undefined, headers); return response.list; } } } exports.OpportunityHandler = OpportunityHandler; //# sourceMappingURL=OpportunityHandler.js.map