added update account and client functionality to api
This commit is contained in:
120
api.yml
120
api.yml
@@ -157,26 +157,54 @@ paths:
|
||||
summary: Update an existing client
|
||||
description: Update an existing client Id
|
||||
operationId: manager.update_client
|
||||
requestBody:
|
||||
description: Update an existing client's details
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Client'
|
||||
parameters:
|
||||
- name: client_id
|
||||
in: query
|
||||
description: ID of client to update
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- name: name
|
||||
in: query
|
||||
description: Client Name
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: birthdate
|
||||
in: query
|
||||
description: Client Birthdate (dd-mm-yyyy)
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: address
|
||||
in: query
|
||||
description: Client Address
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: phone_number
|
||||
in: query
|
||||
description: Client Phone Number
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: email
|
||||
in: query
|
||||
description: Client Email Address
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: notes
|
||||
in: query
|
||||
description: Notes about client
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Client'
|
||||
'400':
|
||||
description: Invalid Client ID supplied
|
||||
'404':
|
||||
description: Client not found
|
||||
'422':
|
||||
description: Validation exception
|
||||
get:
|
||||
tags:
|
||||
- client
|
||||
@@ -273,28 +301,31 @@ paths:
|
||||
description: ID of account to update
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
format: int32
|
||||
requestBody:
|
||||
description: Update an existing account
|
||||
content:
|
||||
application/json:
|
||||
type: string
|
||||
- name: description
|
||||
in: query
|
||||
description: Account description
|
||||
required: false
|
||||
schema:
|
||||
$ref: '#/components/schemas/Account'
|
||||
required: true
|
||||
type: string
|
||||
- name: account_type
|
||||
in: query
|
||||
description: Type of account
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: notes
|
||||
in: query
|
||||
description: Notes about account
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Account'
|
||||
'400':
|
||||
description: Invalid Account ID supplied
|
||||
'404':
|
||||
description: Account not found
|
||||
'422':
|
||||
description: Validation exception
|
||||
get:
|
||||
tags:
|
||||
- account
|
||||
@@ -353,8 +384,7 @@ paths:
|
||||
description: ID of transaction to return
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
format: int32
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation
|
||||
@@ -535,32 +565,6 @@ paths:
|
||||
description: Invalid input
|
||||
'404':
|
||||
description: No transactions found
|
||||
put:
|
||||
tags:
|
||||
- manager
|
||||
summary: Update an existing transaction
|
||||
description: Update an existing transaction
|
||||
operationId: manager.update_transaction
|
||||
requestBody:
|
||||
description: Update an existing transaction
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
required: true
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
'400':
|
||||
description: Invalid Transaction ID supplied
|
||||
'404':
|
||||
description: Transaction not found
|
||||
'422':
|
||||
description: Validation exception
|
||||
/Manager/Hash:
|
||||
get:
|
||||
tags:
|
||||
|
||||
180
manager.py
180
manager.py
@@ -32,24 +32,12 @@ def generate_uuid_short(): # Generates a short uuid
|
||||
### Client ###
|
||||
##############
|
||||
|
||||
def get_client(client_id:int): # Returns a specific client in the database
|
||||
def get_client(client_id:str): # Returns a specific client in the database
|
||||
client = session.query(Client).filter_by(client_id=client_id).one_or_none()
|
||||
if client is None:
|
||||
return jsonify({"error": "Client not found"}), 404
|
||||
if client is not None:
|
||||
return jsonify({"name": client.name, "birthdate": client.birthdate, "opening_timestamp": client.opening_timestamp, "address": client.address, "phone_number": client.phone_number, "email": client.email}), 200
|
||||
|
||||
def change_password(client_id, password:str, new_password:str): # Changes the password of a client
|
||||
old_hash = password_hash(password)
|
||||
new_hash = password_hash(new_password)
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
if client.hash == old_hash:
|
||||
client.hash = new_hash
|
||||
session.commit()
|
||||
return "Password changed successfully.", 200
|
||||
return "Incorrect old password.", 400
|
||||
return f"client_id: {client_id} is not found.", 404
|
||||
return jsonify({"name": client.name, "birthdate": client.birthdate, "opening_timestamp": client.opening_timestamp, "address": client.address, "phone_number": client.phone_number, "email": client.email}), 200
|
||||
return jsonify({"error": "Client not found"}), 404
|
||||
|
||||
def add_client(name:str, birthdate:str, address:str, phone_number:str, email:str, password:str, **kwargs): # Adds a new client to the database
|
||||
client_id = generate_uuid_short()
|
||||
@@ -59,7 +47,7 @@ def add_client(name:str, birthdate:str, address:str, phone_number:str, email:str
|
||||
session.commit()
|
||||
return f"New client has been added: name: {name}, uuid: {client_id} ", 200
|
||||
|
||||
def delete_client(client_id): # Deletes a client from the database
|
||||
def delete_client(client_id:str): # Deletes a client from the database
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
if client.accounts == None:
|
||||
@@ -70,7 +58,42 @@ def delete_client(client_id): # Deletes a client from the database
|
||||
return f"client_id: {client_id} has active accounts and can not be removed.", 400
|
||||
return f"client_id: {client_id} is not found.", 404
|
||||
|
||||
def update_client(client_id:str, **kwargs): # Updates a client in the database
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
name = kwargs.get("name", None)
|
||||
birthdate = kwargs.get("birthdate", None)
|
||||
address = kwargs.get("address", None)
|
||||
phone_number = kwargs.get("phone_number", None)
|
||||
email = kwargs.get("email", None)
|
||||
notes = kwargs.get("notes", None)
|
||||
if name:
|
||||
client.name = name
|
||||
if birthdate:
|
||||
client.birthdate = birthdate
|
||||
if address:
|
||||
client.address = address
|
||||
if phone_number:
|
||||
client.phone_number = phone_number
|
||||
if email:
|
||||
client.email = email
|
||||
if notes:
|
||||
client.notes = notes
|
||||
session.commit()
|
||||
return f"client_id: {client_id} has been updated.", 299
|
||||
return f"Client ID: {client_id} is not found." , 400
|
||||
|
||||
def change_password(client_id:str, password:str, new_password:str): # Changes the password of a client
|
||||
old_hash = password_hash(password)
|
||||
new_hash = password_hash(new_password)
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
if client.hash == old_hash:
|
||||
client.hash = new_hash
|
||||
session.commit()
|
||||
return "Password changed successfully.", 200
|
||||
return "Incorrect old password.", 400
|
||||
return f"client_id: {client_id} is not found.", 404
|
||||
|
||||
|
||||
|
||||
@@ -85,18 +108,7 @@ def logout_user():
|
||||
|
||||
|
||||
|
||||
def update_client(client_id, name, birthdate, address, phone_number, email, notes):
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
client.name = name
|
||||
client.birthdate = birthdate
|
||||
client.address = address
|
||||
client.phone_number = phone_number
|
||||
client.email = email
|
||||
client.notes = notes
|
||||
session.commit()
|
||||
return f"client_id: {client_id} has been updated."
|
||||
return f"Client ID: {client_id} is not found."
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -104,15 +116,14 @@ def update_client(client_id, name, birthdate, address, phone_number, email, note
|
||||
### Account ###
|
||||
###############
|
||||
|
||||
def get_account(account_id:int): # Returns a specific account in the database
|
||||
def get_account(account_id:str): # Returns a specific account in the database
|
||||
account = session.query(Account).filter_by(account_id=account_id).one_or_none()
|
||||
if account is None:
|
||||
for account in session.query(Account).all():
|
||||
if account.account_id == account_id:
|
||||
return jsonify({"client_id": account.client_id, "description": account.description, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes}), 200
|
||||
return jsonify({"error": "Account not found"}), 404
|
||||
if account is not None:
|
||||
for account in account:
|
||||
return jsonify({"client_id": account.client.id, "description": account.description, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes}), 200
|
||||
|
||||
def add_account(client_id, description:str, account_type, **kwargs): # Adds a new account to the database
|
||||
def add_account(client_id:str, description:str, account_type:str, **kwargs): # Adds a new account to the database
|
||||
account_id = generate_uuid_short()
|
||||
notes = kwargs.get("notes", None)
|
||||
client_found = None
|
||||
@@ -121,27 +132,16 @@ def add_account(client_id, description:str, account_type, **kwargs): # Adds a ne
|
||||
if client.client_id == client_id:
|
||||
client_found = client
|
||||
break
|
||||
|
||||
# Check if client was found
|
||||
if client_found is None:
|
||||
return f"client_id: {client_id} is not found.", 422
|
||||
|
||||
# Add the new account
|
||||
new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None)
|
||||
session.add(new_account)
|
||||
session.commit()
|
||||
return f"New account has been added: description: {description}, uuid: {account_id} ", 200
|
||||
"""
|
||||
for client in session.query(Client).all():
|
||||
if client.client_id == client_id:
|
||||
new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None)
|
||||
session.add(new_account)
|
||||
session.commit()
|
||||
return f"New account has been added: description: {description}, uuid: {account_id} ", 200
|
||||
else:
|
||||
return f"client_id: {client_id} is not found.", 422
|
||||
"""
|
||||
def delete_account(account_id): # Deletes an account from the database
|
||||
|
||||
def delete_account(account_id:str): # Deletes an account from the database
|
||||
for account in session.query(Account).all():
|
||||
if account.account_id == account_id:
|
||||
if account.balance == 0:
|
||||
@@ -152,20 +152,27 @@ def delete_account(account_id): # Deletes an account from the database
|
||||
return f"account_id: {account_id} has a balance and can not be removed.", 400
|
||||
return f"account_id: {account_id} is not found.", 404
|
||||
|
||||
|
||||
|
||||
|
||||
def update_account(account_id:int, update:dict):
|
||||
def update_account(account_id:str, **kwargs): # Updates an account in the database
|
||||
for account in session.query(Account).all():
|
||||
if account.account_id == account_id:
|
||||
account.description = update["description"]
|
||||
account.account_type = update["account_type"]
|
||||
account.balance = update["balance"]
|
||||
account.enabled = update["enabled"]
|
||||
account.notes = update["notes"]
|
||||
description = kwargs.get("description", None)
|
||||
account_type = kwargs.get("account_type", None)
|
||||
balance = kwargs.get("balance", None)
|
||||
enabled = kwargs.get("enabled", None)
|
||||
notes = kwargs.get("notes", None)
|
||||
if description:
|
||||
account.description = description
|
||||
if account_type:
|
||||
account.account_type = account_type
|
||||
if balance:
|
||||
account.balance = balance
|
||||
if enabled:
|
||||
account.enabled = enabled
|
||||
if notes:
|
||||
account.notes = notes
|
||||
session.commit()
|
||||
return f"account_id: {update['account_id']} has been updated."
|
||||
return f"account_id: {update['account_id']} is not found."
|
||||
return f"account_id: {account_id} has been updated.", 200
|
||||
return f"account_id: {account_id} is not found.", 400
|
||||
|
||||
|
||||
|
||||
@@ -176,14 +183,10 @@ def update_account(account_id:int, update:dict):
|
||||
|
||||
def get_transaction(transaction_id:int): # Returns a specific transaction in the database
|
||||
transaction = session.query(Transaction).filter_by(transaction_id=transaction_id).one_or_none()
|
||||
if transaction is None:
|
||||
for transaction in session.query(Transaction).all():
|
||||
if transaction.transaction_id == transaction_id:
|
||||
return jsonify({"transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_id": transaction.account_id, "recipient_account_id": transaction.recipient_account_id}), 200
|
||||
return jsonify({"error": "Transaction not found"}), 404
|
||||
if transaction is not None:
|
||||
return jsonify({"transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_number, "recipient_account_number": transaction.recipient_account_number}), 200
|
||||
|
||||
def transaction_history(account_id:int): # Returns all transactions for a specific account
|
||||
result = session.query(Transaction).filter(Transaction.account_id == account_id)
|
||||
return jsonify([{"transaction_id": transaction.transaction_id, "transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_number, "recipient_account_number": transaction.recipient_account_number} for transaction in result]), 200
|
||||
|
||||
def add_transaction(amount:int, account_id, recipient_account_id, **kwargs): # Adds a new transaction to the database
|
||||
transaction_id = generate_uuid()
|
||||
@@ -192,53 +195,24 @@ def add_transaction(amount:int, account_id, recipient_account_id, **kwargs): # A
|
||||
account_from = account
|
||||
if account.account_id == recipient_account_id:
|
||||
account_dest = account
|
||||
|
||||
# Check if account has enough funds
|
||||
if account_from.balance < amount:
|
||||
return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401
|
||||
|
||||
# Perform the transaction
|
||||
account_from.balance -= amount
|
||||
account_dest.balance += amount
|
||||
transaction_type = "transfer"
|
||||
session.commit()
|
||||
|
||||
# Create the transaction record
|
||||
description = kwargs.get("description", None)
|
||||
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, recipient_account_id)
|
||||
session.add(new_transaction)
|
||||
session.commit()
|
||||
|
||||
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200
|
||||
"""
|
||||
if account_from is None:
|
||||
return f"Account ID: {account_id} is not found.", 404
|
||||
if account_dest is None:
|
||||
return f"Account ID: {account_to} is not found.", 404
|
||||
|
||||
for account in session.query(Account).all():
|
||||
if account.account_id == account_id:
|
||||
if account.balance < amount:
|
||||
return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401
|
||||
account.balance -= amount
|
||||
transaction_type = "withdraw"
|
||||
session.commit()
|
||||
return
|
||||
else:
|
||||
return f"Account ID: {account_id} is not found.", 404
|
||||
|
||||
for account in session.query(Account).all():
|
||||
if account.account_id == account_to:
|
||||
account.balance += amount
|
||||
transaction_type = "transfer"
|
||||
session.commit()
|
||||
description = kwargs.get("description", None)
|
||||
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, account_to)
|
||||
session.add(new_transaction)
|
||||
session.commit()
|
||||
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200
|
||||
|
||||
"""
|
||||
def transaction_history(account_id:int): # Returns all transactions for a specific account
|
||||
result = session.query(Transaction).filter(Transaction.account_id == account_id)
|
||||
return jsonify([{"transaction_id": transaction.transaction_id, "transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_id, "recipient_account_number": transaction.recipient_account_id} for transaction in result]), 200
|
||||
|
||||
#####################
|
||||
### Administrator ###
|
||||
@@ -258,18 +232,6 @@ def get_all_transactions(): # Returns all transactions in the database
|
||||
|
||||
|
||||
|
||||
def update_transaction(transaction_id, transaction_type, amount, description, account_id, recipient_account_id):
|
||||
for transaction in session.query(Transaction).all():
|
||||
if transaction.transaction_id == transaction_id:
|
||||
transaction.transaction_type = transaction_type
|
||||
transaction.amount = amount
|
||||
transaction.description = description
|
||||
transaction.account_id = account_id
|
||||
transaction.recipient_account_id = recipient_account_id
|
||||
session.commit()
|
||||
return f"Transaction ID: {transaction_id} has been updated."
|
||||
return f"Transaction ID: {transaction_id} is not found."
|
||||
|
||||
def apply_interest(account_id:int, interest_rate:float):
|
||||
for account in session.query(Account).filter(Account.account_id == account_id):
|
||||
if account.account_id == account_id:
|
||||
|
||||
Reference in New Issue
Block a user