2022-08-23 14:56:59 +00:00
|
|
|
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;
|
2022-08-23 14:56:59 +00:00
|
|
|
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-08-23 14:56:59 +00:00
|
|
|
|
2022-10-29 15:16:44 +00:00
|
|
|
constructor(bankAccountRefArg: BankAccount, dataArg: ITinkBankTransactiondata) {
|
2022-08-23 14:56:59 +00:00
|
|
|
this.bankAccountRef = bankAccountRefArg;
|
|
|
|
this.data = dataArg;
|
|
|
|
}
|
2022-10-29 15:16:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets normalized data
|
|
|
|
*/
|
|
|
|
public getNormalizedData() {
|
|
|
|
return {
|
|
|
|
id: this.data.id,
|
|
|
|
amount: tinkHelpers.getNormalizedAmount(this.data.amount),
|
|
|
|
name: this.data.descriptions.display,
|
|
|
|
description: this.data.descriptions.original
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-08-23 14:56:59 +00:00
|
|
|
}
|