# Accounts Client

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

This client helps in managing users' bank accounts.

## Setup

```javascript
import { AccountsClient } 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 accountsClient = new AccountsClient('XXXX-XXXX-XXXX', SERVER_URL);
```

## How to use

### List Accounts

Fetches a list of accounts per user, sorted by ID in descending order.

```javascript
accountsClient
  .list(userId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

The ID of the user that owns the accounts is required.

Output:

```console
[
  Account {
    id: 320023867,
    providerId: '296ac8fc-1f79-456f-b051-1ad1770dfd5d',
    nature: 'Credit card',
    name: 'Dinners Grafite',
    number: 'Dinners Grafite',
    balance: 0,
    chargeable: false,
    dateCreated: 1673471719653,
    lastUpdated: 1673471822866,
    isBankAggregation: false,
    financialEntityId: 547953460
  },
  Account {
    id: 278020899,
    providerId: '291e5a29-49ed-401f-a583-193caa7acddd',
    nature: 'Debit',
    name: '11188222-4',
    number: '111882224',
    balance: 41233.07,
    chargeable: false,
    dateCreated: 1672938088048,
    lastUpdated: 1673487691911,
    isBankAggregation: false,
    financialEntityId: 547953460
  }
  ...
]

```

### Get Account

Given a valid account ID, fetches the information of an account.

```javascript
accountsClient
  .get(accountId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Account {
  id: 320023867,
  providerId: '296ac8fc-1f79-456f-b051-1ad1770dfd5d',
  nature: 'Credit card',
  name: 'Dinners Grafite',
  number: 'Dinners Grafite',
  balance: 0,
  chargeable: false,
  dateCreated: 1673471719653,
  lastUpdated: 1673471822866,
  isBankAggregation: false,
  financialEntityId: 547953460
},

```

### Create Account

Creates an account. A previosuly created user and a financial entity is required. You have to import the Account Payload Model to create a new one.

```javascript
import { AccountPayload } from "open-banking-pfm-sdk";

...

const newAccountData = new AccountPayload(
        {
          userId: 1115164, //The ID of the user that owns the account. It's required.
          financialEntityId: 743443, //The ID of the financial entity of the account. It's required.
          nature: 'Debit', //The nature of the account. It's required.
          name: 'Premium',  //The name of the account. It's required.
          number: '1111 1111 1111 1111', //The number of the account. It's required.
          balance: 1000, //The balance of the account. It's required.
          chargeable: false //A flag that indicates if the balance of an account will change with transactions changes. It's required.
        }
      );

accountsClient.create(newAccountData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Account {
  id: 2230303,
  providerId: '296ac8fc-1f79-456f-b051-1ad1770dfd5d',
  nature: 'Debit',
  name: "Premium",
  number: "1111 1111 1111 1111",
  balance: 1000,
  chargeable: false,
  dateCreated: 1673471719653,
  lastUpdated: 1673471822866,
  isBankAggregation: false,
  financialEntityId: 547953460
}

```

### Update Account

Updates an account. You can pass an object with the properties to update ( `nature`: _string_, `name`: _string_, `number`: _string_, `balance`: _number_, `chargeable`: _boolean_ ).

```javascript
const modifiedAccountData = { name: 'Gold' };

accountsClient
  .update(accountId, modifiedAccountData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Account {
  id: 2230303,
  providerId: '296ac8fc-1f79-456f-b051-1ad1770dfd5d',
  nature: 'Debit',
  name: "Gold",
  number: "1111 1111 1111 1111",
  balance: 1000,
  chargeable: false,
  dateCreated: 1673471719653,
  lastUpdated: 1673471822866,
  isBankAggregation: false,
  financialEntityId: 547953460
}

```

### Delete Account

Deletes an account and all its information, including transactions.

```javascript
accountsClient
  .delete(accountId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```
