This project is a backend solution for managing points across different payers and transactions. It allows for adding points, spending points, and retrieving the current balance of points per payer. Below are the instructions for setting up and running the server, as well as executing tests for the API.
To run the project, make sure you have the following installed:
- Node.js: You can download it from here.
- Nodemon: Nodemon is a tool that automatically restarts your server when code changes. Install it globally by running
npm install -g nodemon. - Axios: The test file uses Axios for making HTTP requests. If not already installed, run
npm install axios.
-
Clone the repository:
git clone <repository-url> cd <project-directory>
-
Install dependencies: Run the following command to install the necessary Node.js packages for the project:
npm install
To start the server, follow these steps:
-
Open a terminal window, navigate to the project directory, and run the following command:
nodemon server.js
-
The server will start on port 8000 by default. You should see a message in the terminal like:
Server started on http://localhost:8000
There is a test.js file included to simulate requests to the API. This includes adding transactions, spending points, and checking balances.
-
Open another terminal tab (while the server is running in the first tab) and run:
node test.js
-
The test file will send requests to the API and print responses for actions like adding transactions, spending points, and checking balances.
If you'd like to add more test cases in the test.js file, you can follow the format below:
// Add a transaction for DANNON
response = await axios.post('http://localhost:8000/add', {
payer: 'DANNON',
points: 300,
timestamp: '2022-10-31T10:00:00Z'
});
console.log('Add transaction response:', response.data);// Spend 5000 points
const spendResponse = await axios.post('http://localhost:8000/spend', { points: 5000 });
console.log('Spend response:', spendResponse.data);// Check the current balance
const balanceResponse = await axios.get('http://localhost:8000/balance');
console.log('Balance response:', balanceResponse.data);- Method:
POST - Description: Adds points for a specified payer.
- Request Body:
{ "payer": "DANNON", "points": 300, "timestamp": "2022-10-31T10:00:00Z" } - Response: Status code
200on success.
- Method:
POST - Description: Spends points in a first-in, first-out order by timestamp. The request specifies the number of points to be spent, and the API decides which payers to deduct points from.
- Request Body:
{ "points": 5000 } - Response: An array of payers and the points spent, e.g.:
[ { "payer": "DANNON", "points": -300 }, { "payer": "UNILEVER", "points": -200 }, { "MILLER COORS", "points": -4700 } ]
- Method:
GET - Description: Retrieves the current balance of points per payer.
- Response:
{ "DANNON": 1000, "UNILEVER": 0, "MILLER COORS": 5300 }