https://github.com/rbakulin/mortgage_api
REST API for calculating mortgage parameters
https://github.com/rbakulin/mortgage_api
django django-rest-framework docker docker-compose logging mortgage postgresql python rest-api swagger
Last synced: 3 months ago
JSON representation
REST API for calculating mortgage parameters
- Host: GitHub
- URL: https://github.com/rbakulin/mortgage_api
- Owner: rbakulin
- License: mit
- Created: 2020-07-30T23:02:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-06T12:01:47.000Z (about 2 years ago)
- Last Synced: 2024-04-06T13:23:10.362Z (about 2 years ago)
- Topics: django, django-rest-framework, docker, docker-compose, logging, mortgage, postgresql, python, rest-api, swagger
- Language: Python
- Homepage:
- Size: 279 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
MORTGAGE-API 🏠
REST API that allows to calculate mortgage details, such as payment schedule and principal/interest breakdown.
Supports addition of extra payments - they will be dynamically incorporated into the payment schedule.
---
## Basic info
An annuity repayment scheme is used for calculations.
The formula for calculating a monthly payment:
```
PAYMENT = LOAN AMOUNT * (INTEREST RATE / (1 + INTEREST RATE) - NUMBER OF MONTHS - 1)
```
The final payment may be less than the standard monthly payment, so the amount of the final payment is equal to the
remaining balance after the previous payment was made.
## Run
You can run the app natively:
```shell
pip install poetry
poetry install
python manage.py migrate
python manage.py runserver
```
Or via docker-compose:
```shell
docker-compose run app python manage.py migrate
docker-compose up
```
## Quickstart
1. Create a user:
POST `127.0.0.1:8000/auth/register/`
```json
{
"username": "admin",
"password": "adminadmin",
"password2": "adminadmin",
"email": "admin@example.com"
}
```
2. Get a token:
POST `127.0.0.1:8000/auth/token/`
```json
{
"username": "admin",
"password": "adminadmin"
}
```
3. Create a mortgage:
POST `127.0.0.1:8000/api/v1/mortgage/`
headers: {Authorization: Bearer }
```json
{
"percent": "8.20",
"period": 25,
"first_payment_amount": 2500000,
"credit_amount": 11000000,
"issue_date": "2021-09-04"
}
```
4. Calculate mortgage schedule:
POST `127.0.0.1:8000/api/v1/mortgage//calc-payment-schedule/`
headers: {Authorization: Bearer }
5. Add extra payment:
POST `127.0.0.1:8000/api/v1/mortgage//add-extra-payment/`
headers: {Authorization: Bearer }
```json
{
"amount": 70000,
"date": "2021-10-04"
}
```
6. Check calculated payments:
GET `127.0.0.1:8000/api/v1/mortgage//payment/?page_size=100&page=1`
headers: {Authorization: Bearer }
## API structure
Use Swagger to see all endpoints description: [127.0.0.1:8000/swagger/](http://127.0.0.1:8000/swagger/)
## ⚠️ Usage essentials
* All `mortgage/` endpoints are available only for registered users.
* User can only see mortgages that were created by himself.
* You can't CRUD payments directly via API - use `calc-payment-schedule/` endpoint instead.
* Be aware that reusing the `calc-payment-schedule/` endpoint will remove any extra payment that you've already added.
* After updating a mortgage (PUT, PATCH), payment schedule will be recalculated automatically. Also, all extra payments for this mortgage will be removed.
* There are a few rules for adding mortgage (POST) `mortgage/`:
- Mortgage's period should be between 1 and 30 years.
* There are a few rules for adding extra payments `add-extra-payment/`:
- Extra payment's date should be bigger than first payment's date and lower than last payment's date.
- Extra payment's amount should be less than previous payment's debt rest.
* Access token expires in 1 hour, refresh token - in 24 hours (both are 24 hours for dev env). Use `token/refresh/` to refresh the token.
## Logging
The app writes logs to a console and to a file named `mortgage_api.log` in (if it's not `prod` env) the app's base directory.
This file contains events in json representation which is easy to be parsed.
You can go to the `settings.py` and remove `file` handler in `LOGGING` section if it feels redundant.
Besides default Django events all payment calculations are logged.
## Pre-commit hooks
Make sure you did install requirements (`poetry install`) and then just set up the git hook scripts via `pre-commit`:
```shell
pre-commit install
```