@traien/n8n-nodes-espocrm
Version:
n8n integration with EspoCRM
98 lines • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LeadHandler = void 0;
const GenericFunctions_1 = require("../GenericFunctions");
const Utils_1 = require("./Utils");
class LeadHandler {
async create(index) {
const entityData = {};
entityData.firstName = this.getNodeParameter('firstName', index, '');
entityData.lastName = this.getNodeParameter('lastName', index);
entityData.status = this.getNodeParameter('status', index);
const additionalFields = this.getNodeParameter('additionalFields', index, {});
Object.assign(entityData, additionalFields);
if (additionalFields.address) {
const address = additionalFields.address.details;
Object.assign(entityData, (0, Utils_1.processAddressFields)('address', address));
delete entityData.address;
}
const endpoint = '/lead';
const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'POST', endpoint, entityData);
return responseData;
}
async get(index) {
const leadId = this.getNodeParameter('leadId', index);
const endpoint = `/lead/${leadId}`;
const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'GET', endpoint);
return responseData;
}
async update(index) {
const leadId = this.getNodeParameter('leadId', index);
const updateFields = this.getNodeParameter('updateFields', index, {});
const entityData = { ...updateFields };
if (updateFields.address) {
const address = updateFields.address.details;
Object.assign(entityData, (0, Utils_1.processAddressFields)('address', address));
delete entityData.address;
}
const endpoint = `/lead/${leadId}`;
const responseData = await GenericFunctions_1.espoApiRequest.call(this, 'PATCH', endpoint, entityData);
return responseData;
}
async delete(index) {
const leadId = this.getNodeParameter('leadId', index);
const endpoint = `/lead/${leadId}`;
await GenericFunctions_1.espoApiRequest.call(this, 'DELETE', endpoint);
return {
success: true,
entityType: 'lead',
id: leadId,
message: 'Lead deleted successfully'
};
}
async getAll(index) {
const returnAll = this.getNodeParameter('returnAll', index);
const endpoint = '/lead';
const qs = {};
const filterOptions = this.getNodeParameter('filterOptions', index, {});
if (filterOptions.where) {
qs.where = filterOptions.where;
}
if (filterOptions.orderBy) {
qs.orderBy = filterOptions.orderBy;
}
if (filterOptions.order) {
qs.order = filterOptions.order;
}
if (filterOptions.select) {
const selectValue = filterOptions.select;
if (selectValue) {
qs.select = selectValue.split(',').map(s => s.trim()).filter(Boolean);
}
}
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.LeadHandler = LeadHandler;
//# sourceMappingURL=LeadHandler.js.map