tink/ts/tink.classes.bankaccount.ts

129 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

import { BankTransaction } from './tink.classes.banktransaction.js';
import { TinkUser } from './tink.classes.tinkuser.js';
import * as plugins from './tink.plugins.js';
2022-10-29 14:18:29 +00:00
import * as tinkHelpers from './helpers/index.js';
2022-10-29 15:16:44 +00:00
export interface ITinkBankAccountData {
2022-10-29 14:18:29 +00:00
balances: {
booked: {
amount: {
currencyCode: string;
value: {
scale: string;
unscaledValue: string;
};
};
};
available: {
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;
};
financialInstitution: { accountNumber: string; referenceNumbers: unknown };
};
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);
2022-10-29 14:18:29 +00:00
const returnBankAccounts: BankAccount[] = [];
const getBankAccountRecursively = async (nextPageToken?: string) => {
const searchParams = new URLSearchParams();
searchParams.set('pageSize', '200');
if (nextPageToken) {
2022-10-29 14:18:29 +00:00
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);
}
2022-10-29 14:18:29 +00:00
};
await getBankAccountRecursively();
return returnBankAccounts;
2022-10-29 14:18:29 +00:00
}
// INSTANCE
tinkUserRef: TinkUser;
2022-10-29 15:16:44 +00:00
data: ITinkBankAccountData;
constructor(tinkUserRefArg: TinkUser, dataArg: ITinkBankAccountData) {
this.tinkUserRef = tinkUserRefArg;
this.data = dataArg;
}
2022-10-29 14:18:29 +00:00
/**
* updates the account and tries to get the latest state from bunq
*/
public async update() {
const bankAccounts = await BankAccount.getBankAccountsForUser(this.tinkUserRef);
const matchingAccount = bankAccounts.find(
(bankAccountArg) => bankAccountArg.data.id === this.data.id
);
if (matchingAccount) {
this.data = matchingAccount.data;
}
}
/**
* gets normalized data
*/
public getNormalizedData() {
return {
2022-10-29 15:16:44 +00:00
id: this.data.id,
2022-10-29 14:18:29 +00:00
name: this.data.name,
2022-10-30 14:22:13 +00:00
accountNumber: this.data.identifiers?.financialInstitution?.accountNumber || null,
iban: this.data.identifiers.iban?.iban || null,
2022-10-29 14:18:29 +00:00
bookedValue: tinkHelpers.getNormalizedAmount(this.data.balances.booked?.amount),
availableValue: tinkHelpers.getNormalizedAmount(this.data.balances.available?.amount),
};
}
/**
* gets the transactions for the bank account
*/
public async getTransactions() {
const transactions = await BankTransaction.getBankTransactions(this);
return transactions;
}
}