From 4dbdd71f7388e41fc15b2a7fdae55796d9d54fa5 Mon Sep 17 00:00:00 2001 From: Bermi Ferrer Date: Sun, 28 Apr 2019 23:00:26 +0200 Subject: [PATCH] Adding support to get transactions for a given year or between two dates --- examples/get-transactions.js | 12 ++++++++++++ src/index.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 examples/get-transactions.js diff --git a/examples/get-transactions.js b/examples/get-transactions.js new file mode 100644 index 0000000..47d92c3 --- /dev/null +++ b/examples/get-transactions.js @@ -0,0 +1,12 @@ +const DeGiro = require('..'); + +(async () => { + const degiro = DeGiro.create({ + // username: 'your-username', + // password: 'your-password', + }); + + await degiro.login(); + + console.log(await degiro.getTransactions({year: 2019})); +})(); diff --git a/src/index.js b/src/index.js index 93e53ff..e3eb788 100644 --- a/src/index.js +++ b/src/index.js @@ -241,6 +241,41 @@ const create = ({ }); }; + /** + * Gets the transactions for a given year or between two dates + * + * @param {string} options.fromDate - JavaScript Date instance. Defaults to new Date() + * @param {string} options.toDate - JavaScript Date instance. Defaults to new Date() + * @param {string} options.year - When a year is provided options.fromDate and options.toDate + * will be set automatically to include all the transactions for the year. + * @param {number} options.groupTransactionsByOrder - Groups transactions by order. Defaults to false + * @return {Promise} + */ + const getTransactions = (options = { + fromDate: new Date(), + toDate: new Date(), + year: null, + groupTransactionsByOrder: false + }) => { + const formatDate = d => + d.toISOString().slice(0, 10).split("-").reverse().join("/"); + options.intAccount = session.account; + options.sessionId = session.id; + // Providing a year will set fromDate and toDate + if (options.year) { + options.fromDate = new Date(`${options.year}`); + options.toDate = new Date( + new Date(`${options.year + 1}`) - 3600001 + ); + } + options.fromDate = formatDate(options.fromDate); + options.toDate = formatDate(options.toDate); + const params = querystring.stringify(options); + log('getTransactions', params); + return fetch(`${urls.reportingUrl}/v4/transactions?${params}`) + .then(res => res.json()); + }; + /** * Get client info * @@ -484,6 +519,7 @@ const create = ({ login, searchProduct, getData, + getTransactions, getCashFunds, getPortfolio, getAskBidPrice,