Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: withdraw cash error messages #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def create_new(self, first_name: str, last_name: str, age: int, e_mail: str,
error_message=[f"{get_error(error_suf[1])}"]
)

new_user = self.get_user()[-1]
new_user = self.get_user()["data"][-1]

return Schema.api_response(
status=200,
Expand Down
69 changes: 46 additions & 23 deletions controllers/withdrawal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from cmath import pi
from controllers.user import User_Controller
from database.db_connector import SQLite_Connector
from schemas.schemas import Schema
from src.error_message import Error_Message,get_error
from src.success_message import Success_Message

user_controller = User_Controller()

Expand All @@ -9,41 +12,61 @@ def __init__(self, db_name='database/bank_simulator.db') -> None:
super().__init__(db_name)


def get_withdrawal(self, id=None):
def get_money_withdrawal(self, id=None):

money_withdrawals = None

if id:
return self.execute_sql_query(f"SELECT * FROM money_withdrawal WHERE id={id}", Schema.withdrawal)
money_withdrawals = self.execute_sql_query(f"SELECT * FROM money_withdrawal WHERE id={id}", Schema.withdrawal)
else:
money_withdrawals = self.execute_sql_query(f"SELECT * FROM money_withdrawal", Schema.withdrawal)

return self.execute_sql_query(f"SELECT * FROM money_withdrawal", Schema.withdrawal)
if id and not money_withdrawals:
return Schema.api_response(status=404,error_message=[
Error_Message.withdrawal_not_exist.value,])
elif not id and not money_withdrawals:
return Schema.api_response(status=404,error_message=[
Error_Message.there_not_withdrawals.value])
return Schema.api_response(status=200,data = money_withdrawals)


def create_new(self, id_user: int, amount: float, withdrawal_date:str ) -> list:

withdrawal_status = self.withdrawal_amount(id_user,amount)
user = user_controller.get_user(id_user)["data"][0]
print(user)

if not user:
return Schema.api_response(status=404, error_message=[
Error_Message.user_not_exist.value]
)

if user["balance"] < amount:
return Schema.api_response(status=200, error_message=[
Error_Message.negative_balance.value]
)

if not withdrawal_status:
print("levantamento regeitado")
return []
new_user_balance = user["balance"] - amount
user_controller.update_user_balance(user["account_number"], new_user_balance)

sql_query = f"""
INSERT INTO money_withdrawal
(id_user, amount, withdrawal_date)
VALUES
({id_user},{amount},'{withdrawal_date}');
"""
self.execute_sql_query(sql_query,Schema.withdrawal)

return self.get_withdrawal()[-1]

def withdrawal_amount(self, id_user, amount) -> bool:
user = user_controller.get_user(id_user)[0]

if not user:
return False

if user["balance"] < amount:
return False

new_user_balance = user["balance"] - amount
user_controller.update_user_balance(user["account_number"], new_user_balance)
try:
self.execute_sql_query(sql_query,Schema.withdrawal)
except Exception as error:
error_suf = f"{error}".split(".")
return Schema.api_response(
status=500,
error_message=[f"{get_error(error_suf[1])}"]
)

return True
new_money_withdrawal = self.get_money_withdrawal()["data"][-1]
return Schema.api_response(
status=200,
data=new_money_withdrawal,
success_message=[Success_Message.new_withdrawal.value]
)

Binary file modified database/bank_simulator.db
Binary file not shown.
Binary file modified db/bank_simulator.db
Binary file not shown.
4 changes: 2 additions & 2 deletions routes/withdrawal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self) -> None:
super().__init__()

def get(self):
return jsonify(withdrawal_controller.get_withdrawal())
return jsonify(withdrawal_controller.get_money_withdrawal())

def post(self):
args = request_put_args.parse_args()
Expand All @@ -34,7 +34,7 @@ def __init__(self) -> None:
super().__init__()

def get(self, id):
return jsonify(withdrawal_controller.get_withdrawal(id))
return jsonify(withdrawal_controller.get_money_withdrawal(id))



Expand Down
7 changes: 5 additions & 2 deletions src/error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class Error_Message(Enum):

id_not_exist = "This user ID doesn't exist"
user_not_exist = "This user doesn't exist"
there_not_existent_users = "There is no registred users on this app"
there_not_existent_users = "There are no registered users on this app"
e_mail_already_exist = "This email address is already in use"
nif_already_exist = "This NIF is used"
negative_balance = "Amount grather than the account balance"
negative_balance = "Amount greater than the account balance"
equal_password = "Old and New password are the both the same"
internal_error = "Server internal Error"
admin_password_error = "The Admin Password do not match"
withdrawal_not_exist = "This money withdrawal doesn't exist"
there_not_withdrawals = "There are no registered withdrawals on this app"



def get_error(error_suf: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/success_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Success_Message(Enum):
password_updated = "Password updated successfully"
deleted_user = "User deleted successfully"
transfer_success = "Transfer made successfully"
transfer_success_1 = "Transfer made successfully 1"
new_withdrawal = "Successful withdrawal"