# Categories Client

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

This client contains the available actions related to the classification of transactions and budgets.

## Setup

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

## How to use

### List Categories

Fetches a list of categories, sorted by ID in descending order

```javascript
categoriesClient
  .getList(userId, cursor)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

The cursor param is optional. If a cursor is specified, the list starts with the item that has that ID. If a user ID is specified, both system and user categories are fetched. If a user ID is not specified, only system categories are fetched.

Output:

```console
[
  Category {
    id: 1,
    name: 'Hogar',
    color: '#A3CB38',
    imagePath: 'https://cdn.image.com',
    parentCategoryId: null,
    userId: null,
  },
  Category {
    id: 2,
    name: 'Alimentos',
    color: '#FECA46',
    imagePath: 'https://cdn.image.com',
    parentCategoryId: null,
    userId: null,
  },
  ...
]

```

### Get Category

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

```javascript
const categoryId = 2;
categoriesClient
  .get(categoryId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Category {
  id: 2,
  name: 'Alimentos',
  color: '#FECA46',
  imagePath: 'https://cdn.image.com',
  parentCategoryId: null,
  userId: null,
}

```

### Create Category

Creates a category. If a user ID is not specified, the category is considered as a system category. If a parent category ID is specified, the category is considered a subcategory. You have to import the Category Payload Model to update it.

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

...

const newCategoryData = new CategoryPayload(
        {
          userId: 858263023, //The ID of the user that owns the category
          name:  "Sports", //The name of the category. It's required.
          color: "f848ef", //The color of the category
          parentCategoryId: null, //The ID of the parent category that owns this subcategory
        }
      );

categoriesClient.create(newCategoryData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Category {
  id: 363479579,
  name: "Sports",
  color: "f848ef",
  imagePath: "https://cdn.finerio.mx/pfm/financial-entities/default.jpg",
  parentCategoryId: null,
  userId: 858263023,
  dateCreated: 1678317553755,
  lastUpdated: 1678317553755
}

```

### Update Category

Given a valid category id updates a category. You can pass an object with the properties to update ( `name`: _string_, `color`: _string_, `parentCategoryId`: [_number_, _null_] ).

```javascript
const modifiedCategoryData = { name: 'Ball Sports' };

categoriesClient
  .edit(categoryId, modifiedCategoryData)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

Output:

```console
Category {
  id: 363479579,
  name: "Ball Sports",
  color: "f848ef",
  imagePath: "https://cdn.finerio.mx/pfm/financial-entities/default.jpg",
  parentCategoryId: null,
  userId: 858263023,
  dateCreated: 1678317553755,
  lastUpdated: 1678317553755
}
```

### Delete Category

Given a valid category id deletes a category.

```javascript
categoriesClient
  .delete(categoryId)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));
```

If the category was removed, the response will be returned as true.
