Add resource loaders for picklists

This commit is contained in:
Iván Ovejero 2021-06-21 11:28:41 +02:00
parent 32f0a3af85
commit 07881b1385
8 changed files with 138 additions and 34 deletions

View file

@ -26,6 +26,7 @@ import {
GetAllFilterOptions, GetAllFilterOptions,
IdType, IdType,
LoadedFields, LoadedFields,
LoadedLayouts,
LocationType, LocationType,
NameType, NameType,
ProductDetails, ProductDetails,
@ -327,19 +328,7 @@ export async function getFields(
resource: SnakeCaseResource, resource: SnakeCaseResource,
{ onlyCustom } = { onlyCustom: false }, { onlyCustom } = { onlyCustom: false },
) { ) {
const moduleMap: { [resource: string]: string } = { const qs = { module: getModuleName(resource) };
account: 'Accounts',
contact: 'Contacts',
deal: 'Deals',
invoice: 'Invoices',
lead: 'Leads',
product: 'Products',
purchaseOrder: 'Purchase_Orders',
salesOrder: 'Sales_Orders',
vendor: 'Vendors',
};
const qs = { module: moduleMap[resource] };
let { fields } = await zohoApiRequest.call(this, 'GET', '/settings/fields', {}, qs) as LoadedFields; let { fields } = await zohoApiRequest.call(this, 'GET', '/settings/fields', {}, qs) as LoadedFields;
@ -352,6 +341,49 @@ export async function getFields(
return sortBy(options, o => o.name); return sortBy(options, o => o.name);
} }
function getModuleName(resource: string) {
return {
account: 'Accounts',
contact: 'Contacts',
deal: 'Deals',
invoice: 'Invoices',
lead: 'Leads',
product: 'Products',
purchaseOrder: 'Purchase_Orders',
salesOrder: 'Sales_Orders',
vendor: 'Vendors',
quote: 'Quotes',
}[resource];
}
export async function getPicklistOptions(
this: ILoadOptionsFunctions,
resource: string,
targetField: string,
) {
const qs = { module: getModuleName(resource) };
const responseData = await zohoApiRequest.call(this, 'GET', '/settings/layouts', {}, qs) as LoadedLayouts;
const pickListOptions = responseData.layouts[0]
.sections.find(section => section.api_name === getSectionApiName(resource))
?.fields.find(f => f.api_name === targetField)
?.pick_list_values;
if (!pickListOptions) return [];
return pickListOptions.map(
(option) => ({ name: option.display_value, value: option.actual_value }),
);
}
function getSectionApiName(resource: string) {
if (resource === 'purchaseOrder') return 'Purchase Order Information';
if (resource === 'salesOrder') return 'Sales Order Information';
return `${capitalizeInitial(resource)} Information`;
}
/** /**
* Add filter options to a query string object. * Add filter options to a query string object.
*/ */

View file

@ -24,6 +24,7 @@ import {
adjustSalesOrderPayload, adjustSalesOrderPayload,
adjustVendorPayload, adjustVendorPayload,
getFields, getFields,
getPicklistOptions,
handleListing, handleListing,
throwOnEmptyUpdate, throwOnEmptyUpdate,
toLoadOptions, toLoadOptions,
@ -194,7 +195,7 @@ export class ZohoCrm implements INodeType {
// resource fields // resource fields
// ---------------------------------------- // ----------------------------------------
// standard fields // standard fields - called from `makeGetAllFields`
async getAccountFields(this: ILoadOptionsFunctions) { async getAccountFields(this: ILoadOptionsFunctions) {
return getFields.call(this, 'account'); return getFields.call(this, 'account');
@ -285,6 +286,30 @@ export class ZohoCrm implements INodeType {
async getCustomVendorFields(this: ILoadOptionsFunctions) { async getCustomVendorFields(this: ILoadOptionsFunctions) {
return getFields.call(this, 'vendor', { onlyCustom: true }); return getFields.call(this, 'vendor', { onlyCustom: true });
}, },
// ----------------------------------------
// resource picklist options
// ----------------------------------------
async getAccountType(this: ILoadOptionsFunctions) {
return getPicklistOptions.call(this, 'account', 'Account_Type');
},
async getDealStage(this: ILoadOptionsFunctions) {
return getPicklistOptions.call(this, 'deal', 'Stage');
},
async getPurchaseOrderStatus(this: ILoadOptionsFunctions) {
return getPicklistOptions.call(this, 'purchaseOrder', 'Status');
},
async getSalesOrderStatus(this: ILoadOptionsFunctions) {
return getPicklistOptions.call(this, 'salesOrder', 'Status');
},
async getQuoteStage(this: ILoadOptionsFunctions) {
return getPicklistOptions.call(this, 'quote', 'Quote_Stage');
},
}, },
}; };

View file

@ -135,8 +135,11 @@ export const accountFields = [
{ {
displayName: 'Account Type', displayName: 'Account Type',
name: 'Account_Type', name: 'Account_Type',
type: 'string', type: 'options',
default: '', typeOptions: {
loadOptionsMethod: 'getAccountType',
},
default: [],
}, },
{ {
displayName: 'Annual Revenue', displayName: 'Annual Revenue',
@ -323,8 +326,11 @@ export const accountFields = [
{ {
displayName: 'Account Type', displayName: 'Account Type',
name: 'Account_Type', name: 'Account_Type',
type: 'string', type: 'options',
default: '', typeOptions: {
loadOptionsMethod: 'getAccountType',
},
default: [],
}, },
{ {
displayName: 'Annual Revenue', displayName: 'Annual Revenue',

View file

@ -102,9 +102,12 @@ export const dealFields = [
{ {
displayName: 'Stage', displayName: 'Stage',
name: 'stage', name: 'stage',
type: 'string', type: 'options',
required: true, required: true,
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getDealStage',
},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
@ -367,8 +370,11 @@ export const dealFields = [
{ {
displayName: 'Stage', displayName: 'Stage',
name: 'Stage', name: 'Stage',
type: 'string', type: 'options',
default: '', typeOptions: {
loadOptionsMethod: 'getDealStage',
},
default: [],
}, },
], ],
}, },

View file

@ -284,8 +284,11 @@ export const purchaseOrderFields = [
{ {
displayName: 'Status', displayName: 'Status',
name: 'Status', name: 'Status',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getPurchaseOrderStatus',
},
description: 'Status of the purchase order.', description: 'Status of the purchase order.',
}, },
{ {
@ -508,8 +511,11 @@ export const purchaseOrderFields = [
{ {
displayName: 'Status', displayName: 'Status',
name: 'Status', name: 'Status',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getPurchaseOrderStatus',
},
description: 'Status of the purchase order.', description: 'Status of the purchase order.',
}, },
{ {

View file

@ -177,8 +177,12 @@ export const quoteFields = [
{ {
displayName: 'Quote Stage', displayName: 'Quote Stage',
name: 'Quote_Stage', name: 'Quote_Stage',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getQuoteStage',
},
description: 'Stage of the quote.',
}, },
shippingAddress, shippingAddress,
{ {
@ -367,8 +371,12 @@ export const quoteFields = [
{ {
displayName: 'Quote Stage', displayName: 'Quote Stage',
name: 'Quote_Stage', name: 'Quote_Stage',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getQuoteStage',
},
description: 'Stage of the quote.',
}, },
shippingAddress, shippingAddress,
{ {

View file

@ -255,8 +255,11 @@ export const salesOrderFields = [
{ {
displayName: 'Status', displayName: 'Status',
name: 'Status', name: 'Status',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getSalesOrderStatus',
},
description: 'Status of the sales order.', description: 'Status of the sales order.',
}, },
{ {
@ -493,8 +496,11 @@ export const salesOrderFields = [
{ {
displayName: 'Status', displayName: 'Status',
name: 'Status', name: 'Status',
type: 'string', type: 'options',
default: '', default: [],
typeOptions: {
loadOptionsMethod: 'getSalesOrderStatus',
},
description: 'Status of the sales order.', description: 'Status of the sales order.',
}, },
{ {

View file

@ -85,3 +85,18 @@ export type LoadedProducts = Array<{
Product_Name: string; Product_Name: string;
id: string; id: string;
}>; }>;
export type LoadedLayouts = {
layouts: Array<{
sections: Array<{
api_name: string;
fields: Array<{
api_name: string;
pick_list_values: Array<{
display_value: string;
actual_value: string;
}>
}>
}>
}>
}