feat(BankAccount and BankTransaction): now supporting the full retrieval process
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user