# Banks Client

![Language](https://img.shields.io/badge/Language-JavaScript-yellow.svg)

This client contains the available actions related to banks.

## Setup

```javascript
import { BanksClient } from 'open-banking-pfm-sdk';

//The constructor receives an api key to validate access to all of its functions. This parameter is required. If you want to change the API Server Url you can do it passing it as second parameter.
const SERVER_URL =
  'http://tecbantest@ec2-3-21-18-54.us-east-2.compute.amazonaws.com:8081/api/v1/';
const banksClient = new BanksClient('XXXX-XXXX-XXXX', SERVER_URL);
```

## How to use

### Available Banks

Returns the list of available banks to be able to add and manage movements, money and balance with the bank.

```javascript
banksClient
  .getAvailables()
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
[
  Bank {
    bankId: 1115160,
    name: "Bank 1",
    imagePath: "https://cdn.finerio.mx/pfm/financial-entities/bank_BNW_shield.png",
    financialEntityId: 497394395
  },
  Bank {
    bankId: 1115155,
    name: "Bank 2",
    imagePath: "https://cdn.finerio.mx/pfm/financial-entities/bank_BNW_shield.png",
    financialEntityId: 497394395
  },
  ...
]
```

### Get Bank aggregation status

Action to obtain bank aggregation status. The status can be 'completed', 'in process' or 'failed'.

```javascript
const bankId = 'c55a1362-93fd-49eb-86ae-d82205b861c5'; //The ID of the bank to request
const userId = 487969842; //The ID of the user to request.
banksClient
  .getAggregationStatus(bankId, userId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
{
  bankId: "c55a1362-93fd-49eb-86ae-d82205b861c5",
  userId: 487969842,
  status: "CONSENT_REQUESTED"
}
```

### Bank aggregation status with Long Polling

Starts a long polling to get bank aggregation status.

```javascript
const options = {
  bankId: 2230303, //The ID of the bank to request. Is required
  userId: 1111, //The ID of the user to request. Is required.
  onResponse: () => null, //Triggered when the response is received.
  onError: () => null //Triggered when a server error occurs
  time: 5000 //Interval between requests
};

banksClient
  .aggregationStatusSubscribe(options)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

To stop the long polling:

```javascript
banksClient.aggregationStatusUnsubscribe();
```

### Create Bank Consent

Given a bank ID and a user ID, create consent and retrieve the url for ask it.

```javascript
const bankId = 'c55a1362-93fd-49eb-86ae-d82205b861c5'; //The ID of the bank to request
const userId = 487969842; //The ID of the user to request.
const timer = 7889400000; //The expiration time of the user consent in milliseconds
banksClient
  .createConsent(bankId, userId, timer)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
{
  bankId: "c55a1362-93fd-49eb-86ae-d82205b861c5",
  userId: 487969842,
  url: "https://auth1.tboz-csbx.staging.ozoneapi-br.com/auth?client_id=75deaf3f-2597-48a0-bf70"
}
```

### Aggregate Banks

Retrieve a list of banks that have successfully completed the bank aggregation process.

```javascript
const userId = 10; //The ID of the user to request.

banksClient
  .getAggregates(userId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
[
  BankAggregated {
    consentId: "urn:consumption:aac-649a4ef6-d4b4-43a8-b32e-1aba5a0355fd",
    originBankName: null,
    customerIdentification: null,
    cpf: "76109277673",
    targetInstitution: "consumptionSandboxAuth1234",
    deadline: 0,
    expirationDay: 1686184974479,
    status: "CONSENT_REQUESTED",
    isSynchronized: null
  },
  ...
]
```

### Get Bank Resources

Consult the resources granted to the user for the given bank and start in the background the aggregation of the resources that the user is authorised to user.

```javascript
const bankId = 'c55a1362-93fd-49eb-86ae-d82205b861c5'; //The ID of the bank to request
const userId = 487969842; //The ID of the user to request.

banksClient
  .getResources(bankId, userId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
{
  bankId: "c55a1362-93fd-49eb-86ae-d82205b861c5",
  userId: 487969842,
  resources: [
    "ACCOUNT", "CREDIT_CARD_ACCOUNT"
  ]
}
```

### Synchronize

Start of the resources that the user is authorised for.

```javascript
const bankId = 'c55a1362-93fd-49eb-86ae-d82205b861c5'; //The ID of the bank to request
const userId = 487969842; //The ID of the user to request.

banksClient
  .synchronize(bankId, userId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
true
```
