mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
⚡ Refactor product details field
This commit is contained in:
parent
3161f9f72e
commit
6b7bc4392f
|
@ -9,6 +9,7 @@ import {
|
|||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
@ -17,16 +18,14 @@ import {
|
|||
} from 'lodash';
|
||||
|
||||
export async function zohoApiRequest(
|
||||
this: IExecuteFunctions | IHookFunctions,
|
||||
this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
) {
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
|
||||
const { oauthTokenData: { api_domain: apiDomain } } = this.getCredentials('zohoOAuth2Api') as { oauthTokenData: { api_domain: string} };
|
||||
const { oauthTokenData: { api_domain: apiDomain } } = this.getCredentials('zohoOAuth2Api') as ZohoOAuth2ApiCredentials;
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
body: {
|
||||
|
@ -50,8 +49,7 @@ export async function zohoApiRequest(
|
|||
|
||||
try {
|
||||
console.log(JSON.stringify(options, null, 2));
|
||||
const responseData = await this.helpers.requestOAuth2.call(this, 'zohoOAuth2Api', options);
|
||||
return operation === 'getAll' ? responseData : responseData.data;
|
||||
return await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options);
|
||||
} catch (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.
|
||||
*/
|
||||
export async function zohoApiRequestAllItems(
|
||||
this: IHookFunctions | IExecuteFunctions,
|
||||
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
body: IDataObject,
|
||||
qs: IDataObject,
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
) {
|
||||
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
|
@ -86,7 +83,6 @@ export async function zohoApiRequestAllItems(
|
|||
);
|
||||
|
||||
return returnData;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,6 +112,18 @@ export async function handleListing(
|
|||
// 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.
|
||||
*/
|
||||
|
@ -214,3 +222,16 @@ export type AllFields =
|
|||
{ [Location in LocationType]?: { address_fields: { [key: string]: string } } } &
|
||||
{ Account_Name?: { account_name_fields: { [key: string]: string } } } &
|
||||
IDataObject;
|
||||
|
||||
export type ProductDetails = Array<{ id: string, quantity: number }>;
|
||||
|
||||
type ZohoOAuth2ApiCredentials = {
|
||||
oauthTokenData: {
|
||||
api_domain: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type LoadedProducts = Array<{
|
||||
Product_Name: string;
|
||||
id: string;
|
||||
}>;
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
|
@ -15,11 +16,15 @@ import {
|
|||
adjustDealFields,
|
||||
adjustInvoiceFields,
|
||||
adjustLeadFields,
|
||||
adjustProductDetails,
|
||||
adjustPurchaseOrderFields,
|
||||
adjustQuoteFields,
|
||||
adjustSalesOrderFields,
|
||||
handleListing,
|
||||
LoadedProducts,
|
||||
ProductDetails,
|
||||
zohoApiRequest,
|
||||
zohoApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
|
||||
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[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: IDataObject[] = [];
|
||||
|
@ -387,10 +401,10 @@ export class ZohoCrm implements INodeType {
|
|||
// invoice: create
|
||||
// ----------------------------------------
|
||||
|
||||
const productDetails = this.getNodeParameter('Product_Details', i) as ProductDetails;
|
||||
|
||||
const body: IDataObject = {
|
||||
Product_Details: [
|
||||
this.getNodeParameter('Product_Details', i),
|
||||
],
|
||||
Product_Details: adjustProductDetails(productDetails),
|
||||
Subject: this.getNodeParameter('subject', i),
|
||||
};
|
||||
|
||||
|
|
|
@ -223,9 +223,13 @@ export const address = {
|
|||
};
|
||||
|
||||
export const productDetails = (resource: string, operation: string) => ({
|
||||
displayName: 'Product Details',
|
||||
displayName: 'Products',
|
||||
name: 'Product_Details',
|
||||
type: 'collection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
multipleValueButtonText: 'Add Product',
|
||||
},
|
||||
default: {},
|
||||
placeholder: 'Add Field',
|
||||
displayOptions: {
|
||||
|
@ -252,30 +256,13 @@ export const productDetails = (resource: string, operation: string) => ({
|
|||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Product',
|
||||
name: 'product',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
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 ID',
|
||||
name: 'id',
|
||||
type: 'options',
|
||||
default: [],
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getProducts',
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Product Description',
|
||||
|
|
Loading…
Reference in a new issue