Contains the backend logic for rewards calculations
Users can view their reward balances, spend points and system to record every transaction as per payer, points and timestamp
- We want the oldest points to be spent first (oldest based on transaction timestamp, not the order they’re received)
- We want no payer's points to go negative
- Add transactions for a specific payer and date.
- Spend points using the rules above and return a list for each call
- Return all payer point balances
Problem Statement - https://fetch-hiring.s3.us-east-1.amazonaws.com/points.pdf
No database to persist the data and hence each time you run the application user data would be null
Basic validation are performed by the application, added spring security can be added but is not present in this application
Since its a simple application, apis do not have any header or token for authorization
- Install any version above Java 8 but tried and tested in Java 8 and Java 17,
- Install any IDE of your choice VS Code, Intellij
- Postman to test APIs
- Make sure IDE supports Spring Application, if not add plugins for Spring Application
- If using VS Code, go to marketplace and install plugins for Spring
- Clone the project using GitHub to a local folder using
https://github.com/ajaymohandas89/FetchRewardsPointsSystems
- Simply run the PointsApplication.java
- Application will be running on http://localhost:8080
- Test the backend services using API testing tool
- Import API collection in Postman using Import and upload FetchRewards_UserPointsProblem.postman_collection.json or test using below API curl commands
- From the project run test cases under UserPointServiceTest.java
- Check Balance Of User
http://localhost:8080/userpoints/v1/balance/{userId}
curl --location --request GET 'http://localhost:8080/userpoints/v1/balance/1'
[
"UNILEVER:0",
"MILLER COORS:5300",
"DANNON:1000"
]
- Fetch All Users Transactions
http://localhost:8080/userpoints/v1/transactions
curl --location --request GET 'http://localhost:8080/userpoints/v1/transactions'
{
"1": [
{
"transactionId": "4657090f-94f4-4993-8c84-4e2cbf794bd1",
"payer": "DANNON",
"points": 1000,
"timestamp": "2022-12-04T22:07:36.9109449"
},
{
"transactionId": "3445f577-aa00-48ef-b9c3-1b65425bc285",
"payer": "DANNON",
"points": 0,
"timestamp": "2022-12-04T22:08:12.3789424"
},
{
"transactionId": "c258d4a1-085c-42f9-adc1-a83026e6b945",
"payer": "UNILEVER",
"points": 0,
"timestamp": "2022-12-04T22:08:12.3799424"
},
{
"transactionId": "3e1216eb-96ab-4f55-9416-a6e6aee629ef",
"payer": "MILLER COORS",
"points": 5300,
"timestamp": "2022-12-04T22:08:12.3809421"
}
]
}
- Fetch Each User Transactions
http://localhost:8080/userpoints/v1/transactions/{userId}
curl --location --request GET 'http://localhost:8080/userpoints/v1/transactions/1'
[
{
"transactionId": "4657090f-94f4-4993-8c84-4e2cbf794bd1",
"payer": "DANNON",
"points": 1000,
"timestamp": "2022-12-04T22:07:36.9109449"
},
{
"transactionId": "3445f577-aa00-48ef-b9c3-1b65425bc285",
"payer": "DANNON",
"points": 0,
"timestamp": "2022-12-04T22:08:12.3789424"
},
{
"transactionId": "c258d4a1-085c-42f9-adc1-a83026e6b945",
"payer": "UNILEVER",
"points": 0,
"timestamp": "2022-12-04T22:08:12.3799424"
},
{
"transactionId": "3e1216eb-96ab-4f55-9416-a6e6aee629ef",
"payer": "MILLER COORS",
"points": 5300,
"timestamp": "2022-12-04T22:08:12.3809421"
}
]
- Add Point For A User
http://localhost:8080/userpoints/v1/addPoints/{userId}
curl --location --request POST 'http://localhost:8080/userpoints/v1/addPoints/2' \
--header 'Content-Type: application/json' \
--data-raw '{
"payer": "C",
"point": 300
}'
{
"payer": "C",
"point": 300
}
{
"payer": "C",
"points": 300,
"timestamp": "2022-12-04T17:12:15.2119043"
}
- Spend Point For A User
http://localhost:8080/userpoints/v1/spend
curl --location --request PUT 'http://localhost:8080/userpoints/v1/spend' \
--header 'Content-Type: application/json' \
--data-raw '{
"userId": 2,
"points": 150
}'
{
"userId": 1,
"points": 5000
}
[
{
"payer": "DANNON",
"points": "-100"
},
{
"payer": "UNILEVER",
"points": "-200"
},
{
"payer": "MILLER COORS",
"points": "-4700"
}
]