feat(BankAccount and BankTransaction): now supporting the full retrieval process

This commit is contained in:
Philipp Kunz 2022-08-23 16:56:59 +02:00
parent d7ba62e767
commit 7637fca672
11 changed files with 6396 additions and 10001 deletions

16022
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,18 +13,19 @@
"build": "(tsbuild --web)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.61",
"@gitzone/tsbundle": "^1.0.102",
"@gitzone/tstest": "^1.0.70",
"@pushrocks/qenv": "^4.0.10",
"@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.25",
"@gitzone/tsbuild": "^2.1.65",
"@gitzone/tsbundle": "^2.0.7",
"@gitzone/tstest": "^1.0.73",
"@pushrocks/qenv": "^5.0.2",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.7.8",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartrequest": "^1.1.56"
"@pushrocks/smartrequest": "^2.0.10",
"@pushrocks/smarturl": "^3.0.5"
},
"browserslist": [
"last 1 chrome versions"

View File

@ -30,6 +30,28 @@ tap.test('should create a valid request', async (toolsArg) => {
console.log(await tinkuser.getProviderConsents());
});
tap.test('allow tink link to be used', async (toolsArg) => {
await toolsArg.delayFor(60000);
});
tap.test('get provider consents', async () => {
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser('user_1234_abc');
const providerConsents = await tinkuser.getProviderConsents();
console.log(providerConsents);
});
tap.test('get bankaccounts', async (toolsArg) => {
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser('user_1234_abc');
const bankAccounts = await tinkuser.getAllBankAccounts();
console.log(bankAccounts);
for (const bankAccount of bankAccounts) {
const transactions = await bankAccount.getTransactions();
console.log(transactions);
await toolsArg.delayFor(10000);
}
});
tap.test('should delete existing users', async () => {
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
const tinkUser = new tink.TinkUser(tinkTestAccount, null, 'user_1234_abc');

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@mojoio/tink',
version: '3.0.0',
version: '3.1.0',
description: 'an unofficial api abstraction for tink.com'
}

View File

@ -0,0 +1,89 @@
import { BankTransaction } from './tink.classes.banktransaction.js';
import { TinkUser } from './tink.classes.tinkuser.js';
import * as plugins from './tink.plugins.js';
export interface IBankAccountData {
"balances": {
"booked": {
"amount": {
"currencyCode": string,
"value": {
"scale": string,
"unscaledValue": string
}
}
}
},
"customerSegment": string,
"dates": {
"lastRefreshed": string
},
"financialInstitutionId": string,
"id": string,
"identifiers": {
"iban": {
"bban": string,
"iban": string,
},
"pan": {
"masked": string
}
},
"name": string,
"type": string,
}
export class BankAccount {
// STATIC
public static async getAccountUserAccessToken(tinkUserArg: TinkUser) {
const authorizationCode = await tinkUserArg.tinkAccountRef.getUserAuthorizationCode(
tinkUserArg.externalUserIdArg,
tinkUserArg.tinkAccountRef.clientId,
'accounts:read,balances:read,transactions:read,provider-consents:read'
);
const accessToken = await tinkUserArg.tinkAccountRef.getUserAccessToken(authorizationCode);
return accessToken;
}
public static async getBankAccountsForUser(tinkUserArg: TinkUser) {
const userAccessToken = await this.getAccountUserAccessToken(tinkUserArg);
const returnBankAccounts: BankAccount[] = []
const getBankAccountRecursively = async (nextPageToken?: string) => {
const searchParams = new URLSearchParams();
searchParams.set('pageSize', '200');
if (nextPageToken) {
searchParams.set('pageToken', nextPageToken)
}
const response = await tinkUserArg.tinkAccountRef.request({
urlArg: `/data/v2/accounts?${searchParams.toString()}`,
accessToken: userAccessToken,
methodArg: 'GET',
payloadArg: null,
});
for (const account of response.accounts) {
returnBankAccounts.push(new BankAccount(tinkUserArg, account));
}
if (response.nextPageToken.length > 0) {
await getBankAccountRecursively(response.nextPageToken);
}
}
await getBankAccountRecursively();
return returnBankAccounts;
};
// INSTANCE
tinkUserRef: TinkUser;
data: IBankAccountData;
constructor(tinkUserRefArg: TinkUser, dataArg: IBankAccountData) {
this.tinkUserRef = tinkUserRefArg;
this.data = dataArg;
}
/**
* gets the transactions for the bank account
*/
public async getTransactions() {
const transactions = await BankTransaction.getBankTransactions(this);
return transactions;
}
}

View File

@ -0,0 +1,96 @@
import { BankAccount } from './tink.classes.bankaccount.js';
import * as plugins from './tink.plugins.js';
export interface IBankTransactiondata {
accountId:string;
amount: {
currencyCode: string;
value: {
scale: string;
unscaledValue: string;
};
};
categories: {
pfm: {
id: string;
name: string;
};
};
dates: {
booked: string;
value: string;
};
descriptions: {
display: string;
original: string;
};
id: string;
identifiers: {
providerTransactionId: string;
};
merchantInformation: {
merchantCategoryCode: 'string';
merchantName: 'string';
};
providerMutability: string;
reference: string;
status: string;
types: {
financialInstitutionTypeCode: string;
type: string;
};
}
export class BankTransaction {
// STATIC
public static async getTransactionAccessToken(bankAccountArg: BankAccount) {
const authorizationCode =
await bankAccountArg.tinkUserRef.tinkAccountRef.getUserAuthorizationCode(
bankAccountArg.tinkUserRef.externalUserIdArg,
bankAccountArg.tinkUserRef.tinkAccountRef.clientId,
'accounts:read,balances:read,transactions:read,provider-consents:read'
);
const accessToken = await bankAccountArg.tinkUserRef.tinkAccountRef.getUserAccessToken(
authorizationCode
);
return accessToken;
}
public static async getBankTransactions(bankAccountArg: BankAccount) {
const accessToken = await this.getTransactionAccessToken(bankAccountArg);
const pageSize = 100;
const returnTransactions: BankTransaction[] = [];
const getTransactionsRecursively = async (nextPageToken?: string) => {
const searchParams = new URLSearchParams();
searchParams.set('accountIdIn', bankAccountArg.data.id);
searchParams.set('pageSize', '200');
if (nextPageToken) {
searchParams.set('pageToken', nextPageToken)
}
const response = await bankAccountArg.tinkUserRef.tinkAccountRef.request({
urlArg: `/data/v2/transactions?${searchParams.toString()}`,
accessToken: accessToken,
methodArg: 'GET',
payloadArg: null,
});
for (const transaction of response.transactions) {
returnTransactions.push(new BankTransaction(bankAccountArg, transaction));
}
if (response.nextPageToken.length > 0) {
await getTransactionsRecursively(response.nextPageToken);
}
};
await getTransactionsRecursively();
return returnTransactions;
}
// INSTANCE
bankAccountRef: BankAccount;
data: IBankTransactiondata;
constructor(bankAccountRefArg: BankAccount, dataArg: IBankTransactiondata) {
this.bankAccountRef = bankAccountRefArg;
this.data = dataArg;
}
}

View File

@ -1,30 +1,87 @@
import * as plugins from './tink.plugins.js';
export interface IProviderData {
credentialsId: string;
providerName: string;
status: string;
sessionExpiryDate: number;
accountIds: string[];
statusUpdated: number;
}
import { TinkUser } from './tink.classes.tinkuser.js';
/**
* a provider consent maps to tinks bank consents
*/
export class TinkProviderConsent {
// STATIC
public static async getProviderConsentsForUser(tinkUserRefArg: TinkUser) {
const returnProviderConsents: TinkProviderConsent[] = [];
public static async getProviderUserAccessToken(tinkUserRefArg: TinkUser) {
const authorizationCode = await tinkUserRefArg.tinkAccountRef.getUserAuthorizationCode(
tinkUserRefArg.externalUserIdArg,
tinkUserRefArg.tinkAccountRef.clientId,
'accounts:read,balances:read,transactions:read,provider-consents:read'
);
const accessToken = await tinkUserRefArg.tinkAccountRef.getUserAccessToken(authorizationCode);
return accessToken;
}
// STATIC
public static async getProviderConsentsForUser(tinkUserRefArg: TinkUser) {
const returnProviderConsents: TinkProviderConsent[] = [];
const accessToken = await this.getProviderUserAccessToken(tinkUserRefArg);
const responseData = await tinkUserRefArg.tinkAccountRef.request({
urlArg: '/api/v1/provider-consents',
accessToken,
methodArg: 'GET',
payloadArg: null,
});
console.log(responseData);
// console.log(responseData); // no nextPageToken here?
if (responseData.providerConsents) {
for (const providerConsentData of responseData.providerConsents) {
returnProviderConsents.push(new TinkProviderConsent(tinkUserRefArg, providerConsentData));
}
}
return returnProviderConsents;
}
// INSTANCE
constructor(tinkUserRefArg: TinkUser) {}
tinkUserRef: TinkUser;
data: IProviderData;
constructor(tinkUserRefArg: TinkUser, dataArg: IProviderData) {
this.tinkUserRef;
this.data = dataArg;
}
/**
* refresh the bank account data from origin
*/
public async refresh() {
const userAccessToken = await TinkProviderConsent.getProviderUserAccessToken(this.tinkUserRef);
const response = await this.tinkUserRef.tinkAccountRef.request({
accessToken: userAccessToken,
methodArg: 'POST',
urlArg: `https://api.tink.com/api/v1/credentials/${this.data.credentialsId}/refresh`,
payloadArg: null
});
// polling as per documentation for status 'UPDATED';
let pollingRounds = 0;
let status = null;
while(status !== 'UPDATED' && pollingRounds < 10) {
await plugins.smartdelay.delayFor(2000);
await this.update();
status = this.data.status;
pollingRounds++;
}
// think about how to handle errors here
}
public async update() {
const providerConsents = await TinkProviderConsent.getProviderConsentsForUser(this.tinkUserRef);
for (const providerConsent of providerConsents) {
if (providerConsent.data.credentialsId === this.data.credentialsId) {
this.data = providerConsent.data;
}
}
}
}

View File

@ -2,6 +2,7 @@ import * as plugins from './tink.plugins.js';
import { TinkAccount } from './tink.classes.tinkaccount.js';
import { TinkProviderConsent } from './tink.classes.tinkproviderconsent.js';
import { BankAccount } from './tink.classes.bankaccount.js';
export class TinkUser {
// STATIC
@ -112,10 +113,15 @@ export class TinkUser {
public async getTinkLinkForMarket(linkOptionsArg: {
countryId: string;
redirectUrl: string;
/**
* an optional state that is transported through to the callback
*/
customState: string;
testProviderBool?: boolean;
} = {
countryId: 'DE',
redirectUrl: 'https://console.tink.com/callback',
customState: "exampleState",
testProviderBool: true
}): Promise<string> {
if (typeof linkOptionsArg.testProviderBool !== 'boolean') {
@ -126,7 +132,21 @@ export class TinkUser {
'df05e4b379934cd09963197cc855bfe9', // this is a hardcoded app id for tink link, as recommended by tink.com
'authorization:read,authorization:grant,credentials:refresh,credentials:read,credentials:write,providers:read,user:read'
);
const tinkLinkUrl = `https://link.tink.com/1.0/business-transactions/connect-accounts?client_id=${'teststate'}&redirect_uri=${linkOptionsArg.redirectUrl}&authorization_code=${authorizationCode}&market=${linkOptionsArg.countryId}&test=${linkOptionsArg.testProviderBool}`;
const tinkUrlOptions: {[key: string]: string} = {}
tinkUrlOptions['client_id'] = this.tinkAccountRef.clientId;
tinkUrlOptions['redirect_uri']= linkOptionsArg.redirectUrl;
tinkUrlOptions['authorization_code'] = authorizationCode;
tinkUrlOptions['market'] = linkOptionsArg.countryId;
if (linkOptionsArg.testProviderBool) {
tinkUrlOptions['test'] = 'true';
}
if (linkOptionsArg.customState) {
tinkUrlOptions['state'] = linkOptionsArg.customState;
}
const url = plugins.smarturl.Smarturl.createFromUrl('https://link.tink.com/1.0/business-transactions/connect-accounts', {
searchParams: tinkUrlOptions
});
const tinkLinkUrl = url.toString();
return tinkLinkUrl;
}
@ -135,4 +155,12 @@ export class TinkUser {
await TinkProviderConsent.getProviderConsentsForUser(this);
return providerConsents;
}
/**
* gets all accounts
*/
public async getAllBankAccounts() {
const bankAccounts = await BankAccount.getBankAccountsForUser(this);
return bankAccounts;
}
}

View File

@ -2,5 +2,6 @@
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smarturl from '@pushrocks/smarturl';
export { smartdelay, smartrequest, smartpromise };
export { smartdelay, smartrequest, smartpromise, smarturl };

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}