63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
import json,sys,os,copy;from datetime import datetime
|
|
|
|
def loadJ(p):
|
|
f=open(p,'r');d=json.load(f);f.close();return d
|
|
|
|
def saveJ(p,d):
|
|
f=open(p,'w');json.dump(d,f,indent=2);f.close()
|
|
|
|
def proc(accs,txns):
|
|
ok=[];bad=[]
|
|
for t in txns:
|
|
tp=t['type'];aid=t['account_id'];amt=t['amount'];tid=t['id']
|
|
# find account
|
|
a=None
|
|
for x in accs:
|
|
if x['account_id']==aid:a=x
|
|
if a==None:
|
|
t['reason']='account not found';bad.append(t);continue
|
|
if a['status']!='active':
|
|
t['reason']='account not active';bad.append(t);continue
|
|
if amt<=0 and tp!='withdrawal':
|
|
if tp=='deposit':t['reason']='invalid amount';bad.append(t);continue
|
|
if tp=='transfer':t['reason']='invalid amount';bad.append(t);continue
|
|
if amt<=0 and tp=='withdrawal':
|
|
t['reason']='invalid amount';bad.append(t);continue
|
|
if tp=='deposit':
|
|
a['balance']=a['balance']+amt;t['status']='accepted';ok.append(t)
|
|
elif tp=='withdrawal':
|
|
if a['balance']>=amt:
|
|
a['balance']=a['balance']-amt;t['status']='accepted';ok.append(t)
|
|
else:
|
|
t['reason']='insufficient funds';t['status']='rejected';bad.append(t)
|
|
elif tp=='transfer':
|
|
ta=None
|
|
for x in accs:
|
|
if x['account_id']==t.get('to_account_id',''):ta=x
|
|
if ta==None:t['reason']='target account not found';bad.append(t);continue
|
|
if ta['status']!='active':t['reason']='target account not active';bad.append(t);continue
|
|
if a['balance']>=amt:
|
|
a['balance']=a['balance']-amt;ta['balance']=ta['balance']+amt
|
|
t['status']='accepted';ok.append(t)
|
|
else:
|
|
t['reason']='insufficient funds';t['status']='rejected';bad.append(t)
|
|
else:
|
|
t['reason']='unknown type';bad.append(t)
|
|
return accs,ok,bad
|
|
|
|
def main():
|
|
D=loadJ('accounts.json');T=loadJ('transactions.json')
|
|
accs=D['accounts'];txns=T['transactions']
|
|
accs,ok,bad=proc(accs,txns)
|
|
# print results
|
|
print("=== UPDATED ACCOUNTS ===")
|
|
for a in accs:print(" "+a['account_id']+" "+a['holder']+": "+str(a['balance'])+" "+a['currency']+" ("+a['status']+")")
|
|
print("\n=== ACCEPTED ("+str(len(ok))+") ===")
|
|
for t in ok:print(" "+t['id']+" "+t['type']+" "+str(t['amount'])+" -> "+t.get('description',''))
|
|
print("\n=== REJECTED ("+str(len(bad))+") ===")
|
|
for t in bad:print(" "+t['id']+" "+t['type']+" "+str(t['amount'])+" -> "+t.get('reason','unknown'))
|
|
saveJ('accounts_updated_bad.json',{"accounts":accs})
|
|
saveJ('transaction_log_bad.json',{"accepted":ok,"rejected":bad})
|
|
|
|
main()
|