# Transactions Client

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

This client helps in the management of financial movements within an account.

## Setup

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

## How to use

### List Transactions

Fetches a list of transactions per account, sorted by ID in descending order

```javascript
transactionsClient.getList(accountId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
[
  Transaction {
    id: 123,
    date: 1587567125458,
    charge: true,
    description: "UBER EATS",
    amount: 1234.56,
    categoryId: 123,
    dateCreated: 1587567125458,
    lastUpdated: 1587567125458,
    isBankAggregation: true
  },
  Transaction {
    id: 456,
    date: 1587567145458,
    charge: true,
    description: "RAPPI",
    amount: 1234.56,
    categoryId: 123,
    dateCreated: 1646259197099,
    lastUpdated: 1646259197099,
    isBankAggregation: true
  }
  ...
]

```

### Get Transaction

Given a valid transaction ID, fetches the information of a transaction.

```javascript
transactionsClient.get(transactionId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Transaction {
  id: 123,
  date: 1587567125458,
  charge: true,
  description: "UBER EATS",
  amount: 1234.56,
  categoryId: 123,
  dateCreated: 1587567125458,
  lastUpdated: 1587567125458,
  isBankAggregation: true
}

```

### Create Transaction

Creates a transaction. A previosuly created account is required. You have to import the Transaction Payload Model to create a new one.

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

...

const newTransactionData = new TransactionPayload(
        {
          accountId: 487870282, //The ID of the account that holds the transaction. It´s required.
          amount: 1111, //The amount of the transaction. It´s required.
          charge: true, //A flag that indicates if the transaction is a charge or a deposit. It´s required.
          date: 1678255200000, //The date of the transaction. It´s required.
          description: 'Transaction Test', //The description of the transaction. It´s required.
          categoryId: 16 //The category ID of the transaction
        }
      );

transactionsClient.create(newTransactionData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Transaction {
  id: 789,
  date: 1587567145458,
  charge: true,
  description: "Transaction Test",
  amount: 1111,
  categoryId: 16,
  dateCreated: 1587567145458,
  lastUpdated: 1587567145458,
  isBankAggregation: false
}

```

### Update Transaction

Given a valid transaction id updates a transaction. You can pass an object with the properties to update ( `amount`: _number_, `charge`: _boolean_, `date`: _number_, `description`: _string_, `categoryId`: _number_ ).


```javascript

const modifiedTransactionData = { description: 'Edited Transaction Test' };

transactionsClient.edit(transactionId, modifiedTransactionData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Transaction {
  id: 789,
  date: 1587567145458,
  charge: true,
  description: "Edited Transaction Test",
  amount: 1111,
  categoryId: 16,
  dateCreated: 1587567145458,
  lastUpdated: 1587567145458,
  isBankAggregation: false
}
```

### Delete Transaction

Deletes a transaction and all its information.

```javascript
transactionsClient.delete(transactionId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

### Delete all transactions from an account

Deletes all transactions of the account by its identifier.

```javascript
transactionsClient.deleteAll(accountId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```
