-
Notifications
You must be signed in to change notification settings - Fork 41
/
loan.py
229 lines (173 loc) · 7.51 KB
/
loan.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright (c) 2023 Cisco Systems, Inc. and its affiliates All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from concurrent import futures
import random
import datetime
import os
import grpc
import logging
from flask import Flask, request, jsonify
# set logging to debug
logging.basicConfig(level=logging.DEBUG)
from loan_pb2 import *
import loan_pb2_grpc
from pymongo.mongo_client import MongoClient
from dotenv import load_dotenv
load_dotenv()
# db_host = os.getenv("DATABASE_HOST", "localhost")
db_url = os.getenv("DB_URL")
if db_url is None:
raise Exception("DB_URL environment variable is not set")
uri = db_url
protocol = os.getenv('SERVICE_PROTOCOL', 'http')
if protocol is None:
raise Exception("SERVICE_PROTOCOL environment variable is not set")
protocol = protocol.lower()
logging.debug(f"microservice protocol: {protocol}")
client = MongoClient(uri)
db = client["bank"]
collection_accounts = db["accounts"]
collection_loans = db["loans"]
class LoanGeneric:
def ProcessLoanRequest(self, request_data):
name = request_data["name"]
email = request_data["email"]
account_type = request_data["account_type"]
account_number = request_data["account_number"]
govt_id_type = request_data["govt_id_type"]
govt_id_number = request_data["govt_id_number"]
loan_type = request_data["loan_type"]
loan_amount = float(request_data["loan_amount"])
interest_rate = float(request_data["interest_rate"])
time_period = request_data["time_period"]
user_account = self.__getAccount(account_number)
# count = collection_loans.count_documents({"email_id": email, 'account_number': account_number})
count = collection_accounts.count_documents({"email_id": email, 'account_number': account_number})
logging.debug(f"user account only based on account number search : {user_account}")
logging.debug(f"Count whther the email and account exist or not : {count}")
if count == 0:
return {"approved": False, "message": "Email or Account number not found."}
result = self.__approveLoan(user_account, loan_amount)
logging.debug(f"Result {result}")
message = "Loan Approved" if result else "Loan Rejected"
# insert loan request into db
loan_request = {
"name": name,
"email": email,
"account_type": account_type,
"account_number": account_number,
"govt_id_type": govt_id_type,
"govt_id_number": govt_id_number,
"loan_type": loan_type,
"loan_amount": loan_amount,
"interest_rate": interest_rate,
"time_period": time_period,
"status": "Declined",
"timestamp": datetime.datetime.now(),
}
loan_request["status"] = "Approved" if result else "Declined"
collection_loans.insert_one(loan_request)
response = {"approved": result, "message": message}
logging.debug(f"Account: {account_number}")
logging.debug(f"Response: {response}")
return response
def getLoanHistory(self, request_data):
email = request_data["email"]
loans = collection_loans.find({"email": email})
loan_history = []
for l in loans:
loan_history.append(
{
"name": l["name"],
"email": l["email"],
"account_type": l["account_type"],
"account_number": l["account_number"],
"govt_id_type": l["govt_id_type"],
"govt_id_number": l["govt_id_number"],
"loan_type": l["loan_type"],
"loan_amount": l["loan_amount"],
"interest_rate": l["interest_rate"],
"time_period": l["time_period"],
"status": l["status"],
"timestamp": f"{l['timestamp']}",
}
)
return loan_history
def __getAccount(self, account_num):
r = None
accounts = collection_accounts.find()
for acc in accounts:
if acc["account_number"] == account_num:
r = acc
break
# logging.debug(f"Account {r}")
return r
def __approveLoan(self, account, amount):
if amount < 1:
return False
account["balance"] += amount
collection_accounts.update_one(
{"account_number": account["account_number"]},
{"$set": {"balance": account["balance"]}},
)
return True
class LoanService(loan_pb2_grpc.LoanServiceServicer):
def __init__(self) -> None:
super().__init__()
# enable github copiolot
self.loan = LoanGeneric()
def ProcessLoanRequest(self, request, context):
name = request.name
email = request.email
account_type = request.account_type
account_number = request.account_number
govt_id_type = request.govt_id_type
govt_id_number = request.govt_id_number
loan_type = request.loan_type
loan_amount = float(request.loan_amount)
interest_rate = float(request.interest_rate)
time_period = request.time_period
req = {'name': name, 'email': email, 'account_type': account_type, 'account_number': account_number, 'govt_id_type': govt_id_type, 'govt_id_number': govt_id_number, 'loan_type': loan_type, 'loan_amount': loan_amount, 'interest_rate': interest_rate, 'time_period': time_period}
resutl = self.loan.ProcessLoanRequest(req)
response = LoanResponse(approved=resutl['approved'], message=resutl['message'])
return response
def getLoanHistory(self, request, context):
email = request.email
req = {'email': email}
loan_history = []
loans = self.loan.getLoanHistory(req)
for l in loans:
loan_history.append(Loan(name=l['name'], email=l['email'], account_type=l['account_type'], account_number=l['account_number'], govt_id_type=l['govt_id_type'], govt_id_number=l['govt_id_number'], loan_type=l['loan_type'], loan_amount=l['loan_amount'], interest_rate=l['interest_rate'], time_period=l['time_period'], status=l['status'], timestamp=f"{l['timestamp']}"))
return LoansHistoryResponse(loans=loan_history)
app = Flask(__name__)
loan_generic = LoanGeneric()
@app.route("/loan/request", methods=["POST"])
def process_loan_request():
request_data = request.json
logging.debug(f"Request: {request_data}")
response = loan_generic.ProcessLoanRequest(request_data)
return jsonify(response)
@app.route("/loan/history", methods=["POST"])
def get_loan_history():
logging.debug("----------------> Request: /loan/history")
d = request.json
logging.debug(f"Request: {d}")
response = loan_generic.getLoanHistory({"email": d['email']})
return jsonify(response)
def serverGRPC(port):
logging.debug(f"Starting GRPC server on port {port}")
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
loan_pb2_grpc.add_LoanServiceServicer_to_server(LoanService(), server)
server.add_insecure_port(f"[::]:{port}")
server.start()
server.wait_for_termination()
def serverFlask(port):
logging.debug(f"Starting Flask server on port {port}")
app.run(host='0.0.0.0' ,port=port, debug=True)
if __name__ == "__main__":
port = 50053
if protocol == "grpc":
serverGRPC(port)
else:
serverFlask(port)