Compare commits

..

1 Commits

Author SHA1 Message Date
41229ba0bb feat(db): add account existence check before insertion
prevent duplicate accounts by checking if an account with the same username and stream already exists for the user before inserting a new one. returns a 409 conflict error if duplicate is detected.
2025-08-18 14:54:50 +01:00

View File

@ -148,9 +148,14 @@ def add_account(user_id: int) -> Response:
user_id: The ID of the user.
Returns:
A Flask JSON response confirming the account was added.
A Flask JSON response confirming the account was added or an error message.
"""
data = request.form
# Check if account already exists
if account_exists(user_id, data["username"], data["stream"]):
return jsonify({"error": "Account already exists for this user"}), 409
res = single_account_check(data, get_latest_urls_from_dns())
encrypted_password = encrypt_password(data["password"])
query = "INSERT INTO userAccounts (username, stream, streamURL, expiaryDate, password, userID, maxConnections) VALUES (%s, %s, %s, %s, %s, %s, %s)"
@ -167,6 +172,23 @@ def add_account(user_id: int) -> Response:
return jsonify(result)
def account_exists(user_id: int, username: str, stream: str) -> bool:
"""Check if an account with the same username and stream already exists for the user.
Args:
user_id: The ID of the user.
username: The username of the account.
stream: The stream of the account.
Returns:
True if the account exists, False otherwise.
"""
query = "SELECT COUNT(*) as count FROM userAccounts WHERE userID = %s AND username = %s AND stream = %s"
params = (user_id, username, stream)
result = _execute_query(query, params)
return result[0]["count"] > 0
def update_expiry_date(username: str, stream: str, expiry_date: str) -> None:
"""Updates the expiry date of an account.