Skip to content

Commit

Permalink
Added the function getTransaction (#33)
Browse files Browse the repository at this point in the history
* Added the function getTransaction which will return your transactions from a start date untill an end date

* Restored the old dist folder, because I do not know how to create it the same way as in the main project

* Removed the tsconfig file
  • Loading branch information
Stef-Halmans committed Dec 23, 2021
1 parent 20d6321 commit 63c5ecd
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/DeGiro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
i18nMessagesType,
WebSettingsType,
GetPopularStocksConfigType,
GetTransactionsOptionsType,
TransactionType,
} from './types'

// Import requests
Expand All @@ -58,7 +60,9 @@ import {
getAccountReportsRequest,
getCashFundstRequest,
getPopularStocksRequest,
getTransactionsRequest,
} from './api'
import { runInThisContext } from 'vm'

/**
* @class DeGiro
Expand Down Expand Up @@ -236,7 +240,7 @@ export class DeGiro implements DeGiroClassInterface {
return reject('You must log in first')
}
getPortfolioRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, config)
.then(portfolio => this.completePortfolioDetails(portfolio, config.getProductDetails || false))
.then(portfolio => this.completePortfolioDetails(portfolio, config.getProductDetails || false))
.then(resolve)
.catch(reject)
})
Expand All @@ -253,7 +257,7 @@ export class DeGiro implements DeGiroClassInterface {
next(null, position)
})
.catch(error => next(error))
// tslint:disable-next-line: align
// tslint:disable-next-line: align
}, (error, portfolio) => {
if (error) return reject(error)
resolve(<any[]>portfolio)
Expand All @@ -278,7 +282,7 @@ export class DeGiro implements DeGiroClassInterface {

/* Orders methods */

getOrders (config: GetOrdersConfigType): Promise<GetOrdersResultType> {
getOrders(config: GetOrdersConfigType): Promise<GetOrdersResultType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
Expand Down Expand Up @@ -312,6 +316,13 @@ export class DeGiro implements DeGiroClassInterface {
return deleteOrderRequest(orderId, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}

getTransactions(options: GetTransactionsOptionsType): Promise<TransactionType[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first');
}
return getTransactionsRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, options);
}

/* Miscellaneous methods */

getProductsByIds(ids: string[]): Promise<any[]> {
Expand Down
42 changes: 42 additions & 0 deletions src/api/getTransactionsRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { DEGIRO_API_PATHS } from "../enums";
import { AccountConfigType, AccountDataType, GetTransactionsOptionsType, TransactionType } from "../types"
import { debug } from "../utils";
const { GET_TRANSACTIONS_PATH } = DEGIRO_API_PATHS;

export function getTransactionsRequest(accountData: AccountDataType, accountConfig: AccountConfigType, config: GetTransactionsOptionsType): Promise<TransactionType[]> {
return new Promise((resolve, reject) => {
// Create params to get orders by types
let params = ''
params += `fromDate=${encodeURIComponent(config.fromDate)}&`
params += `toDate=${encodeURIComponent(config.toDate)}&`
params += `groupTransactionsByOrder`
params += `intAccount=${accountData.data.intAccount}&`
params += `sessionId=${accountConfig.data.sessionId}`

const requestOptions: {
method?: string,
body?: string,
headers: {
[key: string]: string,
},
credentials: 'include',
referer: string,
} = {
headers: {
Cookie: `JSESSIONID=${accountConfig.data.sessionId};`,
},
credentials: 'include',
referer: 'https://trader.degiro.nl/trader/',
}

// Do the request to get a account config data
const uri = `${accountConfig.data.reportingUrl}${GET_TRANSACTIONS_PATH}?${params}`
debug(`Making request to ${uri}`)
fetch(uri, requestOptions)
.then(res => res.json())
.then((res) => {
resolve(res.data);
})
.catch(reject)
})
}
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getWebUserSettingsRequest } from './getWebUserSettingsRequest'
import { getAccountReportsRequest } from './getAccountReportsRequest'
import { getCashFundstRequest } from './getCashFundstRequest'
import { getPopularStocksRequest } from './getPopularStocksRequest'
import { getTransactionsRequest } from './getTransactionsRequest'

export {
loginRequest,
Expand All @@ -42,4 +43,5 @@ export {
getAccountReportsRequest,
getCashFundstRequest,
getPopularStocksRequest,
getTransactionsRequest,
}
1 change: 1 addition & 0 deletions src/enums/DeGiroEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum DEGIRO_API_PATHS {
GET_ACCOUNT_CONFIG_PATH = 'login/secure/config',
GET_GENERIC_DATA_PATH = 'v5/update/',
CREATE_ORDER_PATH = 'v5/checkOrder',
GET_TRANSACTIONS_PATH = 'v4/transactions',
GET_ACCOUNT_STATE_PATH = 'v6/accountoverview',
GET_ACCOUNT_INFO_PATH = 'v5/account/info/',
GET_LATESTS_NEWS_PATH = 'newsfeed/v2/latest-news',
Expand Down
5 changes: 5 additions & 0 deletions src/types/GetTransactionsOptionsType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GetTransactionsOptionsType = {
fromDate: string,
toDate: string,
groupTransactionsByOrder: boolean
}
16 changes: 16 additions & 0 deletions src/types/TransactionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type TransactionType = {
buysell: string,
counterParty: string,
date: string,
id: number,
orderTypeId: number,
price: number,
productId: number,
quantity: number,
total: number,
totalInBaseCurrency: number,
totalPlusFeeInBaseCurrency: number,
tradingVenue: string,
transactionTypeId: number,
transfered: boolean
}
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { WebSettingsType } from './WebSettingsType'
import { WebUserSettingType } from './WebUserSettingType'
import { i18nMessagesType } from './i18nMessagesType'
import { GetPopularStocksConfigType } from './GetPopularStocksConfigType'
import { GetTransactionsOptionsType } from './GetTransactionsOptionsType'
import { TransactionType } from './TransactionType'

import {
ConfigDictionaryType,
Expand All @@ -45,6 +47,7 @@ import {
Country,
} from './ConfigDictionaryType'


export {
CashFoundType,
DeGiroSettupType,
Expand All @@ -63,12 +66,14 @@ export {
IsLoginOptionsType,
GetOrdersConfigType,
GetOrdersResultType,
GetTransactionsOptionsType,
GetAccountStateOptionsType,
AccountReportsType,
ReportType,
AccountInfoType,
FavouriteProductType,
StockType,
TransactionType,
GetHistoricalOrdersOptionsType,
HistoricalOrdersType,
GetNewsOptionsType,
Expand Down

0 comments on commit 63c5ecd

Please sign in to comment.