mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 12:44:07 -08:00
feat(Wise Node): Add Support to download statements as JSON, CSV or PDF (#3468)
This commit is contained in:
parent
7346da0b34
commit
51663c1fcb
|
@ -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);
|
||||
}
|
||||
|
@ -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',
|
||||
|
|
|
@ -28,7 +28,6 @@ import {
|
|||
import {
|
||||
BorderlessAccount,
|
||||
ExchangeRateAdditionalFields,
|
||||
handleBinaryData,
|
||||
Profile,
|
||||
Recipient,
|
||||
StatementAdditionalFields,
|
||||
|
@ -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++) {
|
||||
|
||||
|
@ -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),
|
||||
|
@ -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') {
|
||||
|
@ -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
|
||||
|
@ -518,7 +535,7 @@ export class Wise implements INodeType {
|
|||
: returnData.push(responseData);
|
||||
}
|
||||
|
||||
if (downloadReceipt && responseData !== undefined) {
|
||||
if (binaryOutput && responseData !== undefined) {
|
||||
return this.prepareOutputData(responseData);
|
||||
}
|
||||
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in a new issue