mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Refactor types in adjusters
This commit is contained in:
parent
68bfd3deee
commit
843cc78885
|
@ -101,7 +101,7 @@ export async function handleListing(
|
||||||
) {
|
) {
|
||||||
let responseData;
|
let responseData;
|
||||||
|
|
||||||
const returnAll = this.getNodeParameter('returnAll', 0);
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
||||||
|
|
||||||
if (returnAll) {
|
if (returnAll) {
|
||||||
return await zohoApiRequestAllItems.call(this, method, endpoint, body, qs);
|
return await zohoApiRequestAllItems.call(this, method, endpoint, body, qs);
|
||||||
|
@ -119,11 +119,15 @@ export async function handleListing(
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
const adjustLocationFields = (locationType: LocationType) => (allFields: IDataObject) => {
|
const adjustLocationFields = (locationType: LocationType) => (allFields: AllFields) => {
|
||||||
const locationField = allFields[locationType];
|
const locationField = allFields[locationType];
|
||||||
if (!locationField || !hasAddressFields(locationField)) return allFields;
|
|
||||||
|
|
||||||
return { ...omit(locationType, allFields), ...locationField.address_fields };
|
if (!locationField) return allFields;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...omit(locationType, allFields),
|
||||||
|
...locationField.address_fields,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const adjustAddressFields = adjustLocationFields('Address');
|
const adjustAddressFields = adjustLocationFields('Address');
|
||||||
|
@ -133,14 +137,13 @@ const adjustShippingAddressFields = adjustLocationFields('Shipping_Address');
|
||||||
const adjustOtherAddressFields = adjustLocationFields('Other_Address');
|
const adjustOtherAddressFields = adjustLocationFields('Other_Address');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a date field's timestamp.
|
* Remove the timestamp set by the datepicker from a date field.
|
||||||
*/
|
*/
|
||||||
const adjustDateField = (dateType: DateType) => (allFields: IDataObject) => {
|
const adjustDateField = (dateType: DateType) => (allFields: AllFields) => {
|
||||||
const dateField = allFields[dateType];
|
const dateField = allFields[dateType];
|
||||||
|
|
||||||
if (!dateField) return allFields;
|
if (!dateField) return allFields;
|
||||||
|
|
||||||
// @ts-ignore TODO
|
|
||||||
allFields[dateType] = dateField.split('T')[0];
|
allFields[dateType] = dateField.split('T')[0];
|
||||||
|
|
||||||
return allFields;
|
return allFields;
|
||||||
|
@ -151,17 +154,18 @@ const adjustClosingDateField = adjustDateField('Closing_Date');
|
||||||
const adjustInvoiceDateField = adjustDateField('Invoice_Date');
|
const adjustInvoiceDateField = adjustDateField('Invoice_Date');
|
||||||
const adjustDueDateField = adjustDateField('Due_Date');
|
const adjustDueDateField = adjustDateField('Due_Date');
|
||||||
|
|
||||||
const adjustAccountField = (allFields: IDataObject) => {
|
/**
|
||||||
|
* Place an account name field's contents at the top level of the payload.
|
||||||
|
*/
|
||||||
|
const adjustAccountField = (allFields: AllFields) => {
|
||||||
if (!allFields.Account_Name) return allFields;
|
if (!allFields.Account_Name) return allFields;
|
||||||
|
|
||||||
// @ts-ignore TODO
|
return {
|
||||||
return { ...omit('Account_Name', allFields), ...allFields.Account_Name.account_name_fields };
|
...omit('Account_Name', allFields),
|
||||||
|
...allFields.Account_Name.account_name_fields,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------
|
|
||||||
// field adjusters per resource
|
|
||||||
// ----------------------------------------
|
|
||||||
|
|
||||||
export const adjustAccountFields = flow(
|
export const adjustAccountFields = flow(
|
||||||
adjustBillingAddressFields,
|
adjustBillingAddressFields,
|
||||||
adjustShippingAddressFields,
|
adjustShippingAddressFields,
|
||||||
|
@ -195,12 +199,7 @@ export const adjustSalesOrderFields = adjustInvoiceFields;
|
||||||
// helpers
|
// helpers
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
|
||||||
export const omit = (keyToOmit: string, { [keyToOmit]: _, ...omittedPropObj }) => omittedPropObj;
|
const omit = (keyToOmit: string, { [keyToOmit]: _, ...omittedPropObj }) => omittedPropObj;
|
||||||
|
|
||||||
function hasAddressFields(locationField: unknown): locationField is LocationField {
|
|
||||||
if (typeof locationField !== 'object' || locationField === null) return false;
|
|
||||||
return locationField.hasOwnProperty('address_fields');
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
// types
|
// types
|
||||||
|
@ -208,10 +207,10 @@ function hasAddressFields(locationField: unknown): locationField is LocationFiel
|
||||||
|
|
||||||
type LocationType = 'Address' | 'Billing_Address' | 'Mailing_Address' | 'Shipping_Address' | 'Other_Address';
|
type LocationType = 'Address' | 'Billing_Address' | 'Mailing_Address' | 'Shipping_Address' | 'Other_Address';
|
||||||
|
|
||||||
type LocationField = {
|
|
||||||
address_fields: { [key in LocationType]: string };
|
|
||||||
};
|
|
||||||
|
|
||||||
type DateType = 'Date_of_Birth' | 'Closing_Date' | 'Due_Date' | 'Invoice_Date';
|
type DateType = 'Date_of_Birth' | 'Closing_Date' | 'Due_Date' | 'Invoice_Date';
|
||||||
|
|
||||||
type DateField = { Date_of_Birth?: string; Closing_Date?: string; Due_Date?: string; Invoice_Date?: string };
|
export type AllFields =
|
||||||
|
{ [Date in DateType]?: string } &
|
||||||
|
{ [Location in LocationType]?: { address_fields: { [key: string]: string } } } &
|
||||||
|
{ Account_Name?: { account_name_fields: { [key: string]: string } } } &
|
||||||
|
IDataObject;
|
||||||
|
|
|
@ -294,7 +294,7 @@ export class ZohoCrm implements INodeType {
|
||||||
} else if (resource === 'deal') {
|
} else if (resource === 'deal') {
|
||||||
|
|
||||||
// **********************************************************************
|
// **********************************************************************
|
||||||
// deal
|
// deal
|
||||||
// **********************************************************************
|
// **********************************************************************
|
||||||
|
|
||||||
// https://www.zoho.com/crm/developer/docs/api/v2/deals-response.html
|
// https://www.zoho.com/crm/developer/docs/api/v2/deals-response.html
|
||||||
|
|
Loading…
Reference in a new issue