mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
d6239d5bfb
* Update Compression node * Update Crypto node * Update DateTime node * Update EditImage node * Update EmailSend node * Update ExecuteWorkflow node * Update FTP node * Update Function node * Update FunctionItem node * Update ExecuteCommand node * Update OpenWeatherMap node * Update ReadBinaryFile node * Update ReadPdf node * Update RssFeedRead node & add URL validation * Update SpreadsheetFile node * Update Switch node * Update WriteBinaryFile node * Update Xml node * Update ActiveCampaign node * Update Airtable node * Update ApiTemplateIo node * Update Asana node * Update AwsLambda node * Update AwsSns node * Update AwsComprehend node * Update AwsRekognition node * Update AwsS3 node * Fix Error item * Update AwsSes node * Update AwsSqs node * Update Amqp node * Update Bitly node * Update Box node * Update Brandfetch node * Update CircleCi node * Update Clearbit node * Update ClickUp node * Update Cockpit node * Update CoinGecko node * Update Contentful node * Update ConvertKit node * Update Cortex node * Update CustomerIo node * Update DeepL node * Update Demio node * Update Disqus node * Update Drift node * Update Dropbox node * Update GetResponse node * Refactor & Update Ghost node * Update Github node * Update Gitlab node * Update GoogleAnalytics node * Update GoogleBooks node * Update GoogleCalendar node * Update GoogleDrive node * Update Gmail node * Update GoogleSheets node * Update GoogleSlides node * Update GoogleTasks node * Update Gotify node * Update GraphQL node * Update HackerNews node * Update Harvest node * Update HtmlExtract node * Update Hubspot node * Update Hunter node * Update Intercom node * Update Kafka node * Refactor & update Line node * Update LinkedIn node * Update Mailchimp node * Update Mandrill node * Update Matrix node * Update Mautic node * Update Medium node * Update MessageBird node * Update Mindee node * Update Mocean node * Update MondayCom node * Update MicrosoftExcel node * Update MicrosoftOneDrive node * Update MicrosoftOutlook node * Update Affinity node * Update Chargebee node * Update Discourse node * Update Freshdesk node * Update YouTube node * Update InvoiceNinja node * Update MailerLite node * Update Mailgun node * Update Mailjet node * Update Mattermost node * Update Nasa node * Update NextCloud node * Update OpenThesaurus node * Update Orbit node * Update PagerDuty node * Update PayPal node * Update Peekalink node * Update Phantombuster node * Update PostHog node * Update ProfitWell node * Refactor & Update Pushbullet node * Update QuickBooks node * Update Raindrop node * Update Reddit node * Update Rocketchat node * Update S3 node * Update Salesforce node * Update SendGrid node * Update SentryIo node * Update Shopify node * Update Signl4 node * Update Slack node * Update Spontit node * Update Spotify node * Update Storyblok node * Refactor & Update Strapi node * Refactor & Update Strava node * Update Taiga node * Refactor & update Tapfiliate node * Update Telegram node * Update TheHive node * Update Todoist node * Update TravisCi node * Update Trello node * Update Twilio node * Update Twist node * Update Twitter node * Update Uplead node * Update UProc node * Update Vero node * Update Webflow node * Update Wekan node * Update Wordpress node * Update Xero node * Update Yourls node * Update Zendesk node * Update ZohoCrm node * Refactor & Update Zoom node * Update Zulip node * Update Clockify node * Update MongoDb node * Update MySql node * Update MicrosoftTeams node * Update Stackby node * Refactor Discourse node * Support corner-case in Github node update * Support corner-case in Gitlab node update * Refactor & Update GoogleContacts node * Refactor Mindee node * Update Coda node * Lint fixes * Update Beeminder node * Update Google Firebase RealtimeDatabase node * Update HelpScout node * Update Mailcheck node * Update Paddle node * Update Pipedrive node * Update Pushover node * Update Segment node * Refactor & Update Vonage node * Added new conditions to warnings on execute batch cmd * Added keep only properties flag * Fixed code for keep only props * Added dependencies for image editing Co-authored-by: dali <servfrdali@yahoo.fr>
1058 lines
34 KiB
TypeScript
1058 lines
34 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
billFields,
|
|
billOperations,
|
|
customerFields,
|
|
customerOperations,
|
|
employeeFields,
|
|
employeeOperations,
|
|
estimateFields,
|
|
estimateOperations,
|
|
invoiceFields,
|
|
invoiceOperations,
|
|
itemFields,
|
|
itemOperations,
|
|
paymentFields,
|
|
paymentOperations,
|
|
purchaseFields,
|
|
purchaseOperations,
|
|
vendorFields,
|
|
vendorOperations,
|
|
} from './descriptions';
|
|
|
|
import {
|
|
getRefAndSyncToken,
|
|
getSyncToken,
|
|
handleBinaryData,
|
|
handleListing,
|
|
loadResource,
|
|
populateFields,
|
|
processLines,
|
|
quickBooksApiRequest,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
capitalCase,
|
|
} from 'change-case';
|
|
|
|
import {
|
|
isEmpty,
|
|
} from 'lodash';
|
|
|
|
import {
|
|
QuickBooksOAuth2Credentials,
|
|
} from './types';
|
|
|
|
export class QuickBooks implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'QuickBooks Online',
|
|
name: 'quickbooks',
|
|
icon: 'file:quickbooks.svg',
|
|
group: ['transform'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume the QuickBooks Online API',
|
|
defaults: {
|
|
name: 'QuickBooks Online',
|
|
color: '#2CA01C',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'quickBooksOAuth2Api',
|
|
required: true,
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
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',
|
|
},
|
|
{
|
|
name: 'Purchase',
|
|
value: 'purchase',
|
|
},
|
|
{
|
|
name: 'Vendor',
|
|
value: 'vendor',
|
|
},
|
|
],
|
|
default: 'customer',
|
|
description: 'Resource to consume',
|
|
},
|
|
...billOperations,
|
|
...billFields,
|
|
...customerOperations,
|
|
...customerFields,
|
|
...employeeOperations,
|
|
...employeeFields,
|
|
...estimateOperations,
|
|
...estimateFields,
|
|
...invoiceOperations,
|
|
...invoiceFields,
|
|
...itemOperations,
|
|
...itemFields,
|
|
...paymentOperations,
|
|
...paymentFields,
|
|
...purchaseOperations,
|
|
...purchaseFields,
|
|
...vendorOperations,
|
|
...vendorFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
async getCustomers(this: ILoadOptionsFunctions) {
|
|
return await loadResource.call(this, 'customer');
|
|
},
|
|
|
|
async getCustomFields(this: ILoadOptionsFunctions) {
|
|
return await loadResource.call(this, 'preferences');
|
|
},
|
|
|
|
async getItems(this: ILoadOptionsFunctions) {
|
|
return await loadResource.call(this, 'item');
|
|
},
|
|
|
|
async getPurchases(this: ILoadOptionsFunctions) {
|
|
return await loadResource.call(this, 'purchase');
|
|
},
|
|
|
|
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;
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const { oauthTokenData } = this.getCredentials('quickBooksOAuth2Api') as QuickBooksOAuth2Credentials;
|
|
|
|
const companyId = oauthTokenData.callbackQueryString.realmId;
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
try {
|
|
if (resource === 'bill') {
|
|
|
|
// *********************************************************************
|
|
// bill
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/bill
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// bill: create
|
|
// ----------------------------------
|
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
|
|
|
if (!lines.length) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one line for the ${resource}.`);
|
|
}
|
|
|
|
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.');
|
|
}
|
|
|
|
lines.forEach(line => {
|
|
if (line.DetailType === 'AccountBasedExpenseLineDetail' && line.accountId === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'Please enter an account ID for the associated line.');
|
|
} else if (line.DetailType === 'ItemBasedExpenseLineDetail' && line.itemId === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'Please enter an item ID for the associated line.');
|
|
}
|
|
});
|
|
|
|
let body = {
|
|
VendorRef: {
|
|
value: this.getNodeParameter('VendorRef', i),
|
|
},
|
|
} as IDataObject;
|
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
// bill: delete
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
operation: 'delete',
|
|
} as IDataObject;
|
|
|
|
const body = {
|
|
Id: this.getNodeParameter('billId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
// bill: update
|
|
// ----------------------------------
|
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(this, i, companyId, resource, 'VendorRef');
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('billId', i),
|
|
SyncToken: syncToken,
|
|
sparse: true,
|
|
VendorRef: {
|
|
name: ref.name,
|
|
value: ref.value,
|
|
},
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'customer') {
|
|
|
|
// *********************************************************************
|
|
// customer
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/customer
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// customer: create
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
DisplayName: this.getNodeParameter('displayName', i),
|
|
} as IDataObject;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// customer: get
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
// customer: update
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('customerId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
sparse: true,
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'employee') {
|
|
|
|
// *********************************************************************
|
|
// employee
|
|
// *********************************************************************
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// employee: create
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
FamilyName: this.getNodeParameter('FamilyName', i),
|
|
GivenName: this.getNodeParameter('GivenName', i),
|
|
} as IDataObject;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// employee: get
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
// employee: update
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('employeeId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
sparse: true,
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'estimate') {
|
|
|
|
// *********************************************************************
|
|
// estimate
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/estimate
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// estimate: create
|
|
// ----------------------------------
|
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
|
|
|
if (!lines.length) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one line for the ${resource}.`);
|
|
}
|
|
|
|
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.');
|
|
}
|
|
|
|
lines.forEach(line => {
|
|
if (line.DetailType === 'SalesItemLineDetail' && line.itemId === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'Please enter an item ID for the associated line.');
|
|
}
|
|
});
|
|
|
|
let body = {
|
|
CustomerRef: {
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
},
|
|
} as IDataObject;
|
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
// estimate: delete
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
operation: 'delete',
|
|
} as IDataObject;
|
|
|
|
const body = {
|
|
Id: this.getNodeParameter('estimateId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const estimateId = this.getNodeParameter('estimateId', i) as string;
|
|
const download = this.getNodeParameter('download', i) as boolean;
|
|
|
|
if (download) {
|
|
|
|
responseData = await handleBinaryData.call(this, items, i, companyId, resource, estimateId);
|
|
|
|
} 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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
// estimate: send
|
|
// ----------------------------------
|
|
|
|
const estimateId = this.getNodeParameter('estimateId', i) as string;
|
|
|
|
const qs = {
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(this, i, companyId, resource, 'CustomerRef');
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('estimateId', i),
|
|
SyncToken: syncToken,
|
|
sparse: true,
|
|
CustomerRef: {
|
|
name: ref.name,
|
|
value: ref.value,
|
|
},
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'invoice') {
|
|
|
|
// *********************************************************************
|
|
// invoice
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/invoice
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// invoice: create
|
|
// ----------------------------------
|
|
|
|
const lines = this.getNodeParameter('Line', i) as IDataObject[];
|
|
|
|
if (!lines.length) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one line for the ${resource}.`);
|
|
}
|
|
|
|
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.');
|
|
}
|
|
|
|
lines.forEach(line => {
|
|
if (line.DetailType === 'SalesItemLineDetail' && line.itemId === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'Please enter an item ID for the associated line.');
|
|
}
|
|
});
|
|
|
|
let body = {
|
|
CustomerRef: {
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
},
|
|
} as IDataObject;
|
|
|
|
body.Line = processLines.call(this, body, lines, resource);
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
// invoice: delete
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
operation: 'delete',
|
|
} as IDataObject;
|
|
|
|
const body = {
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
|
const download = this.getNodeParameter('download', i) as boolean;
|
|
|
|
if (download) {
|
|
|
|
responseData = await handleBinaryData.call(this, items, i, companyId, resource, invoiceId);
|
|
|
|
} 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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
// invoice: send
|
|
// ----------------------------------
|
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
|
|
|
const qs = {
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(this, i, companyId, resource, 'CustomerRef');
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
SyncToken: syncToken,
|
|
sparse: true,
|
|
CustomerRef: {
|
|
name: ref.name,
|
|
value: ref.value,
|
|
},
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
} else if (operation === 'void') {
|
|
|
|
// ----------------------------------
|
|
// invoice: void
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
Id: this.getNodeParameter('invoiceId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
operation: 'void',
|
|
} as IDataObject;
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'item') {
|
|
|
|
// *********************************************************************
|
|
// item
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/item
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// item: get
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
}
|
|
|
|
} else if (resource === 'payment') {
|
|
|
|
// *********************************************************************
|
|
// payment
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/payment
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// payment: create
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
CustomerRef: {
|
|
value: this.getNodeParameter('CustomerRef', i),
|
|
},
|
|
TotalAmt: this.getNodeParameter('TotalAmt', i),
|
|
} as IDataObject;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
// payment: delete
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
operation: 'delete',
|
|
} as IDataObject;
|
|
|
|
const body = {
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const paymentId = this.getNodeParameter('paymentId', i) as string;
|
|
const download = this.getNodeParameter('download', i) as boolean;
|
|
|
|
if (download) {
|
|
|
|
responseData = await handleBinaryData.call(this, items, i, companyId, resource, paymentId);
|
|
|
|
} 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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'send') {
|
|
|
|
// ----------------------------------
|
|
// payment: send
|
|
// ----------------------------------
|
|
|
|
const paymentId = this.getNodeParameter('paymentId', i) as string;
|
|
|
|
const qs = {
|
|
sendTo: this.getNodeParameter('email', i) as string,
|
|
} as IDataObject;
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const { ref, syncToken } = await getRefAndSyncToken.call(this, i, companyId, resource, 'CustomerRef');
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
SyncToken: syncToken,
|
|
sparse: true,
|
|
CustomerRef: {
|
|
name: ref.name,
|
|
value: ref.value,
|
|
},
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
} else if (operation === 'void') {
|
|
|
|
// ----------------------------------
|
|
// payment: void
|
|
// ----------------------------------
|
|
|
|
const qs = {
|
|
Id: this.getNodeParameter('paymentId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
operation: 'void',
|
|
} as IDataObject;
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, qs, {});
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
}
|
|
|
|
} else if (resource === 'purchase') {
|
|
|
|
// *********************************************************************
|
|
// purchase
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/purchase
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// purchase: get
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
}
|
|
|
|
} else if (resource === 'vendor') {
|
|
|
|
// *********************************************************************
|
|
// vendor
|
|
// *********************************************************************
|
|
|
|
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/vendor
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// vendor: create
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
DisplayName: this.getNodeParameter('displayName', i),
|
|
} as IDataObject;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
body = populateFields.call(this, body, additionalFields, resource);
|
|
|
|
const endpoint = `/v3/company/${companyId}/${resource}`;
|
|
responseData = await quickBooksApiRequest.call(this, 'POST', endpoint, {}, body);
|
|
responseData = responseData[capitalCase(resource)];
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// vendor: get
|
|
// ----------------------------------
|
|
|
|
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
|
|
// ----------------------------------
|
|
|
|
const endpoint = `/v3/company/${companyId}/query`;
|
|
responseData = await handleListing.call(this, i, endpoint, resource);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
// vendor: update
|
|
// ----------------------------------
|
|
|
|
let body = {
|
|
Id: this.getNodeParameter('vendorId', i),
|
|
SyncToken: await getSyncToken.call(this, i, companyId, resource),
|
|
sparse: true,
|
|
} as IDataObject;
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
if (isEmpty(updateFields)) {
|
|
throw new NodeOperationError(this.getNode(), `Please enter at least one field to update for the ${resource}.`);
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
const download = this.getNodeParameter('download', 0, false) as boolean;
|
|
if (['invoice', 'estimate', 'payment'].includes(resource) && ['get'].includes(operation) && download) {
|
|
// in this case responseDate? === items
|
|
if (!responseData){
|
|
items[i].json = { error: error.message };
|
|
responseData = items;
|
|
}else {
|
|
responseData[i].json = { error: error.message };
|
|
}
|
|
}else {
|
|
returnData.push({ error: error.message });
|
|
}
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
Array.isArray(responseData)
|
|
? returnData.push(...responseData)
|
|
: returnData.push(responseData);
|
|
}
|
|
|
|
const download = this.getNodeParameter('download', 0, false) as boolean;
|
|
|
|
if (['invoice', 'estimate', 'payment'].includes(resource) && ['get'].includes(operation) && download) {
|
|
return this.prepareOutputData(responseData);
|
|
} else {
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|
|
}
|
|
|