-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (115 loc) · 3.8 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import datetime
from flask import Flask
from flask_restful import reqparse, Api, Resource
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask_cors import CORS
app = Flask(__name__)
api = Api(app)
CORS(app, resources={r"/transactions/*": {"origins": "*"}})
client = MongoClient("localhost", 27017)
db = client.bank
ts = db.transactions
new_ts = db.newTransactions
parser = reqparse.RequestParser()
parser.add_argument("startDate", required=True, type=str, location="json")
parser.add_argument("endDate", required=True, type=str, location="json")
parser.add_argument("mode", choices=("count", "amount"), required=True, type=str)
parser.add_argument(
"currency", choices=("rial", "toman"), location="json", required=True, type=str
)
parser.add_argument(
"period",
choices=("day", "week", "month", "year"),
required=True,
type=str,
location="json",
)
def datetime_parser(date_str: str) -> datetime.datetime:
return datetime.datetime.strptime(date_str, "%Y-%m-%d")
def format_fixer(period: str) -> dict:
if period == "day":
return "%Y-%m-%d"
elif period == "month":
return "%Y-%m"
elif period == "year":
return "%Y"
elif period == "week":
return "%Y-%U"
def project_generator(args: dict) -> dict:
res = {"_id": 0, "key": "$_id"}
if args["mode"] == "amount":
if args["currency"] == "toman":
res["value"] = {"$divide": ["$amount", 10]}
else:
res["value"] = "$amount"
else:
res["value"] = "$count"
return {"$project": res}
class Transactions(Resource):
def post(self, user_id):
args = parser.parse_args()
start_date = datetime_parser(args["startDate"])
end_date = datetime_parser(args["endDate"])
res = ts.aggregate(
[
{
"$match": {
"merchantId": ObjectId(user_id),
"createdAt": {
"$lt": end_date,
"$gt": start_date,
},
}
},
{
"$group": {
"_id": {
"$dateToString": {
"date": "$createdAt",
"format": format_fixer(args["period"]),
}
},
"amount": {"$sum": "$amount"},
"count": {"$sum": 1},
}
},
project_generator(args),
]
)
return list(res)
class AllTransactions(Resource):
def post(self):
args = parser.parse_args()
start_date = datetime_parser(args["startDate"])
end_date = datetime_parser(args["endDate"])
res = ts.aggregate(
[
{
"$match": {
"createdAt": {
"$lt": end_date,
"$gt": start_date,
},
}
},
{
"$group": {
"_id": {
"$dateToString": {
"date": "$createdAt",
"format": format_fixer(args["period"]),
}
},
"amount": {"$sum": "$amount"},
"count": {"$sum": 1},
}
},
project_generator(args),
]
)
return list(res)
api.add_resource(AllTransactions, "/transactions")
api.add_resource(Transactions, "/transactions/<user_id>")
if __name__ == "__main__":
app.run(debug=True)