Add filters for lead:getAll

This commit is contained in:
Iván Ovejero 2021-05-31 10:26:19 +02:00
parent 405ff8d75a
commit e6905bf955
4 changed files with 119 additions and 4 deletions

View file

@ -16,12 +16,14 @@ import {
import { import {
flow, flow,
sortBy,
} from 'lodash'; } from 'lodash';
import { import {
AllFields, AllFields,
DateType, DateType,
IdType, IdType,
LoadedFields,
LocationType, LocationType,
NameType, NameType,
ProductDetails, ProductDetails,
@ -58,6 +60,7 @@ export async function zohoApiRequest(
if (!Object.keys(qs).length) { if (!Object.keys(qs).length) {
delete options.qs; delete options.qs;
} }
try { try {
const responseData = await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options); const responseData = await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options);
@ -284,3 +287,15 @@ const omit = (propertyToOmit: string, { [propertyToOmit]: _, ...remainingObject
*/ */
export const toLoadOptions = (items: ResourceItems, nameProperty: NameType) => export const toLoadOptions = (items: ResourceItems, nameProperty: NameType) =>
items.map((item) => ({ name: item[nameProperty], value: item.id })); items.map((item) => ({ name: item[nameProperty], value: item.id }));
/**
* Retrieve all fields for a resource, sorted alphabetically.
*/
export async function getFields(this: ILoadOptionsFunctions, resource: string) {
const { fields } = await zohoApiRequest.call(
this, 'GET', '/settings/fields', {}, { module: `${resource}s` },
) as LoadedFields;
const options = fields.map(({field_label, api_name}) => ({ name: field_label, value: api_name }));
return sortBy(options, o => o.name);
}

View file

@ -8,8 +8,6 @@ import {
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
@ -23,9 +21,9 @@ import {
adjustQuotePayload, adjustQuotePayload,
adjustSalesOrderPayload, adjustSalesOrderPayload,
adjustVendorPayload, adjustVendorPayload,
getFields,
handleListing, handleListing,
throwOnEmptyUpdate, throwOnEmptyUpdate,
throwOnErrorStatus,
toLoadOptions, toLoadOptions,
zohoApiRequest, zohoApiRequest,
zohoApiRequestAllItems, zohoApiRequestAllItems,
@ -35,6 +33,7 @@ import {
LoadedAccounts, LoadedAccounts,
LoadedContacts, LoadedContacts,
LoadedDeals, LoadedDeals,
LoadedFields,
LoadedProducts, LoadedProducts,
LoadedVendors, LoadedVendors,
ProductDetails, ProductDetails,
@ -174,6 +173,10 @@ export class ZohoCrm implements INodeType {
return toLoadOptions(deals, 'Deal_Name'); return toLoadOptions(deals, 'Deal_Name');
}, },
async getLeadFields(this: ILoadOptionsFunctions) {
return getFields.call(this, 'lead');
},
async getProducts(this: ILoadOptionsFunctions) { async getProducts(this: ILoadOptionsFunctions) {
const products = await zohoApiRequestAllItems.call(this, 'GET', '/products') as LoadedProducts; const products = await zohoApiRequestAllItems.call(this, 'GET', '/products') as LoadedProducts;
return toLoadOptions(products, 'Product_Name'); return toLoadOptions(products, 'Product_Name');
@ -585,7 +588,14 @@ export class ZohoCrm implements INodeType {
// lead: getAll // lead: getAll
// ---------------------------------------- // ----------------------------------------
responseData = await handleListing.call(this, 'GET', '/leads'); const qs: IDataObject = {};
const options = this.getNodeParameter('options', i) as IDataObject;
if (Object.keys(options).length) {
Object.assign(qs, options);
}
responseData = await handleListing.call(this, 'GET', '/leads', {}, qs);
} else if (operation === 'update') { } else if (operation === 'update') {

View file

@ -287,6 +287,89 @@ export const leadFields = [
// lead: getAll // lead: getAll
// ---------------------------------------- // ----------------------------------------
...makeGetAllFields('lead'), ...makeGetAllFields('lead'),
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'lead',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Approved',
name: 'approved',
type: 'boolean',
default: true,
description: 'Retrieve only approved leads. Defaults to true.',
},
{
displayName: 'Converted',
name: 'converted',
type: 'boolean',
default: false,
description: 'Retrieve only converted leads. Defaults to false.',
},
{
displayName: 'Fields',
name: 'fields',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getLeadFields',
},
default: [],
},
{
displayName: 'Include Child',
name: 'include_child',
type: 'boolean',
default: false,
description: 'Retrieve only leads from child territories.',
},
{
displayName: 'Sort By',
name: 'sort_by',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getLeadFields',
},
default: [],
description: 'Field to sort leads by.',
},
{
displayName: 'Sort Order',
name: 'sort_order',
type: 'options',
options: [
{
name: 'Ascending',
value: 'asc',
},
{
name: 'Descending',
value: 'desc',
},
],
default: 'desc',
description: 'Ascending or descending order sort order.',
},
{
displayName: 'Territory ID',
name: 'territory_id',
type: 'string',
default: '',
description: 'Retrieve only leads from this territory.',
},
],
},
// ---------------------------------------- // ----------------------------------------
// lead: update // lead: update

View file

@ -52,6 +52,13 @@ export type LoadedDeals = Array<{
id: string; id: string;
}>; }>;
export type LoadedFields = {
fields: Array<{
field_label: string;
api_name: string;
}>
};
export type LoadedVendors = Array<{ export type LoadedVendors = Array<{
Vendor_Name: string; Vendor_Name: string;
id: string; id: string;