tink/ts/tink.classes.banktransaction.ts

118 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

import { BankAccount } from './tink.classes.bankaccount.js';
import * as plugins from './tink.plugins.js';
2022-10-29 15:16:44 +00:00
import * as tinkHelpers from './helpers/index.js';
export interface ITinkBankTransactiondata {
id: string;
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;
};
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;
2022-10-29 15:16:44 +00:00
data: ITinkBankTransactiondata;
2022-10-29 15:16:44 +00:00
constructor(bankAccountRefArg: BankAccount, dataArg: ITinkBankTransactiondata) {
this.bankAccountRef = bankAccountRefArg;
this.data = dataArg;
}
2022-10-29 15:16:44 +00:00
/**
* gets normalized data
*/
public getNormalizedData() {
return {
id: this.data.id,
2022-10-30 16:27:13 +00:00
date: new Date(this.data.dates.booked).getTime(),
2022-10-29 15:16:44 +00:00
amount: tinkHelpers.getNormalizedAmount(this.data.amount),
name: this.data.descriptions.display,
2022-10-29 15:46:17 +00:00
description: this.data.descriptions.original,
2022-10-30 16:27:13 +00:00
originAccountId: this.data.accountId,
2022-10-29 15:46:17 +00:00
justForLooks: {
2022-10-30 16:40:16 +00:00
originalScaledAmount: this.data.amount,
dateIso: new Date(this.data.dates.booked).toISOString()
2022-10-29 15:46:17 +00:00
}
2022-10-29 15:16:44 +00:00
}
}
}