-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_controllers.py
255 lines (208 loc) · 10 KB
/
account_controllers.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from bottle import get, post, request, run, route, template, redirect, static_file, error
from account_repository import create_dummy_account, get_account_details
from account_transactions import AccountTransactions
from account_balance_repository import get_account_balance_details, add_balance_detail, create_dummy_balance_for_account, update_account_balance
from account_transaction_repository import generate_reference_id, get_todays_date, get_current_datetime, add_transaction_detail, get_transactions_by_account_no, get_transactions_by_channel, get_transactions_by_reference, get_debit_charge
from account_operations import AccountOperations
from account_operations_repository import update_account_status
from account_table import create_tables
import json
import time
import os
from db_connect import create_connection
create_tables()
create_dummy_account()
create_dummy_balance_for_account()
@route('/account/debit/', method=['POST'])
def debit_account():
try:
connection = create_connection()
cursor = connection.cursor()
account_number = request.forms.get('account_number')
amount = request.forms.get('amount')
currency = request.forms.get('currency')
debit_or_credit = request.forms.get('debit_or_credit')
narration = request.forms.get('narration')
type_of_transaction = request.forms.get('type_of_transaction')
channel = request.forms.get('channel')
transaction = AccountTransactions(account_number, amount, currency,
debit_or_credit, narration, generate_reference_id(), get_current_datetime(),
type_of_transaction, get_todays_date(), channel)
details_of_account = get_account_details(account_number)
account_balance_details = get_account_balance_details(account_number)
debit_charge = get_debit_charge(amount, channel, account_number)
debit_response = transaction.debit_account(int(transaction.amount) + int(debit_charge), details_of_account, account_balance_details)
if (debit_response is "Success"):
update_account_balance(account_number, account_balance_details["available_balance"])
account_balance_details = get_account_balance_details(account_number)
add_transaction = add_transaction_detail(account_number, amount, currency,
debit_or_credit, narration, generate_reference_id(), get_current_datetime(),
type_of_transaction, get_todays_date(), account_balance_details["available_balance"], channel)
add_balance = add_balance_detail(account_number, currency, account_balance_details["available_balance"],
account_balance_details["cleared_balance"], account_balance_details["unclear_balance"], account_balance_details["hold_balance"],
account_balance_details["minimum_balance"])
if (debit_response is "Success"):
data = {
"message": "Debit operation successful!",
"status": "OK",
"Available Balance": account_balance_details["available_balance"],
"success": True
}
else:
data = {
"message": debit_response,
"status": "Failed",
"Available Balance": account_balance_details["available_balance"],
"success": False
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/account/credit/', method=['POST'])
def credit_account():
try:
account_number = request.forms.get('account_number')
amount = request.forms.get('amount')
currency = request.forms.get('currency')
debit_or_credit = request.forms.get('debit_or_credit')
narration = request.forms.get('narration')
type_of_transaction = request.forms.get('type_of_transaction')
channel = request.forms.get('channel')
transaction = AccountTransactions(account_number, amount, currency,
debit_or_credit, narration, generate_reference_id(), get_current_datetime(),
type_of_transaction, get_todays_date(), channel)
details_of_account = get_account_details(account_number)
account_balance_details = get_account_balance_details(account_number)
print(account_balance_details)
credit_response = transaction.credit_account(transaction.amount, details_of_account, account_balance_details)
if (credit_response == "Success"):
update_account_balance(account_number, account_balance_details["available_balance"])
account_balance_details = get_account_balance_details(account_number)
add_transaction = add_transaction_detail(account_number, amount, currency,
debit_or_credit, narration, generate_reference_id(), get_current_datetime(),
type_of_transaction, get_todays_date(), account_balance_details["available_balance"], channel)
print(add_transaction)
add_balance = add_balance_detail(account_number, currency, account_balance_details["available_balance"],
account_balance_details["cleared_balance"], account_balance_details["unclear_balance"], account_balance_details["hold_balance"],
account_balance_details["minimum_balance"])
if (credit_response == "Success"):
data = {
"message": "Credit operation successful!",
"status": "OK",
"Available Balance": account_balance_details["available_balance"],
"success": True
}
else:
data = {
"message": credit_response,
"status": "Failed",
"Available Balance": account_balance_details["available_balance"],
"success": False
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/account/freeze/', method=['POST'])
def freeze_account():
try:
account_number = request.forms.get('account_number')
details_of_account = get_account_details(account_number)
operation = AccountOperations(account_number)
response = operation.freeze_account(details_of_account)
if (response == "Success"):
update_account_status(account_number, details_of_account["status"])
details_of_account = get_account_details(account_number)
if (response == "Success"):
data = {
"message": "Operation successful!",
"status": "OK",
"Account Status": details_of_account["status"],
"success": True
}
else:
data = {
"message": response,
"status": "Failed",
"Account Status": details_of_account["status"],
"success": False
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/account/unfreeze/', method=['POST'])
def unfreeze_account():
try:
account_number = request.forms.get('account_number')
details_of_account = get_account_details(account_number)
operation = AccountOperations(account_number)
response = operation.unfreeze_account(details_of_account)
if (response == "Success"):
update_account_status(account_number, details_of_account["status"])
details_of_account = get_account_details(account_number)
if (response == "Success"):
data = {
"message": "Operation successful!",
"status": "OK",
"Account Status": details_of_account["status"],
"success": True
}
else:
data = {
"message": response,
"status": "Failed",
"Account Status": details_of_account["status"],
"success": False
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/hello/<name>')
def greet(name='Stranger'):
return template('Hello {{name}}, how are you?', name=name)
@route('/account/getAllTransactions/<account_number>')
def getCustomerTransactions(account_number=None):
try:
transactions = get_transactions_by_account_no(account_number)
data = {
"message": "Operation successful!",
"status": "OK",
"Transactions": transactions,
"success": True
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/account/getAllTransactions/<account_number>/<channel>')
def getCustomerTransactions(account_number=None, channel=None):
try:
transactions = get_transactions_by_channel(account_number, channel)
data = {
"message": "Operation successful!",
"status": "OK",
"Transactions": transactions,
"success": True
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
@route('/account/getAllTransactions/<account_number>/<reference>')
def getCustomerTransactions(account_number=None, reference=None):
try:
transactions = get_transactions_by_reference(account_number, reference)
data = {
"message": "Operation successful!",
"status": "OK",
"Transactions": transactions,
"success": True
}
response = json.dumps(data)
return response
except Exception as exception:
return exception
run(host='localhost', port=8080, debug=True)