2022-07-04 02:12:08 -07:00
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-02-13 08:27:08 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2021-02-13 08:27:08 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
billFields,
|
|
|
|
billOperations,
|
|
|
|
customerFields,
|
|
|
|
customerOperations,
|
|
|
|
employeeFields,
|
|
|
|
employeeOperations,
|
|
|
|
estimateFields,
|
|
|
|
estimateOperations,
|
|
|
|
invoiceFields,
|
|
|
|
invoiceOperations,
|
|
|
|
itemFields,
|
|
|
|
itemOperations,
|
|
|
|
paymentFields,
|
|
|
|
paymentOperations,
|
2021-06-27 04:21:11 -07:00
|
|
|
purchaseFields,
|
|
|
|
purchaseOperations,
|
2021-08-13 02:45:26 -07:00
|
|
|
transactionFields,
|
|
|
|
transactionOperations,
|
2021-02-13 08:27:08 -08:00
|
|
|
vendorFields,
|
|
|
|
vendorOperations,
|
|
|
|
} from './descriptions';
|
|
|
|
|
|
|
|
import {
|
2021-08-13 02:45:26 -07:00
|
|
|
adjustTransactionDates,
|
2021-02-13 08:27:08 -08:00
|
|
|
getRefAndSyncToken,
|
|
|
|
getSyncToken,
|
|
|
|
handleBinaryData,
|
|
|
|
handleListing,
|
|
|
|
loadResource,
|
|
|
|
populateFields,
|
|
|
|
processLines,
|
|
|
|
quickBooksApiRequest,
|
2021-08-13 02:45:26 -07:00
|
|
|
simplifyTransactionReport,
|
2021-02-13 08:27:08 -08:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { capitalCase } from 'change-case';
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { isEmpty } from 'lodash';
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { QuickBooksOAuth2Credentials, TransactionFields } from './types';
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2021-02-13 08:27:08 -08:00
|
|
|
export class QuickBooks implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2021-06-27 04:21:11 -07:00
|
|
|
displayName: 'QuickBooks Online',
|
2021-02-13 08:27:08 -08:00
|
|
|
name: 'quickbooks',
|
|
|
|
icon: 'file:quickbooks.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-06-27 04:21:11 -07:00
|
|
|
description: 'Consume the QuickBooks Online API',
|
2021-02-13 08:27:08 -08:00
|
|
|
defaults: {
|
2021-06-27 04:21:11 -07:00
|
|
|
name: 'QuickBooks Online',
|
2021-02-13 08:27:08 -08:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'quickBooksOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-02-13 08:27:08 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Bill',
|
|
|
|
value: 'bill',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
value: 'customer',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Employee',
|
|
|
|
value: 'employee',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Estimate',
|
|
|
|
value: 'estimate',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Invoice',
|
|
|
|
value: 'invoice',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Item',
|
|
|
|
value: 'item',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Payment',
|
|
|
|
value: 'payment',
|
|
|
|
},
|
2021-06-27 04:21:11 -07:00
|
|
|
{
|
|
|
|
name: 'Purchase',
|
|
|
|
value: 'purchase',
|
|
|
|
},
|
2021-08-13 02:45:26 -07:00
|
|
|
{
|
|
|
|
name: 'Transaction',
|
|
|
|
value: 'transaction',
|
|
|
|
},
|
2021-02-13 08:27:08 -08:00
|
|
|
{
|
|
|
|
name: 'Vendor',
|
|
|
|
value: 'vendor',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'customer',
|
|
|
|
},
|
|
|
|
...billOperations,
|
|
|
|
...billFields,
|
|
|
|
...customerOperations,
|
|
|
|
...customerFields,
|
|
|
|
...employeeOperations,
|
|
|
|
...employeeFields,
|
|
|
|
...estimateOperations,
|
|
|
|
...estimateFields,
|
|
|
|
...invoiceOperations,
|
|
|
|
...invoiceFields,
|
|
|
|
...itemOperations,
|
|
|
|
...itemFields,
|
|
|
|
...paymentOperations,
|
|
|
|
...paymentFields,
|
2021-06-27 04:21:11 -07:00
|
|
|
...purchaseOperations,
|
|
|
|
...purchaseFields,
|
2021-08-13 02:45:26 -07:00
|
|
|
...transactionOperations,
|
|
|
|
...transactionFields,
|
2021-02-13 08:27:08 -08:00
|
|
|
...vendorOperations,
|
|
|
|
...vendorFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getCustomers(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'customer');
|
|
|
|
},
|
|
|
|
|
|
|
|
async getCustomFields(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'preferences');
|
|
|
|
},
|
|
|
|
|
2021-08-13 02:45:26 -07:00
|
|
|
async getDepartments(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'department');
|
|
|
|
},
|
|
|
|
|
2021-02-13 08:27:08 -08:00
|
|
|
async getItems(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'item');
|
|
|
|
},
|
|
|
|
|
2021-08-13 02:45:26 -07:00
|
|
|
async getMemos(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'CreditMemo');
|
|
|
|
},
|
|
|
|
|
2021-06-27 04:21:11 -07:00
|
|
|
async getPurchases(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'purchase');
|
|
|
|
},
|
|
|
|
|
2022-06-03 11:35:24 -07:00
|
|
|
async getTaxCodeRefs(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'TaxCode');
|
|
|
|
},
|
|
|
|
|
2021-08-13 02:45:26 -07:00
|
|
|
async getTerms(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'Term');
|
|
|
|
},
|
|
|
|
|
2021-02-13 08:27:08 -08:00
|
|
|
async getVendors(this: ILoadOptionsFunctions) {
|
|
|
|
return await loadResource.call(this, 'vendor');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
let responseData;
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { oauthTokenData } = (await this.getCredentials(
|
|
|
|
'quickBooksOAuth2Api',
|
|
|
|
)) as QuickBooksOAuth2Credentials;
|
2021-02-13 08:27:08 -08:00
|
|
|
const companyId = oauthTokenData.callbackQueryString.realmId;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'bill') {
|
|
|
|
// *********************************************************************
|
|
|
|
// bill
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/bill
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// bill: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!lines.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one line for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
lines.some(
|
|
|
|
(line) =>
|
|
|
|
line.DetailType === undefined ||
|
|
|
|
line.Amount === undefined ||
|
|
|
|
line.Description === undefined,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter detail type, amount and description for every line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
lines.forEach((line) => {
|
|
|
|
if (
|
|
|
|
line.DetailType === 'AccountBasedExpenseLineDetail' &&
|
|
|
|
line.accountId === undefined
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter an account ID for the associated line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
line.DetailType === 'ItemBasedExpenseLineDetail' &&
|
|
|
|
line.itemId === undefined
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter an item ID for the associated line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
});
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
VendorRef: {
|
|
|
|
value: this.getNodeParameter('VendorRef', i),
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
|
|
|
|
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// bill: delete
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
operation: 'delete',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body = {
|
|
|
|
Id: this.getNodeParameter('billId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// bill: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const billId = this.getNodeParameter('billId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${billId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// bill: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// bill: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(
|
|
|
|
this,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
'VendorRef',
|
|
|
|
);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('billId', i),
|
|
|
|
SyncToken: syncToken,
|
|
|
|
sparse: true,
|
|
|
|
VendorRef: {
|
|
|
|
name: ref.name,
|
|
|
|
value: ref.value,
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
|
|
|
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'customer') {
|
|
|
|
// *********************************************************************
|
|
|
|
// customer
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/customer
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// customer: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
DisplayName: this.getNodeParameter('displayName', i),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// customer: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const customerId = this.getNodeParameter('customerId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${customerId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// customer: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// customer: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('customerId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
sparse: true,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'employee') {
|
|
|
|
// *********************************************************************
|
|
|
|
// employee
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// employee: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
FamilyName: this.getNodeParameter('FamilyName', i),
|
|
|
|
GivenName: this.getNodeParameter('GivenName', i),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// employee: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const employeeId = this.getNodeParameter('employeeId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${employeeId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// employee: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// employee: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('employeeId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
sparse: true,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'estimate') {
|
|
|
|
// *********************************************************************
|
|
|
|
// estimate
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/estimate
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!lines.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one line for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
lines.some(
|
|
|
|
(line) =>
|
|
|
|
line.DetailType === undefined ||
|
|
|
|
line.Amount === undefined ||
|
|
|
|
line.Description === undefined,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter detail type, amount and description for every line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
lines.forEach((line) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
if (line.DetailType === 'SalesItemLineDetail' && line.itemId === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter an item ID for the associated line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
});
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
CustomerRef: {
|
|
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: delete
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
operation: 'delete',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body = {
|
|
|
|
Id: this.getNodeParameter('estimateId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const estimateId = this.getNodeParameter('estimateId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (download) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await handleBinaryData.call(
|
|
|
|
this,
|
|
|
|
items,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
estimateId,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${estimateId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: send
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const estimateId = this.getNodeParameter('estimateId', i) as string;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${estimateId}/send`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// estimate: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(
|
|
|
|
this,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
'CustomerRef',
|
|
|
|
);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('estimateId', i),
|
|
|
|
SyncToken: syncToken,
|
|
|
|
sparse: true,
|
|
|
|
CustomerRef: {
|
|
|
|
name: ref.name,
|
|
|
|
value: ref.value,
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'invoice') {
|
|
|
|
// *********************************************************************
|
|
|
|
// invoice
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/invoice
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!lines.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one line for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
lines.some(
|
|
|
|
(line) =>
|
|
|
|
line.DetailType === undefined ||
|
|
|
|
line.Amount === undefined ||
|
|
|
|
line.Description === undefined,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter detail type, amount and description for every line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
lines.forEach((line) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
if (line.DetailType === 'SalesItemLineDetail' && line.itemId === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Please enter an item ID for the associated line.',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
});
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
CustomerRef: {
|
|
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: delete
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
operation: 'delete',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body = {
|
|
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (download) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await handleBinaryData.call(
|
|
|
|
this,
|
|
|
|
items,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
invoiceId,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${invoiceId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: send
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${invoiceId}/send`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(
|
|
|
|
this,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
'CustomerRef',
|
|
|
|
);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
|
|
SyncToken: syncToken,
|
|
|
|
sparse: true,
|
|
|
|
CustomerRef: {
|
|
|
|
name: ref.name,
|
|
|
|
value: ref.value,
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'void') {
|
|
|
|
// ----------------------------------
|
|
|
|
// invoice: void
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
operation: 'void',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'item') {
|
|
|
|
// *********************************************************************
|
|
|
|
// item
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/item
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const item = this.getNodeParameter('itemId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${item}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
}
|
|
|
|
} else if (resource === 'payment') {
|
|
|
|
// *********************************************************************
|
|
|
|
// payment
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/payment
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
CustomerRef: {
|
|
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
|
|
},
|
|
|
|
TotalAmt: this.getNodeParameter('TotalAmt', i),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: delete
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
operation: 'delete',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body = {
|
|
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const paymentId = this.getNodeParameter('paymentId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (download) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await handleBinaryData.call(
|
|
|
|
this,
|
|
|
|
items,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
paymentId,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${paymentId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: send
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const paymentId = this.getNodeParameter('paymentId', i) as string;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${paymentId}/send`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(
|
|
|
|
this,
|
|
|
|
i,
|
|
|
|
companyId,
|
|
|
|
resource,
|
|
|
|
'CustomerRef',
|
|
|
|
);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
|
|
SyncToken: syncToken,
|
|
|
|
sparse: true,
|
|
|
|
CustomerRef: {
|
|
|
|
name: ref.name,
|
|
|
|
value: ref.value,
|
|
|
|
},
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'void') {
|
|
|
|
// ----------------------------------
|
|
|
|
// payment: void
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs = {
|
|
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
operation: 'void',
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'purchase') {
|
|
|
|
// *********************************************************************
|
|
|
|
// purchase
|
|
|
|
// *********************************************************************
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/purchase
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// purchase: get
|
|
|
|
// ----------------------------------
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const purchaseId = this.getNodeParameter('purchaseId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${purchaseId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// purchase: getAll
|
|
|
|
// ----------------------------------
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
}
|
2021-08-13 02:45:26 -07:00
|
|
|
} else if (resource === 'transaction') {
|
|
|
|
// *********************************************************************
|
|
|
|
// transaction
|
|
|
|
// *********************************************************************
|
|
|
|
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/transactionlist
|
|
|
|
|
|
|
|
if (operation === 'getReport') {
|
|
|
|
// ----------------------------------
|
|
|
|
// transaction: getReport
|
|
|
|
// ----------------------------------
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { columns, memo, term, customer, vendor, ...rest } = this.getNodeParameter(
|
|
|
|
'filters',
|
|
|
|
i,
|
|
|
|
) as TransactionFields;
|
2021-08-13 02:45:26 -07:00
|
|
|
|
|
|
|
let qs = { ...rest };
|
|
|
|
|
|
|
|
if (columns?.length) {
|
|
|
|
qs.columns = columns.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memo?.length) {
|
|
|
|
qs.memo = memo.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (term?.length) {
|
|
|
|
qs.term = term.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (customer?.length) {
|
|
|
|
qs.customer = customer.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vendor?.length) {
|
|
|
|
qs.vendor = vendor.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
qs = adjustTransactionDates(qs);
|
|
|
|
|
|
|
|
const endpoint = `/v3/company/${companyId}/reports/TransactionList`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, qs, {});
|
|
|
|
|
|
|
|
const simplifyResponse = this.getNodeParameter('simple', i, true) as boolean;
|
|
|
|
|
|
|
|
if (!Object.keys(responseData?.Rows).length) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simplifyResponse && !Array.isArray(responseData)) {
|
|
|
|
responseData = simplifyTransactionReport(responseData);
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'vendor') {
|
|
|
|
// *********************************************************************
|
|
|
|
// vendor
|
|
|
|
// *********************************************************************
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/vendor
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// vendor: create
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
DisplayName: this.getNodeParameter('displayName', i),
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// vendor: get
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const vendorId = this.getNodeParameter('vendorId', i);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}/${vendorId}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'GET', endpoint, {}, {});
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// vendor: getAll
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// vendor: update
|
|
|
|
// ----------------------------------
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let body = {
|
|
|
|
Id: this.getNodeParameter('vendorId', i),
|
|
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
|
|
sparse: true,
|
|
|
|
} as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isEmpty(updateFields)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}.`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = populateFields.call(this, body, updateFields, resource);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
|
|
responseData = responseData[capitalCase(resource)];
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', 0, false);
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
['invoice', 'estimate', 'payment'].includes(resource) &&
|
|
|
|
['get'].includes(operation) &&
|
|
|
|
download
|
|
|
|
) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// in this case responseDate? === items
|
2022-08-17 08:50:24 -07:00
|
|
|
if (!responseData) {
|
2021-07-19 23:58:54 -07:00
|
|
|
items[i].json = { error: error.message };
|
|
|
|
responseData = items;
|
2022-08-17 08:50:24 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData[i].json = { error: error.message };
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2021-06-27 04:21:11 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push(...executionData);
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', 0, false);
|
2021-02-13 08:27:08 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
['invoice', 'estimate', 'payment'].includes(resource) &&
|
|
|
|
['get'].includes(operation) &&
|
|
|
|
download
|
|
|
|
) {
|
2021-02-13 08:27:08 -08:00
|
|
|
return this.prepareOutputData(responseData);
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-02-13 08:27:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|