Advertisement
Alatri_Aymen

Untitled

Dec 29th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. import json
  2. import os
  3. import sys
  4. import uuid
  5. import boto3
  6. import time
  7.  
  8. s3 = boto3.client('s3')
  9.  
  10. def getUser(users, userId):
  11.     for u in users:
  12.         if(u['user_id'] == userId):
  13.             return u
  14.     return None
  15.  
  16.  
  17. def getAccount(accounts, account_id):
  18.     for a in accounts:
  19.         if(a['account_id'] == account_id):
  20.             return a
  21.     return None
  22.  
  23.  
  24. def checkTransactionValid(account, transaction):
  25.     if(float(transaction['transaction_amount']) <= 0.0):
  26.         return False
  27.     if(float(account['account_balance']) < float(transaction['transaction_amount'])):
  28.         return False
  29.     return True
  30.  
  31.  
  32. def completeTransaction(senderAccount, receiverAccount, transaction):
  33.     receiverAccount['account_balance'] = float(
  34.         receiverAccount['account_balance']) + float(transaction['transaction_amount'])
  35.     senderAccount['account_balance'] = float(
  36.         senderAccount['account_balance']) - float(transaction['transaction_amount'])
  37.        
  38. def saveConfig(senderAccount, receiverAccount, config):
  39.     config['accounts'] = list([
  40.         senderAccount, receiverAccount])
  41.     bucketName = 'transactionsresults'
  42.     s3 = boto3.resource('s3')
  43.     bucket = s3.Bucket(bucketName)
  44.     path = 'tmp/transaction_result_'+str(time.time())+'.json'
  45.     data = json.dumps(config)
  46.     bucket.put_object(
  47.         ACL='public-read',
  48.         ContentType='application/json',
  49.         Key=path,
  50.         Body=data,
  51.     )
  52.  
  53.     body = {
  54.         "uploaded": "true",
  55.         "bucket": bucketName,
  56.         "path": path,
  57.     }
  58.     return {
  59.         "statusCode": 200,
  60.         "body": json.dumps(body)
  61.     }
  62.  
  63.  
  64. def transaction_handler(event, context):
  65.     bucket = 'transactionstorage'
  66.     key = 'transaction.json'    
  67.     response = s3.get_object(Bucket = bucket,Key=key)
  68.     content = response['Body']
  69.     jsontransaction = json.loads(content.read())
  70.     transaction = jsontransaction['transaction']
  71.     with open('./config.json') as conf:
  72.         config = json.load(conf)
  73.     users = config['users']
  74.     accounts = config['accounts']
  75.     sender = getUser(users, transaction['sender_id'])
  76.     receiver = getUser(users, transaction['receiver_id'])
  77.     if((sender == None) or (receiver == None)):
  78.         return {
  79.             'statusCode': 400,
  80.             'body': 'Invalid sender or receiver ID'
  81.         }
  82.  
  83.     senderAccount = getAccount(accounts, sender['user_account_id'])
  84.     receiverAccount = getAccount(accounts, receiver['user_account_id'])
  85.     if(senderAccount == None):
  86.         return {
  87.             'statusCode': 400,
  88.             'body': 'There\'s no accounts registered for the user '+sender['user_id']
  89.         }
  90.     if(receiverAccount == None):
  91.         return {
  92.             'statusCode': 400,
  93.             'body': 'There\'s no accounts registered for the user '+receiver['user_Id']
  94.         }
  95.     validTransaction = checkTransactionValid(senderAccount, transaction)
  96.     if(validTransaction == False):
  97.         return {
  98.             'statusCode': 400,
  99.             'body': 'Transaction not valid'
  100.         }
  101.     completeTransaction(senderAccount, receiverAccount, transaction)
  102.     saveConfig(senderAccount, receiverAccount, config)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement