-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (55 loc) · 2.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
from flask import Flask, request, jsonify
from flask_cors import CORS
from finance_manager import FinanceManager
from util import *
app = Flask(__name__)
CORS(app)
# Configure logging
logging.basicConfig(level=logging.INFO)
# Assuming FinanceManager initialization doesn't require parameters,
# or you have predefined parameters to pass.
finance_manager = FinanceManager(config_path="config/config.json",
db_uri="mongodb://localhost:27017/",
db_name="finance_app_db")
@app.route('/add_stream', methods=['POST'])
def add_stream():
data = request.json
logging.info(f'Received request to add stream: {data}')
user_id = data.get('user_id')
stream_type = data.get('stream_type')
# Common fields for both income and expense streams
common_fields = {
'start_date_str': format_date(data.get('start_date_str'), default=None),
'end_date_str': format_date(data.get('end_date_str'), default=None),
}
try:
if stream_type == 'income':
kwargs = {
**common_fields,
'annual_income': to_int(data.get('annual_income')),
'personal_allowance': to_int(data.get('personal_allowance'), 12570),
'bonus': to_int(data.get('bonus')),
'pension_percentage': to_int(data.get('pension_percentage')),
'plan_type': data.get('plan_type', 'Plan 1'),
'is_scottish': data.get('is_scottish', False),
'is_married': data.get('is_married', False),
'is_blind': data.get('is_blind', False),
}
elif stream_type == 'expense':
kwargs = {
**common_fields,
'principal': to_int(data.get('principal')),
'annual_interest_rate': to_float(data.get('annual_interest_rate')),
'years': to_int(data.get('years')),
}
else:
return jsonify({"error": "Invalid stream type provided."}), 400
stream_id = finance_manager.add_stream(user_id=user_id, stream_type=stream_type, **kwargs)
logging.info(f'Stream added successfully: {stream_id}')
return jsonify({"stream_id": str(stream_id)}), 201
except Exception as e:
logging.error(f'Error adding stream: {str(e)}')
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True) # Consider removing debug=True in production