When a users spends points, they don't know or care which payer the points come from. There are two rules for determining what points to "spend" first:
- Oldest points to be spent first (oldest based on transaction timestamp, not the order they’re received)
- 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 of
{ "payer": <string>, "points": <integer> }
for each call. - Return all payer point balances.
- Go to the project folder
cd PayersPointsApi
- Create a virtual environment
python -m venv venv
- Activate the created virtual environment:
-
venv\Scripts\activate.bat
- for Windows; -
source venv/bin/activate
- for Linux & MacOS.
-
- Install dependencies
pip install -r requirements.txt
- Go to the folder next to manage.py*
cd DjangoApp
- Start a local server Django
python manage.py runserver
- Go to the project folder
cd PayersPointsApi
- Build an image based on Dockerfile
docker build . -t transactions_api
- Launch container
docker run -p -t 8000:8000 transactions_api
- /api/transactions/
- /api/balance/
- /api/spend-points/
Initial transactions:
{ "payer": "DANNON", "points": 1000, "timestamp": "2020-11-02T14:00:00Z" }
{ "payer": "UNILEVER", "points": 200, "timestamp": "2020-10-31T11:00:00Z" }
{ "payer": "DANNON", "points": -200, "timestamp": "2020-10-31T15:00:00Z" }
{ "payer": "MILLER COORS", "points": 10000, "timestamp": "2020-11-01T14:00:00Z" }
{ "payer": "DANNON", "points": 300, "timestamp": "2020-10-31T10:00:00Z" }
Then you call your spend points route with the following request:
{ "points": 5000 }
The expected response from the spend call would be:
[
{ "payer": "DANNON", "points": -100 },
{ "payer": "UNILEVER", "points": -200 },
{ "payer": "MILLER COORS", "points": -4,700 }
]
A subsequent call to the points balance route, after the spend, should returns the following results:
{
"DANNON": 1000,
"UNILEVER": 0,
"MILLER COORS": 5300
}
P.s. there are ready-made tests in the repository, and if you open the project locally, there will already be a database with transactions from the example