feat: Add helper functions for User

This commit is contained in:
Backend IM Bot 2025-04-11 11:23:43 +00:00
parent c1446359a3
commit 4d685c1555

60
helpers/user_helpers.py Normal file
View File

@ -0,0 +1,60 @@
from typing import Optional
from sqlalchemy.orm import Session
from models.user import User
from schemas.user import UserLogin
def authenticate_user(db: Session, user_data: UserLogin) -> Optional[User]:
"""
Authenticate a user by checking if the provided email and password match a record in the database.
Args:
db (Session): The SQLAlchemy database session.
user_data (UserLogin): The user login data containing email and password.
Returns:
Optional[User]: The User object if authentication is successful, None otherwise.
"""
user = db.query(User).filter(User.email == user_data.email).first()
if user and user.verify_password(user_data.password):
return user
return None
def get_user_with_phone(db: Session, user_data: UserLogin) -> Optional[User]:
"""
Retrieve a user object from the database, including the phone number, based on the provided login data.
Args:
db (Session): The SQLAlchemy database session.
user_data (UserLogin): The user login data containing email and password.
Returns:
Optional[User]: The User object with phone number if found, None otherwise.
"""
user = authenticate_user(db, user_data)
if user:
return db.query(User).filter(User.email == user_data.email).first()
return None
def authenticate_user_and_get_phone(db: Session, user_data: UserLogin) -> Optional[User]:
"""
Authenticate a user and retrieve the user object with the phone number.
Args:
db (Session): The SQLAlchemy database session.
user_data (UserLogin): The user login data containing email and password.
Returns:
Optional[User]: The User object with phone number if authentication is successful, None otherwise.
"""
return get_user_with_phone(db, user_data)
```
These helper functions are designed to work with the provided `login` endpoint. Here's a breakdown of their functionality:
1. `authenticate_user(db, user_data)`: This function takes a database session and a `UserLogin` object as input. It queries the database for a user with the provided email and checks if the password matches. If the user is found and the password is correct, it returns the `User` object. Otherwise, it returns `None`.
2. `get_user_with_phone(db, user_data)`: This function first calls `authenticate_user` to authenticate the user based on the provided login data. If the user is authenticated successfully, it retrieves the `User` object from the database, including the phone number, by querying with the email address. If the user is not authenticated, it returns `None`.
3. `authenticate_user_and_get_phone(db, user_data)`: This function is a convenience function that combines the functionality of `authenticate_user` and `get_user_with_phone`. It authenticates the user and retrieves the `User` object with the phone number in a single function call.
These helper functions assume that the `User` model has a `verify_password` method to check the provided password against the stored password hash. Additionally, the `User` model should have a `phone` attribute to store the user's phone number.