Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support getting Wise statements as JSON, CSV or PDF - N8N-3847 #3468

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions packages/nodes-base/nodes/Wise/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export async function wiseApiRequest(
delete options.qs;
}

if (option.encoding) {
delete options.json;
}

if (Object.keys(option)) {
Object.assign(options, option);
}
Expand Down Expand Up @@ -112,26 +116,6 @@ export async function wiseApiRequest(
}
}

/**
* Populate the binary property of node items with binary data for a PDF file.
*/
export async function handleBinaryData(
this: IExecuteFunctions,
items: INodeExecutionData[],
i: number,
endpoint: string,
) {
const data = await wiseApiRequest.call(this, 'GET', endpoint, {}, {}, { encoding: null });
const binaryProperty = this.getNodeParameter('binaryProperty', i) as string;

items[i].binary = items[i].binary ?? {};
items[i].binary![binaryProperty] = await this.helpers.prepareBinaryData(data);
items[i].binary![binaryProperty].fileName = this.getNodeParameter('fileName', i) as string;
items[i].binary![binaryProperty].fileExtension = 'pdf';

return items;
}

export function getTriggerName(eventName: string) {
const events: IDataObject = {
'tranferStateChange': 'transfers#state-change',
Expand Down
33 changes: 25 additions & 8 deletions packages/nodes-base/nodes/Wise/Wise.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import {
BorderlessAccount,
ExchangeRateAdditionalFields,
handleBinaryData,
Profile,
Recipient,
StatementAdditionalFields,
Expand Down Expand Up @@ -175,7 +174,7 @@ export class Wise implements INodeType {

let responseData;
const returnData: IDataObject[] = [];
let downloadReceipt = false;
let binaryOutput = false;

for (let i = 0; i < items.length; i++) {

Expand Down Expand Up @@ -221,7 +220,8 @@ export class Wise implements INodeType {

const profileId = this.getNodeParameter('profileId', i);
const borderlessAccountId = this.getNodeParameter('borderlessAccountId', i);
const endpoint = `v3/profiles/${profileId}/borderless-accounts/${borderlessAccountId}/statement.json`;
const format = this.getNodeParameter('format', i) as 'json' | 'csv' | 'pdf';
const endpoint = `v3/profiles/${profileId}/borderless-accounts/${borderlessAccountId}/statement.${format}`;

const qs = {
currency: this.getNodeParameter('currency', i),
Expand All @@ -241,8 +241,19 @@ export class Wise implements INodeType {
qs.intervalEnd = moment().utc().format();
}

responseData = await wiseApiRequest.call(this, 'GET', endpoint, {}, qs);
if (format === 'json') {
responseData = await wiseApiRequest.call(this, 'GET', endpoint, {}, qs);
}
else {
const data = await wiseApiRequest.call(this, 'GET', endpoint, {}, qs, {encoding: 'arraybuffer'});
const binaryProperty = this.getNodeParameter('binaryProperty', i) as string;

items[i].binary = items[i].binary ?? {};
items[i].binary![binaryProperty] = await this.helpers.prepareBinaryData(data, this.getNodeParameter('fileName', i) as string);

responseData = items;
binaryOutput = true;
}
}

} else if (resource === 'exchangeRate') {
Expand Down Expand Up @@ -454,14 +465,20 @@ export class Wise implements INodeType {
// ----------------------------------

const transferId = this.getNodeParameter('transferId', i);
downloadReceipt = this.getNodeParameter('downloadReceipt', i) as boolean;

const downloadReceipt = this.getNodeParameter('downloadReceipt', i) as boolean;
if (downloadReceipt) {

// https://api-docs.transferwise.com/#transfers-get-receipt-pdf

responseData = await handleBinaryData.call(this, items, i, `v1/transfers/${transferId}/receipt.pdf`);
const data = await wiseApiRequest.call(this, 'GET', `v1/transfers/${transferId}/receipt.pdf`, {}, {}, {encoding: 'arraybuffer'});
const binaryProperty = this.getNodeParameter('binaryProperty', i) as string;

items[i].binary = items[i].binary ?? {};
items[i].binary![binaryProperty] = await this.helpers.prepareBinaryData(data, this.getNodeParameter('fileName', i) as string);

responseData = items;
binaryOutput = true;
} else {

// https://api-docs.transferwise.com/#transfers-get-by-id
Expand Down Expand Up @@ -518,7 +535,7 @@ export class Wise implements INodeType {
: returnData.push(responseData);
}

if (downloadReceipt && responseData !== undefined) {
if (binaryOutput && responseData !== undefined) {
return this.prepareOutputData(responseData);
}

Expand Down
76 changes: 76 additions & 0 deletions packages/nodes-base/nodes/Wise/descriptions/AccountDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,82 @@ export const accountFields: INodeProperties[] = [
},
},
},
{
displayName: 'Format',
name: 'format',
type: 'options',
default: 'json',
description: 'File format to retrieve the statement in',
displayOptions: {
show: {
resource: [
'account',
],
operation: [
'getStatement',
],
},
},
options: [
{
name: 'JSON',
value: 'json',
},
{
name: 'CSV',
value: 'csv',
},
{
name: 'PDF',
value: 'pdf',
},
],
},
{
displayName: 'Binary Property',
name: 'binaryProperty',
type: 'string',
required: true,
default: 'data',
description: 'Name of the binary property to which to write to',
displayOptions: {
show: {
resource: [
'account',
],
operation: [
'getStatement',
],
format: [
'csv',
'pdf',
],
},
},
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
required: true,
default: '',
placeholder: 'data.pdf',
description: 'Name of the file that will be downloaded',
displayOptions: {
show: {
resource: [
'account',
],
operation: [
'getStatement',
],
format: [
'csv',
'pdf',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
Expand Down