Refactor product details field

This commit is contained in:
Iván Ovejero 2021-05-09 16:30:16 +02:00
parent 3161f9f72e
commit 6b7bc4392f
3 changed files with 61 additions and 39 deletions

View file

@ -9,6 +9,7 @@ import {
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
NodeApiError, NodeApiError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -17,16 +18,14 @@ import {
} from 'lodash'; } from 'lodash';
export async function zohoApiRequest( export async function zohoApiRequest(
this: IExecuteFunctions | IHookFunctions, this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions,
method: string, method: string,
endpoint: string, endpoint: string,
body: IDataObject = {}, body: IDataObject = {},
qs: IDataObject = {}, qs: IDataObject = {},
uri?: string, uri?: string,
) { ) {
const operation = this.getNodeParameter('operation', 0) as string; const { oauthTokenData: { api_domain: apiDomain } } = this.getCredentials('zohoOAuth2Api') as ZohoOAuth2ApiCredentials;
const { oauthTokenData: { api_domain: apiDomain } } = this.getCredentials('zohoOAuth2Api') as { oauthTokenData: { api_domain: string} };
const options: OptionsWithUri = { const options: OptionsWithUri = {
body: { body: {
@ -50,8 +49,7 @@ export async function zohoApiRequest(
try { try {
console.log(JSON.stringify(options, null, 2)); console.log(JSON.stringify(options, null, 2));
const responseData = await this.helpers.requestOAuth2.call(this, 'zohoOAuth2Api', options); return await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options);
return operation === 'getAll' ? responseData : responseData.data;
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error);
} }
@ -61,13 +59,12 @@ export async function zohoApiRequest(
* Make an authenticated API request to Zoho CRM API and return all items. * Make an authenticated API request to Zoho CRM API and return all items.
*/ */
export async function zohoApiRequestAllItems( export async function zohoApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions, this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string, method: string,
endpoint: string, endpoint: string,
body: IDataObject, body: IDataObject = {},
qs: IDataObject, qs: IDataObject = {},
) { ) {
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let responseData; let responseData;
@ -86,7 +83,6 @@ export async function zohoApiRequestAllItems(
); );
return returnData; return returnData;
} }
/** /**
@ -116,6 +112,18 @@ export async function handleListing(
// field adjusters // field adjusters
// ---------------------------------------- // ----------------------------------------
/**
* Place a product ID at a nested position in a product details field.
*/
export const adjustProductDetails = (productDetails: ProductDetails) => {
return productDetails.map(p => {
return {
...omit('product', p),
product: { id: p.id },
};
});
};
/** /**
* Place a location field's contents at the top level of the payload. * Place a location field's contents at the top level of the payload.
*/ */
@ -214,3 +222,16 @@ export type AllFields =
{ [Location in LocationType]?: { address_fields: { [key: string]: string } } } & { [Location in LocationType]?: { address_fields: { [key: string]: string } } } &
{ Account_Name?: { account_name_fields: { [key: string]: string } } } & { Account_Name?: { account_name_fields: { [key: string]: string } } } &
IDataObject; IDataObject;
export type ProductDetails = Array<{ id: string, quantity: number }>;
type ZohoOAuth2ApiCredentials = {
oauthTokenData: {
api_domain: string;
};
};
export type LoadedProducts = Array<{
Product_Name: string;
id: string;
}>;

View file

@ -4,6 +4,7 @@ import {
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
@ -15,11 +16,15 @@ import {
adjustDealFields, adjustDealFields,
adjustInvoiceFields, adjustInvoiceFields,
adjustLeadFields, adjustLeadFields,
adjustProductDetails,
adjustPurchaseOrderFields, adjustPurchaseOrderFields,
adjustQuoteFields, adjustQuoteFields,
adjustSalesOrderFields, adjustSalesOrderFields,
handleListing, handleListing,
LoadedProducts,
ProductDetails,
zohoApiRequest, zohoApiRequest,
zohoApiRequestAllItems,
} from './GenericFunctions'; } from './GenericFunctions';
import { import {
@ -131,6 +136,15 @@ export class ZohoCrm implements INodeType {
], ],
}; };
methods = {
loadOptions: {
async getProducts(this: ILoadOptionsFunctions) {
const responseData = await zohoApiRequestAllItems.call(this, 'GET', '/products') as LoadedProducts;
return responseData.map((p) => ({ name: p.Product_Name, value: p.id }));
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
@ -387,10 +401,10 @@ export class ZohoCrm implements INodeType {
// invoice: create // invoice: create
// ---------------------------------------- // ----------------------------------------
const productDetails = this.getNodeParameter('Product_Details', i) as ProductDetails;
const body: IDataObject = { const body: IDataObject = {
Product_Details: [ Product_Details: adjustProductDetails(productDetails),
this.getNodeParameter('Product_Details', i),
],
Subject: this.getNodeParameter('subject', i), Subject: this.getNodeParameter('subject', i),
}; };

View file

@ -223,9 +223,13 @@ export const address = {
}; };
export const productDetails = (resource: string, operation: string) => ({ export const productDetails = (resource: string, operation: string) => ({
displayName: 'Product Details', displayName: 'Products',
name: 'Product_Details', name: 'Product_Details',
type: 'collection', type: 'collection',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Product',
},
default: {}, default: {},
placeholder: 'Add Field', placeholder: 'Add Field',
displayOptions: { displayOptions: {
@ -252,30 +256,13 @@ export const productDetails = (resource: string, operation: string) => ({
default: '', default: '',
}, },
{ {
displayName: 'Product', displayName: 'Product ID',
name: 'product', name: 'id',
type: 'collection', type: 'options',
default: {}, default: [],
options: [ typeOptions: {
{ loadOptionsMethod: 'getProducts',
displayName: 'ID', },
name: 'id',
type: 'string',
default: '',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Product Code',
name: 'Product_Code',
type: 'string',
default: '',
},
],
}, },
{ {
displayName: 'Product Description', displayName: 'Product Description',