2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-07-12 09:12:32 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-07-12 09:12:32 -07:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2022-03-27 01:38:49 -07:00
|
|
|
JsonObject,
|
2020-07-12 09:12:32 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { xeroApiRequest, xeroApiRequestAllItems } from './GenericFunctions';
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { invoiceFields, invoiceOperations } from './InvoiceDescription';
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { contactFields, contactOperations } from './ContactDescription';
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IInvoice, ILineItem } from './InvoiceInterface';
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IAddress, IContact, IPhone } from './IContactInterface';
|
2020-07-12 09:12:32 -07:00
|
|
|
|
|
|
|
export class Xero implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Xero',
|
|
|
|
name: 'xero',
|
2021-03-25 09:10:02 -07:00
|
|
|
icon: 'file:xero.svg',
|
2020-07-12 09:12:32 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Xero API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Xero',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'xeroOAuth2Api',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-07-12 09:12:32 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-07-12 09:12:32 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contact',
|
|
|
|
value: 'contact',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Invoice',
|
|
|
|
value: 'invoice',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'invoice',
|
|
|
|
},
|
|
|
|
// CONTACT
|
|
|
|
...contactOperations,
|
|
|
|
...contactFields,
|
|
|
|
// INVOICE
|
|
|
|
...invoiceOperations,
|
|
|
|
...invoiceFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the item codes to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getItemCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { Items: items } = await xeroApiRequest.call(this, 'GET', '/items', {
|
|
|
|
organizationId,
|
|
|
|
});
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const item of items) {
|
|
|
|
const itemName = item.Description;
|
|
|
|
const itemId = item.Code;
|
|
|
|
returnData.push({
|
|
|
|
name: itemName,
|
|
|
|
value: itemId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the account codes to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getAccountCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { Accounts: accounts } = await xeroApiRequest.call(this, 'GET', '/Accounts', {
|
|
|
|
organizationId,
|
|
|
|
});
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const account of accounts) {
|
|
|
|
const accountName = account.Name;
|
|
|
|
const accountId = account.Code;
|
|
|
|
returnData.push({
|
|
|
|
name: accountName,
|
|
|
|
value: accountId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the tenants to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getTenants(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const tenants = await xeroApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'https://api.xero.com/connections',
|
|
|
|
);
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const tenant of tenants) {
|
|
|
|
const tenantName = tenant.tenantName;
|
|
|
|
const tenantId = tenant.tenantId;
|
|
|
|
returnData.push({
|
|
|
|
name: tenantName,
|
|
|
|
value: tenantId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the brading themes to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getBrandingThemes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { BrandingThemes: themes } = await xeroApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/BrandingThemes',
|
|
|
|
{ organizationId },
|
|
|
|
);
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const theme of themes) {
|
|
|
|
const themeName = theme.Name;
|
|
|
|
const themeId = theme.BrandingThemeID;
|
|
|
|
returnData.push({
|
|
|
|
name: themeName,
|
|
|
|
value: themeId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the brading themes to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getCurrencies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { Currencies: currencies } = await xeroApiRequest.call(this, 'GET', '/Currencies', {
|
|
|
|
organizationId,
|
|
|
|
});
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const currency of currencies) {
|
|
|
|
const currencyName = currency.Code;
|
|
|
|
const currencyId = currency.Description;
|
|
|
|
returnData.push({
|
|
|
|
name: currencyName,
|
|
|
|
value: currencyId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the tracking categories to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// select them easily
|
|
|
|
async getTrakingCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { TrackingCategories: categories } = await xeroApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/TrackingCategories',
|
|
|
|
{ organizationId },
|
|
|
|
);
|
2020-07-12 09:12:32 -07:00
|
|
|
for (const category of categories) {
|
|
|
|
const categoryName = category.Name;
|
|
|
|
const categoryId = category.TrackingCategoryID;
|
|
|
|
returnData.push({
|
|
|
|
name: categoryName,
|
|
|
|
value: categoryId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2023-04-19 07:00:49 -07:00
|
|
|
// // Get all the tracking categories to display them to user so that they can
|
2020-07-12 09:12:32 -07:00
|
|
|
// // select them easily
|
|
|
|
// async getTrakingOptions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
// const organizationId = this.getCurrentNodeParameter('organizationId');
|
|
|
|
// const name = this.getCurrentNodeParameter('name');
|
|
|
|
// const returnData: INodePropertyOptions[] = [];
|
|
|
|
// const { TrackingCategories: categories } = await xeroApiRequest.call(this, 'GET', '/TrackingCategories', { organizationId });
|
|
|
|
// const { Options: options } = categories.filter((category: IDataObject) => category.Name === name)[0];
|
|
|
|
// for (const option of options) {
|
|
|
|
// const optionName = option.Name;
|
|
|
|
// const optionId = option.TrackingOptionID;
|
|
|
|
// returnData.push({
|
|
|
|
// name: optionName,
|
|
|
|
// value: optionId,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// return returnData;
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-07-12 09:12:32 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.xero.com/documentation/api/invoices
|
|
|
|
if (resource === 'invoice') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
|
|
|
const type = this.getNodeParameter('type', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const lineItemsValues = (this.getNodeParameter('lineItemsUi', i) as IDataObject)
|
|
|
|
.lineItemsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const body: IInvoice = {
|
2022-03-27 01:38:49 -07:00
|
|
|
organizationId,
|
|
|
|
Type: type,
|
|
|
|
Contact: { ContactID: contactId },
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
2020-07-12 09:12:32 -07:00
|
|
|
|
|
|
|
if (lineItemsValues) {
|
|
|
|
const lineItems: ILineItem[] = [];
|
|
|
|
for (const lineItemValue of lineItemsValues) {
|
|
|
|
const lineItem: ILineItem = {
|
|
|
|
Tracking: [],
|
|
|
|
};
|
|
|
|
lineItem.AccountCode = lineItemValue.accountCode as string;
|
|
|
|
lineItem.Description = lineItemValue.description as string;
|
|
|
|
lineItem.DiscountRate = lineItemValue.discountRate as string;
|
|
|
|
lineItem.ItemCode = lineItemValue.itemCode as string;
|
|
|
|
lineItem.LineAmount = lineItemValue.lineAmount as string;
|
|
|
|
lineItem.Quantity = (lineItemValue.quantity as number).toString();
|
|
|
|
lineItem.TaxAmount = lineItemValue.taxAmount as string;
|
|
|
|
lineItem.TaxType = lineItemValue.taxType as string;
|
|
|
|
lineItem.UnitAmount = lineItemValue.unitAmount as string;
|
|
|
|
// if (lineItemValue.trackingUi) {
|
|
|
|
// //@ts-ignore
|
|
|
|
// const { trackingValues } = lineItemValue.trackingUi as IDataObject[];
|
|
|
|
// if (trackingValues) {
|
|
|
|
// for (const trackingValue of trackingValues) {
|
|
|
|
// const tracking: IDataObject = {};
|
|
|
|
// tracking.Name = trackingValue.name as string;
|
|
|
|
// tracking.Option = trackingValue.option as string;
|
|
|
|
// lineItem.Tracking!.push(tracking);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
lineItems.push(lineItem);
|
|
|
|
}
|
|
|
|
body.LineItems = lineItems;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.brandingThemeId) {
|
|
|
|
body.BrandingThemeID = additionalFields.brandingThemeId as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.currency) {
|
|
|
|
body.CurrencyCode = additionalFields.currency as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.currencyRate) {
|
|
|
|
body.CurrencyRate = additionalFields.currencyRate as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.date) {
|
|
|
|
body.Date = additionalFields.date as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.dueDate) {
|
|
|
|
body.DueDate = additionalFields.dueDate as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.dueDate) {
|
|
|
|
body.DueDate = additionalFields.dueDate as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.expectedPaymentDate) {
|
|
|
|
body.ExpectedPaymentDate = additionalFields.expectedPaymentDate as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.invoiceNumber) {
|
|
|
|
body.InvoiceNumber = additionalFields.invoiceNumber as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.lineAmountType) {
|
2022-09-20 06:33:29 -07:00
|
|
|
body.LineAmountTypes = additionalFields.lineAmountType as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (additionalFields.plannedPaymentDate) {
|
|
|
|
body.PlannedPaymentDate = additionalFields.plannedPaymentDate as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.reference) {
|
|
|
|
body.Reference = additionalFields.reference as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.sendToContact) {
|
|
|
|
body.SentToContact = additionalFields.sendToContact as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.status) {
|
|
|
|
body.Status = additionalFields.status as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.url) {
|
|
|
|
body.Url = additionalFields.url as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await xeroApiRequest.call(this, 'POST', '/Invoices', body);
|
|
|
|
responseData = responseData.Invoices;
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const body: IInvoice = {
|
2022-03-27 01:38:49 -07:00
|
|
|
organizationId,
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (updateFields.lineItemsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const lineItemsValues = (updateFields.lineItemsUi as IDataObject)
|
|
|
|
.lineItemsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (lineItemsValues) {
|
|
|
|
const lineItems: ILineItem[] = [];
|
|
|
|
for (const lineItemValue of lineItemsValues) {
|
|
|
|
const lineItem: ILineItem = {
|
|
|
|
Tracking: [],
|
|
|
|
};
|
|
|
|
lineItem.AccountCode = lineItemValue.accountCode as string;
|
|
|
|
lineItem.Description = lineItemValue.description as string;
|
|
|
|
lineItem.DiscountRate = lineItemValue.discountRate as string;
|
|
|
|
lineItem.ItemCode = lineItemValue.itemCode as string;
|
|
|
|
lineItem.LineAmount = lineItemValue.lineAmount as string;
|
|
|
|
lineItem.Quantity = (lineItemValue.quantity as number).toString();
|
|
|
|
lineItem.TaxAmount = lineItemValue.taxAmount as string;
|
|
|
|
lineItem.TaxType = lineItemValue.taxType as string;
|
|
|
|
lineItem.UnitAmount = lineItemValue.unitAmount as string;
|
|
|
|
// if (lineItemValue.trackingUi) {
|
|
|
|
// //@ts-ignore
|
|
|
|
// const { trackingValues } = lineItemValue.trackingUi as IDataObject[];
|
|
|
|
// if (trackingValues) {
|
|
|
|
// for (const trackingValue of trackingValues) {
|
|
|
|
// const tracking: IDataObject = {};
|
|
|
|
// tracking.Name = trackingValue.name as string;
|
|
|
|
// tracking.Option = trackingValue.option as string;
|
|
|
|
// lineItem.Tracking!.push(tracking);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
lineItems.push(lineItem);
|
|
|
|
}
|
|
|
|
body.LineItems = lineItems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.type) {
|
|
|
|
body.Type = updateFields.type as string;
|
|
|
|
}
|
|
|
|
if (updateFields.Contact) {
|
2022-03-27 01:38:49 -07:00
|
|
|
body.Contact = { ContactID: updateFields.contactId as string };
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (updateFields.brandingThemeId) {
|
|
|
|
body.BrandingThemeID = updateFields.brandingThemeId as string;
|
|
|
|
}
|
|
|
|
if (updateFields.currency) {
|
|
|
|
body.CurrencyCode = updateFields.currency as string;
|
|
|
|
}
|
|
|
|
if (updateFields.currencyRate) {
|
|
|
|
body.CurrencyRate = updateFields.currencyRate as string;
|
|
|
|
}
|
|
|
|
if (updateFields.date) {
|
|
|
|
body.Date = updateFields.date as string;
|
|
|
|
}
|
|
|
|
if (updateFields.dueDate) {
|
|
|
|
body.DueDate = updateFields.dueDate as string;
|
|
|
|
}
|
|
|
|
if (updateFields.dueDate) {
|
|
|
|
body.DueDate = updateFields.dueDate as string;
|
|
|
|
}
|
|
|
|
if (updateFields.expectedPaymentDate) {
|
|
|
|
body.ExpectedPaymentDate = updateFields.expectedPaymentDate as string;
|
|
|
|
}
|
|
|
|
if (updateFields.invoiceNumber) {
|
|
|
|
body.InvoiceNumber = updateFields.invoiceNumber as string;
|
|
|
|
}
|
|
|
|
if (updateFields.lineAmountType) {
|
2022-09-20 06:33:29 -07:00
|
|
|
body.LineAmountTypes = updateFields.lineAmountType as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (updateFields.plannedPaymentDate) {
|
|
|
|
body.PlannedPaymentDate = updateFields.plannedPaymentDate as string;
|
|
|
|
}
|
|
|
|
if (updateFields.reference) {
|
|
|
|
body.Reference = updateFields.reference as string;
|
|
|
|
}
|
|
|
|
if (updateFields.sendToContact) {
|
|
|
|
body.SentToContact = updateFields.sendToContact as boolean;
|
|
|
|
}
|
|
|
|
if (updateFields.status) {
|
|
|
|
body.Status = updateFields.status as string;
|
|
|
|
}
|
|
|
|
if (updateFields.url) {
|
|
|
|
body.Url = updateFields.url as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await xeroApiRequest.call(this, 'POST', `/Invoices/${invoiceId}`, body);
|
|
|
|
responseData = responseData.Invoices;
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
|
|
|
const invoiceId = this.getNodeParameter('invoiceId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(this, 'GET', `/Invoices/${invoiceId}`, {
|
|
|
|
organizationId,
|
|
|
|
});
|
2020-07-12 09:12:32 -07:00
|
|
|
responseData = responseData.Invoices;
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.statuses) {
|
|
|
|
qs.statuses = (options.statuses as string[]).join(',');
|
|
|
|
}
|
|
|
|
if (options.orderBy) {
|
2022-08-17 08:50:24 -07:00
|
|
|
qs.order = `${options.orderBy} ${
|
|
|
|
options.sortOrder === undefined ? 'DESC' : options.sortOrder
|
|
|
|
}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (options.where) {
|
|
|
|
qs.where = options.where;
|
|
|
|
}
|
|
|
|
if (options.createdByMyApp) {
|
|
|
|
qs.createdByMyApp = options.createdByMyApp as boolean;
|
|
|
|
}
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'Invoices',
|
|
|
|
'GET',
|
|
|
|
'/Invoices',
|
|
|
|
{ organizationId },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/Invoices',
|
2022-08-17 08:50:24 -07:00
|
|
|
{ organizationId },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Invoices;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (resource === 'contact') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-03-27 01:38:49 -07:00
|
|
|
const addressesUi = additionalFields.addressesUi as IDataObject;
|
|
|
|
const phonesUi = additionalFields.phonesUi as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const body: IContact = {
|
2022-03-27 01:38:49 -07:00
|
|
|
Name: name,
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.accountNumber) {
|
|
|
|
body.AccountNumber = additionalFields.accountNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.bankAccountDetails) {
|
|
|
|
body.BankAccountDetails = additionalFields.bankAccountDetails as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.contactNumber) {
|
|
|
|
body.ContactNumber = additionalFields.contactNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.contactStatus) {
|
|
|
|
body.ContactStatus = additionalFields.contactStatus as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.defaultCurrency) {
|
|
|
|
body.DefaultCurrency = additionalFields.defaultCurrency as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.emailAddress) {
|
|
|
|
body.EmailAddress = additionalFields.emailAddress as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.firstName) {
|
|
|
|
body.FirstName = additionalFields.firstName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.lastName) {
|
|
|
|
body.LastName = additionalFields.lastName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.purchasesDefaultAccountCode) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.PurchasesDefaultAccountCode =
|
|
|
|
additionalFields.purchasesDefaultAccountCode as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.salesDefaultAccountCode) {
|
|
|
|
body.SalesDefaultAccountCode = additionalFields.salesDefaultAccountCode as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.skypeUserName) {
|
|
|
|
body.SkypeUserName = additionalFields.skypeUserName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.taxNumber) {
|
|
|
|
body.taxNumber = additionalFields.taxNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.xeroNetworkKey) {
|
|
|
|
body.xeroNetworkKey = additionalFields.xeroNetworkKey as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2022-03-27 01:38:49 -07:00
|
|
|
if (phonesUi) {
|
|
|
|
const phoneValues = phonesUi?.phonesValues as IDataObject[];
|
|
|
|
if (phoneValues) {
|
|
|
|
const phones: IPhone[] = [];
|
|
|
|
for (const phoneValue of phoneValues) {
|
|
|
|
const phone: IPhone = {};
|
|
|
|
phone.PhoneType = phoneValue.phoneType as string;
|
|
|
|
phone.PhoneNumber = phoneValue.phoneNumber as string;
|
|
|
|
phone.PhoneAreaCode = phoneValue.phoneAreaCode as string;
|
|
|
|
phone.PhoneCountryCode = phoneValue.phoneCountryCode as string;
|
|
|
|
phones.push(phone);
|
|
|
|
}
|
|
|
|
body.Phones = phones;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addressesUi) {
|
|
|
|
const addressValues = addressesUi?.addressesValues as IDataObject[];
|
|
|
|
if (addressValues) {
|
|
|
|
const addresses: IAddress[] = [];
|
|
|
|
for (const addressValue of addressValues) {
|
|
|
|
const address: IAddress = {};
|
|
|
|
address.AddressType = addressValue.type as string;
|
|
|
|
address.AddressLine1 = addressValue.line1 as string;
|
|
|
|
address.AddressLine2 = addressValue.line2 as string;
|
|
|
|
address.City = addressValue.city as string;
|
|
|
|
address.Region = addressValue.region as string;
|
|
|
|
address.PostalCode = addressValue.postalCode as string;
|
|
|
|
address.Country = addressValue.country as string;
|
|
|
|
address.AttentionTo = addressValue.attentionTo as string;
|
|
|
|
addresses.push(address);
|
|
|
|
}
|
|
|
|
body.Addresses = addresses;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(this, 'POST', '/Contacts', {
|
|
|
|
organizationId,
|
|
|
|
Contacts: [body],
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Contacts;
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(this, 'GET', `/Contacts/${contactId}`, {
|
|
|
|
organizationId,
|
|
|
|
});
|
2020-07-12 09:12:32 -07:00
|
|
|
responseData = responseData.Contacts;
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.includeArchived) {
|
|
|
|
qs.includeArchived = options.includeArchived as boolean;
|
|
|
|
}
|
|
|
|
if (options.orderBy) {
|
2022-08-17 08:50:24 -07:00
|
|
|
qs.order = `${options.orderBy} ${
|
|
|
|
options.sortOrder === undefined ? 'DESC' : options.sortOrder
|
|
|
|
}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (options.where) {
|
|
|
|
qs.where = options.where;
|
|
|
|
}
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'Contacts',
|
|
|
|
'GET',
|
|
|
|
'/Contacts',
|
|
|
|
{ organizationId },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/Contacts',
|
2022-08-17 08:50:24 -07:00
|
|
|
{ organizationId },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Contacts;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
|
|
const organizationId = this.getNodeParameter('organizationId', i) as string;
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2022-03-27 01:38:49 -07:00
|
|
|
const addressesUi = updateFields.addressesUi as IDataObject;
|
|
|
|
const phonesUi = updateFields.phonesUi as IDataObject;
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IContact = {};
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.accountNumber) {
|
|
|
|
body.AccountNumber = updateFields.accountNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.name) {
|
|
|
|
body.Name = updateFields.name as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.bankAccountDetails) {
|
|
|
|
body.BankAccountDetails = updateFields.bankAccountDetails as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.contactNumber) {
|
|
|
|
body.ContactNumber = updateFields.contactNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.contactStatus) {
|
|
|
|
body.ContactStatus = updateFields.contactStatus as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.defaultCurrency) {
|
|
|
|
body.DefaultCurrency = updateFields.defaultCurrency as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.emailAddress) {
|
|
|
|
body.EmailAddress = updateFields.emailAddress as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.firstName) {
|
|
|
|
body.FirstName = updateFields.firstName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.lastName) {
|
|
|
|
body.LastName = updateFields.lastName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.purchasesDefaultAccountCode) {
|
|
|
|
body.PurchasesDefaultAccountCode = updateFields.purchasesDefaultAccountCode as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.salesDefaultAccountCode) {
|
|
|
|
body.SalesDefaultAccountCode = updateFields.salesDefaultAccountCode as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.skypeUserName) {
|
|
|
|
body.SkypeUserName = updateFields.skypeUserName as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.taxNumber) {
|
|
|
|
body.taxNumber = updateFields.taxNumber as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.xeroNetworkKey) {
|
|
|
|
body.xeroNetworkKey = updateFields.xeroNetworkKey as string;
|
|
|
|
}
|
2020-07-12 09:12:32 -07:00
|
|
|
|
2022-03-27 01:38:49 -07:00
|
|
|
if (phonesUi) {
|
|
|
|
const phoneValues = phonesUi?.phonesValues as IDataObject[];
|
|
|
|
if (phoneValues) {
|
|
|
|
const phones: IPhone[] = [];
|
|
|
|
for (const phoneValue of phoneValues) {
|
|
|
|
const phone: IPhone = {};
|
|
|
|
phone.PhoneType = phoneValue.phoneType as string;
|
|
|
|
phone.PhoneNumber = phoneValue.phoneNumber as string;
|
|
|
|
phone.PhoneAreaCode = phoneValue.phoneAreaCode as string;
|
|
|
|
phone.PhoneCountryCode = phoneValue.phoneCountryCode as string;
|
|
|
|
phones.push(phone);
|
|
|
|
}
|
|
|
|
body.Phones = phones;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addressesUi) {
|
|
|
|
const addressValues = addressesUi?.addressesValues as IDataObject[];
|
|
|
|
if (addressValues) {
|
|
|
|
const addresses: IAddress[] = [];
|
|
|
|
for (const addressValue of addressValues) {
|
|
|
|
const address: IAddress = {};
|
|
|
|
address.AddressType = addressValue.type as string;
|
|
|
|
address.AddressLine1 = addressValue.line1 as string;
|
|
|
|
address.AddressLine2 = addressValue.line2 as string;
|
|
|
|
address.City = addressValue.city as string;
|
|
|
|
address.Region = addressValue.region as string;
|
|
|
|
address.PostalCode = addressValue.postalCode as string;
|
|
|
|
address.Country = addressValue.country as string;
|
|
|
|
address.AttentionTo = addressValue.attentionTo as string;
|
|
|
|
addresses.push(address);
|
|
|
|
}
|
|
|
|
body.Addresses = addresses;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await xeroApiRequest.call(this, 'POST', `/Contacts/${contactId}`, {
|
|
|
|
organizationId,
|
|
|
|
Contacts: [body],
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Contacts;
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ json: { error: (error as JsonObject).message } });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-07-12 09:12:32 -07:00
|
|
|
}
|
|
|
|
}
|