n8n/packages/nodes-base/nodes/QuickBooks/GenericFunctions.ts
Iván Ovejero 5baa31b053
Add QuickBooks node (#1365)
* Add OAuth2 credentials

* Adjust credentials params

* Add node to listing

* Add initial node scaffolding

* Remove unused credentials params

* Add customer search with select statement

* Add pagination to customer search

* Add customer creation functionality

* Add customer update functionality

* Small formatting fix

* Adjust property name casing for consistency

* Adjust customer operations for consistency

* Handle large QuickBooks listings

* Add initial estimate resource description

* Add estimate resource retrieval operations

* Refactor customer billing address

* Simplify customer billing address

* Fix casing for customer additional fields

* Adjust types to accommodate loadOptions functions

* Add loadOptions for customers to estimate

* Sort customer update fields alphabetically

* Refactor estimate line into standalone file

* Add stub for PDF retrieval operation

* Add invoice resource description and execute branches

* Implement estimate PDF download functionality

* Place descriptions in their own dir

* Add get and getAll for invoices

* Add send functionality to invoices

* Refactor handling of binary data

* Add invoice voiding functionality

* Add invoice deletion functionality

* Refactor resources into subdirs and add interfaces

* Add get and getAll for bill

* Add payment description

* Add get and getAll for payment

* Make variables in endpoints consistent

* Refactor interfaces for consistency

* Add interface for item resource

* Fill in fields for all resources

* Minor fixes in defaults and descriptions

* Refactor loader

* Add all resources to execute function

* Fix line property duplication

* Add get and getAll for vendor

* Optimize description imports

* Add creation for customer and bill

* Add update operation for bill

* Refactor create and update for customer

* Implement employee create and update

* Implement create and update for estimate

* Make address params more consistent

* Add create and update to payment

* Add item operations

* Add create and delete operations for invoice

* Refactor binary data handler

* Refactor generic functions

* Add create and update operations for vendor

* Fix build

* Fix total amount in bill:update

* Fix balance in bill:update

* Remove currency from bill:update

* Implement reference retrieval in bill:update

* Fix failing params in customer:update

* Fix param in employee:update

* Implement reference retrieval in estimate:update

* Fix failing params in estimate:update

* Fix failing params in invoice:update

* Fix failing param in vendor:update

* Implement reference retrieval in payment:update

* Remove unused interfaces

* Rename line property function

* Remove hared directory

* Refactor reference and sync token retrieval

* Fix line structure in bill:create

* Fix line structure in estimate:create

* Flatten responses

* Refactor line processing

* Remove unused interfaces

* Add endpoint documentation

* Fix payment:void content type

* Fix default for bill line item

* Hide auth URI query parameters

* Hide auth header parameter

* Add switch for credentials environment

* Adjust OAuth2 callback to accommodate realmId

* Retrieve realmId from OAuth2 flow

*  Improvements

* Reposition dividers

* Add IDs to display names of reference fields

* Add estimate:delete and bill:delete

* Load items in lines for bill, estimate and invoice

* Add filename for binary property in PDF download

*  Improvements

* Adjust field description

* Implement estimate:send

* Adjust field description

* Adjust custom field descriptions

* Add missing period to description

*  Minor improvements on QuickBooks-Node

* Add descriptions for bill

* Add descriptions for customer

* Add descriptions for employee

* Add descriptions for estimate

* Add descriptions for invoice

* Add descriptions for payment

* Add descriptions for vendor

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-02-13 17:27:08 +01:00

437 lines
10 KiB
TypeScript

import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
} from 'n8n-workflow';
import {
CustomField,
GeneralAddress,
Ref,
} from './descriptions/Shared.interface';
import {
capitalCase,
} from 'change-case';
import {
pickBy,
} from 'lodash';
import {
OptionsWithUri,
} from 'request';
/**
* Make an authenticated API request to QuickBooks.
*/
export async function quickBooksApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
qs: IDataObject,
body: IDataObject,
option: IDataObject = {},
): Promise<any> { // tslint:disable-line:no-any
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let isDownload = false;
if (['estimate', 'invoice', 'payment'].includes(resource) && operation === 'get') {
isDownload = this.getNodeParameter('download', 0) as boolean;
}
const productionUrl = 'https://quickbooks.api.intuit.com';
const sandboxUrl = 'https://sandbox-quickbooks.api.intuit.com';
const credentials = this.getCredentials('quickBooksOAuth2Api') as IDataObject;
const options: OptionsWithUri = {
headers: {
'user-agent': 'n8n',
},
method,
uri: `${credentials.environment === 'sandbox' ? sandboxUrl : productionUrl}${endpoint}`,
qs,
body,
json: !isDownload,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
if (Object.keys(option)) {
Object.assign(options, option);
}
if (isDownload) {
options.headers!['Accept'] = 'application/pdf';
}
if (resource === 'invoice' && operation === 'send') {
options.headers!['Content-Type'] = 'application/octet-stream';
}
if (
(resource === 'invoice' && (operation === 'void' || operation === 'delete')) ||
(resource === 'payment' && (operation === 'void' || operation === 'delete'))
) {
options.headers!['Content-Type'] = 'application/json';
}
try {
return await this.helpers.requestOAuth2!.call(this, 'quickBooksOAuth2Api', options);
} catch (error) {
const errors = error.error.Fault.Error;
if (errors && Array.isArray(errors)) {
const errorMessage = errors.map(
(e) => `QuickBooks error response [${e.code}]: ${e.Message} - Detail: ${e.Detail}`,
).join('|');
throw new Error(errorMessage);
}
throw error;
}
}
/**
* Make an authenticated API request to QuickBooks and return all results.
*/
export async function quickBooksApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
qs: IDataObject,
body: IDataObject,
resource: string,
): Promise<any> { // tslint:disable-line:no-any
let responseData;
let startPosition = 1;
const maxResults = 1000;
const returnData: IDataObject[] = [];
const maxCount = await getCount.call(this, method, endpoint, qs);
const originalQuery = qs.query;
do {
qs.query = `${originalQuery} MAXRESULTS ${maxResults} STARTPOSITION ${startPosition}`;
responseData = await quickBooksApiRequest.call(this, method, endpoint, qs, body);
returnData.push(...responseData.QueryResponse[capitalCase(resource)]);
startPosition += maxResults;
} while (maxCount > returnData.length);
return returnData;
}
async function getCount(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
qs: IDataObject,
): Promise<any> { // tslint:disable-line:no-any
const responseData = await quickBooksApiRequest.call(this, method, endpoint, qs, {});
return responseData.QueryResponse.totalCount;
}
/**
* Handles a QuickBooks listing by returning all items or up to a limit.
*/
export async function handleListing(
this: IExecuteFunctions,
i: number,
endpoint: string,
resource: string,
): Promise<any> { // tslint:disable-line:no-any
let responseData;
const qs = {
query: `SELECT * FROM ${resource}`,
} as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i);
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.query) {
qs.query += ` ${filters.query}`;
}
if (returnAll) {
return await quickBooksApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, resource);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.query += ` MAXRESULTS ${limit}`;
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, qs, {});
responseData = responseData.QueryResponse[capitalCase(resource)];
return responseData;
}
}
/**
* Get the SyncToken required for delete and void operations in QuickBooks.
*/
export async function getSyncToken(
this: IExecuteFunctions,
i: number,
companyId: string,
resource: string,
) {
const resourceId = this.getNodeParameter(`${resource}Id`, i);
const getEndpoint = `/v3/company/${companyId}/${resource}/${resourceId}`;
const propertyName = capitalCase(resource);
const { [propertyName]: { SyncToken } } = await quickBooksApiRequest.call(this, 'GET', getEndpoint, {}, {});
return SyncToken;
}
/**
* Get the reference and SyncToken required for update operations in QuickBooks.
*/
export async function getRefAndSyncToken(
this: IExecuteFunctions,
i: number,
companyId: string,
resource: string,
ref: string,
) {
const resourceId = this.getNodeParameter(`${resource}Id`, i);
const endpoint = `/v3/company/${companyId}/${resource}/${resourceId}`;
const responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
return {
ref: responseData[capitalCase(resource)][ref],
syncToken: responseData[capitalCase(resource)].SyncToken,
};
}
/**
* Populate node items with binary data.
*/
export async function handleBinaryData(
this: IExecuteFunctions,
items: INodeExecutionData[],
i: number,
companyId: string,
resource: string,
resourceId: string,
) {
const binaryProperty = this.getNodeParameter('binaryProperty', i) as string;
const fileName = this.getNodeParameter('fileName', i) as string;
const endpoint = `/v3/company/${companyId}/${resource}/${resourceId}/pdf`;
const data = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {}, { encoding: null });
items[i].binary = items[i].binary ?? {};
items[i].binary![binaryProperty] = await this.helpers.prepareBinaryData(data);
items[i].binary![binaryProperty].fileName = fileName;
items[i].binary![binaryProperty].fileExtension = 'pdf';
return items;
}
export async function loadResource(
this: ILoadOptionsFunctions,
resource: string,
) {
const returnData: INodePropertyOptions[] = [];
const qs = {
query: `SELECT * FROM ${resource}`,
} as IDataObject;
const { oauthTokenData: { callbackQueryString: { realmId } } } = this.getCredentials('quickBooksOAuth2Api') as { oauthTokenData: { callbackQueryString: { realmId: string } } };
const endpoint = `/v3/company/${realmId}/query`;
const resourceItems = await quickBooksApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, resource);
if (resource === 'preferences') {
const { SalesFormsPrefs: { CustomField } } = resourceItems[0];
const customFields = CustomField[1].CustomField;
for (const customField of customFields) {
const length = customField.Name.length;
returnData.push({
name: customField.StringValue,
value: customField.Name.charAt(length - 1),
});
}
return returnData;
}
resourceItems.forEach((resourceItem: { DisplayName: string, Name: string, Id: string }) => {
returnData.push({
name: resourceItem.DisplayName || resourceItem.Name,
value: resourceItem.Id,
});
});
return returnData;
}
/**
* Populate the `Line` property in a request body.
*/
export function processLines(
this: IExecuteFunctions,
body: IDataObject,
lines: IDataObject[],
resource: string,
) {
lines.forEach((line) => {
if (resource === 'bill') {
if (line.DetailType === 'AccountBasedExpenseLineDetail') {
line.AccountBasedExpenseLineDetail = {
AccountRef: {
value: line.accountId,
},
};
delete line.accountId;
} else if (line.DetailType === 'ItemBasedExpenseLineDetail') {
line.ItemBasedExpenseLineDetail = {
ItemRef: {
value: line.itemId,
},
};
delete line.itemId;
}
} else if (resource === 'estimate') {
if (line.DetailType === 'SalesItemLineDetail') {
line.SalesItemLineDetail = {
ItemRef: {
value: line.itemId,
},
};
delete line.itemId;
}
} else if (resource === 'invoice') {
if (line.DetailType === 'SalesItemLineDetail') {
line.SalesItemLineDetail = {
ItemRef: {
value: line.itemId,
},
};
delete line.itemId;
}
}
});
return lines;
}
/**
* Populate update fields or additional fields into a request body.
*/
export function populateFields(
this: IExecuteFunctions,
body: IDataObject,
fields: IDataObject,
resource: string,
) {
Object.entries(fields).forEach(([key, value]) => {
if (resource === 'bill') {
if (key.endsWith('Ref')) {
const { details } = value as { details: Ref };
body[key] = {
name: details.name,
value: details.value,
};
} else {
body[key] = value;
}
} else if (['customer', 'employee', 'vendor'].includes(resource)) {
if (key === 'BillAddr') {
const { details } = value as { details: GeneralAddress };
body.BillAddr = pickBy(details, detail => detail !== '');
} else if (key === 'PrimaryEmailAddr') {
body.PrimaryEmailAddr = {
Address: value,
};
} else if (key === 'PrimaryPhone') {
body.PrimaryPhone = {
FreeFormNumber: value,
};
} else {
body[key] = value;
}
} else if (resource === 'estimate' || resource === 'invoice') {
if (key === 'BillAddr' || key === 'ShipAddr') {
const { details } = value as { details: GeneralAddress };
body[key] = pickBy(details, detail => detail !== '');
} else if (key === 'BillEmail') {
body.BillEmail = {
Address: value,
};
} else if (key === 'CustomFields') {
const { Field } = value as { Field: CustomField[] };
body.CustomField = Field;
const length = (body.CustomField as CustomField[]).length;
for (let i = 0; i < length; i++) {
//@ts-ignore
body.CustomField[i]['Type'] = 'StringType';
}
} else if (key === 'CustomerMemo') {
body.CustomerMemo = {
value,
};
} else if (key.endsWith('Ref')) {
const { details } = value as { details: Ref };
body[key] = {
name: details.name,
value: details.value,
};
} else if (key === 'TotalTax') {
body.TxnTaxDetail = {
TotalTax: value,
};
} else {
body[key] = value;
}
} else if (resource === 'payment') {
body[key] = value;
}
});
return body;
}