open-banking-pfm-sdk
Version:
The Open Banking PFM SDK uses Client classes and with **Promises** to get responses from the Open Banking PFM API in an easier way and structured as data models.
179 lines (141 loc) • 3.98 kB
Markdown
# Budgets Client

This client has the actions that allow you to manage the representations of the user's budget plans.
## Setup
```javascript
import { BudgetsClient } 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 budgetsClient = new BudgetsClient('XXXX-XXXX-XXXX', SERVER_URL);
```
## How to use
### List Budgets
Fetches a list of budgets per user, sorted by ID in descending order.
```javascript
budgetsClient
.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.
Output:
```console
[
Budget {
id: 982908114,
categoryId: 11,
name: "Sports",
amount: 1111.00,
warningPercentage: 0.70,
spent: 0.00,
leftToSpend: 1111.00,
status: "ok",
dateCreated: 1677714920234,
lastUpdated: 1677714920234
},
Budget {
id: 982908115,
categoryId: 11,
name: "Travelling",
amount: 1111.00,
warningPercentage: 0.70,
spent: 0.00,
leftToSpend: 1111.00,
status: "ok",
dateCreated: 1677714920234,
lastUpdated: 1677714920234
}
...
]
```
### Get Budget
Given a valid budget ID, fetches the information of a budget.
```javascript
const budgetId = 982908114;
budgetsClient
.get(budgetId)
.then((data) => console.log(data))
.catch((error) => console.log(error));
```
Output:
```console
Budget {
id: 982908114,
categoryId: 11,
name: "Sports",
amount: 1111.00,
warningPercentage: 0.70,
spent: 0.00,
leftToSpend: 1111.00,
status: "ok",
dateCreated: 1677714920234,
lastUpdated: 1677714920234
}
```
### Create Budget
Creates a budget. A previously created user and a category is required. There can be only one budget per category.
```javascript
import { BudgetPayload } from "open-banking-pfm-sdk";
...
const newBudgetData = new BudgetPayload(
{
amount: 100, //The amount of the budget. It's required.
categoryId: 62, //The category ID of the budget. It's required.
name: "Donaciones", //The name of the budget. It's required.
userId: 1859616, //The ID of the user that owns the budget. It's required.
warningPercentage: 0.5 //The percentage where the budget status changes from stable to warning. This is optinal field. By default is 0.7.
}
);
budgetsClient.create(newBudgetData)
.then((data) => console.log(data))
.catch((error) => console.log(error));
```
Output:
```console
Budget {
id: 234924782,
categoryId: 62,
name: "Donaciones",
amount: 100.00,
warningPercentage: 0.70,
spent: 0.00,
leftToSpend: 100.00,
status: "ok",
dateCreated: 1678317204886,
lastUpdated: 1678317204886
}
```
### Update Budget
Update a budget. You can pass an object with the properties to update ( `amount`: _number_, `name`: _string_, `warningPercentage`: _number_ ).
```javascript
const modifiedBudgetData = { amount: 500 };
budgetsClient
.edit(budgetId, modifiedBudgetData)
.then((data) => console.log(data))
.catch((error) => console.log(error));
```
Output:
```console
Budget {
id: 234924782,
categoryId: 62,
name: "Donaciones",
amount: 500.00,
warningPercentage: 0.70,
spent: 0.00,
leftToSpend: 100.00,
status: "ok",
dateCreated: 1678317204886,
lastUpdated: 1678317204886
}
```
### Delete Budget
Deletes a budget and all its information.
```javascript
budgetsClient
.delete(budgetId)
.then((data) => console.log(data))
.catch((error) => console.log(error));
```
If the budget was removed, the response will be returned as true.