updates
This commit is contained in:
BIN
Backend/src/utils/__pycache__/currency_helpers.cpython-312.pyc
Normal file
BIN
Backend/src/utils/__pycache__/currency_helpers.cpython-312.pyc
Normal file
Binary file not shown.
BIN
Backend/src/utils/__pycache__/response_helpers.cpython-312.pyc
Normal file
BIN
Backend/src/utils/__pycache__/response_helpers.cpython-312.pyc
Normal file
Binary file not shown.
BIN
Backend/src/utils/__pycache__/role_helpers.cpython-312.pyc
Normal file
BIN
Backend/src/utils/__pycache__/role_helpers.cpython-312.pyc
Normal file
Binary file not shown.
24
Backend/src/utils/currency_helpers.py
Normal file
24
Backend/src/utils/currency_helpers.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
Utility functions for currency handling
|
||||
"""
|
||||
CURRENCY_SYMBOLS = {
|
||||
'USD': '$',
|
||||
'EUR': '€',
|
||||
'GBP': '£',
|
||||
'JPY': '¥',
|
||||
'CNY': '¥',
|
||||
'KRW': '₩',
|
||||
'SGD': 'S$',
|
||||
'THB': '฿',
|
||||
'AUD': 'A$',
|
||||
'CAD': 'C$',
|
||||
'VND': '₫',
|
||||
'INR': '₹',
|
||||
'CHF': 'CHF',
|
||||
'NZD': 'NZ$'
|
||||
}
|
||||
|
||||
def get_currency_symbol(currency: str) -> str:
|
||||
"""Get currency symbol for a given currency code"""
|
||||
return CURRENCY_SYMBOLS.get(currency.upper(), currency)
|
||||
|
||||
51
Backend/src/utils/response_helpers.py
Normal file
51
Backend/src/utils/response_helpers.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
Utility functions for standardizing API responses
|
||||
"""
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
def success_response(
|
||||
data: Any = None,
|
||||
message: Optional[str] = None,
|
||||
**kwargs
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create a standardized success response.
|
||||
Returns both 'success' (boolean) and 'status' (string) for backward compatibility.
|
||||
"""
|
||||
response: Dict[str, Any] = {
|
||||
'success': True,
|
||||
'status': 'success'
|
||||
}
|
||||
|
||||
if data is not None:
|
||||
response['data'] = data
|
||||
|
||||
if message:
|
||||
response['message'] = message
|
||||
|
||||
# Add any additional fields
|
||||
response.update(kwargs)
|
||||
|
||||
return response
|
||||
|
||||
def error_response(
|
||||
message: str,
|
||||
errors: Optional[list] = None,
|
||||
**kwargs
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create a standardized error response.
|
||||
"""
|
||||
response: Dict[str, Any] = {
|
||||
'success': False,
|
||||
'status': 'error',
|
||||
'message': message
|
||||
}
|
||||
|
||||
if errors:
|
||||
response['errors'] = errors
|
||||
|
||||
response.update(kwargs)
|
||||
|
||||
return response
|
||||
|
||||
47
Backend/src/utils/role_helpers.py
Normal file
47
Backend/src/utils/role_helpers.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Utility functions for role-based access control
|
||||
"""
|
||||
from sqlalchemy.orm import Session
|
||||
from ..models.user import User
|
||||
from ..models.role import Role
|
||||
|
||||
def get_user_role_name(user: User, db: Session) -> str:
|
||||
"""Get the role name for a user"""
|
||||
role = db.query(Role).filter(Role.id == user.role_id).first()
|
||||
return role.name if role else 'customer'
|
||||
|
||||
def is_admin(user: User, db: Session) -> bool:
|
||||
"""Check if user is admin"""
|
||||
return get_user_role_name(user, db) == 'admin'
|
||||
|
||||
def is_staff(user: User, db: Session) -> bool:
|
||||
"""Check if user is staff"""
|
||||
return get_user_role_name(user, db) == 'staff'
|
||||
|
||||
def is_accountant(user: User, db: Session) -> bool:
|
||||
"""Check if user is accountant"""
|
||||
return get_user_role_name(user, db) == 'accountant'
|
||||
|
||||
def is_customer(user: User, db: Session) -> bool:
|
||||
"""Check if user is customer"""
|
||||
return get_user_role_name(user, db) == 'customer'
|
||||
|
||||
def can_access_all_payments(user: User, db: Session) -> bool:
|
||||
"""Check if user can see all payments (admin or accountant)"""
|
||||
role_name = get_user_role_name(user, db)
|
||||
return role_name in ['admin', 'accountant']
|
||||
|
||||
def can_access_all_invoices(user: User, db: Session) -> bool:
|
||||
"""Check if user can see all invoices (admin or accountant)"""
|
||||
role_name = get_user_role_name(user, db)
|
||||
return role_name in ['admin', 'accountant']
|
||||
|
||||
def can_create_invoices(user: User, db: Session) -> bool:
|
||||
"""Check if user can create invoices (admin, staff, or accountant)"""
|
||||
role_name = get_user_role_name(user, db)
|
||||
return role_name in ['admin', 'staff', 'accountant']
|
||||
|
||||
def can_manage_users(user: User, db: Session) -> bool:
|
||||
"""Check if user can manage users (admin only)"""
|
||||
return is_admin(user, db)
|
||||
|
||||
Reference in New Issue
Block a user