Simple Flask app that accepts HTTP requests and returns responses based on conditions outlined in the section below.
- Every user has an
accounts
that storespoints
perpayers
. - Payer submit transactions.
- Each transaction should include
payer
,points
,timestamp
information. - Transactions are not necessarily added in the order of their transaction timestamps.
- A transaction will be added to
earnings
list (orwithdraws
list) if positive points (or negative points). - Transactions are stored in memory on the back end.
- A payer's total points can go below 0 in the middle of adding transactions, but it won't be below 0 after adding all transactions.
- Each transaction should include
- The user can
spend
points.- The user's total points can't go below 0.
- When spending points, the oldest points are spent first based on their transaction's timestamp, regardless of payer.
-
Clone repo locally
git clone https://github.com/xuxiaqing2011/Reward-Points
-
Go to the project's root directory
cd /my/path/to/Reward-Points
-
Install packages
pip install -r requirements.txt
-
Start the server
python app.py
-
Verify the app is running by visiting http://localhost:5000/. You should see the following greeting:
"Home Page"
NOTE Because this web service doesn't use any durable data store, there will be no data in the backend whenever the sever is started, which means:
- The user will initially have a
balance
of zero. accounts
,earnings
,withdraws
will be empty.
From any curl tool such as Postman or a basic shell curl command, make requests to http://localhost:5000/ENDPOINT where ENDPOINT is one of the routes described below. For POST requests, such as adding a transaction or spending points, use JSON schema.
Currently only the following routes are implemented:
-
Show Points:
/check_balance
GET http://localhost:5000/check_balance
This route gives the user
accounts
information, which revealspoints
perpayer
. -
Add points:
/add_transactions
POST http://localhost:5000/add_transactions Example JSON: { "payer": "DANNON", "points": 1000, "timestamp": "2020-11-02T14:00:00Z" }
NOTE: Transactions with positive points are added to
earnings
and inserted at the right place based on timestamp (later -> early). Transactions with negative points are added towithdraws
, and these transactions are used to negate the positive points before spending. -
Spend points:
/spend_points
POST http://localhost:5000/spend_points Example JSON: { "points": 5000 }
NOTE: Spending uses positive points from the earliest transaction in
earnings
list. But before that, we will first check if the same payer appears in thewithdraws
list (meaning payer withdraws points). If they do, full/part of the positive points are used to negate the nagetive points. Any remaining positive points will be used for spending.
Run tests in test_app.py
from the project's main directory:
pytest
Tests check that the app should:
GET
all points by payerPOST
a new transactionPOST
spend points available- NOT
POST
spend a negative/unavailable amount of points