- Added subscription_service.py for managing subscriptions with functions to create, read, update, and delete subscriptions. - Added tenant_service.py for managing tenants with functions to create, read, update, delete tenants, and assign users to tenants. - Added user_service.py for managing users with functions to create, read, update, and delete users. - Created tests for business types, plans, subscriptions, tenants, and users, ensuring proper functionality and error handling. - Implemented pagination in user listing and included user profiles in responses. - Established fixtures for database setup and teardown in tests.
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.subscription import Subscription
|
|
from app.schemas.subscription import SubscriptionCreate, SubscriptionUpdate
|
|
|
|
|
|
def get_subscription_list(db: Session):
|
|
return db.query(Subscription).all()
|
|
|
|
|
|
def get_subscription_by_id(db: Session, id: str):
|
|
sub = db.query(Subscription).filter(Subscription.id == id).first()
|
|
if not sub:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
return sub
|
|
|
|
|
|
def create_subscription(db: Session, data: SubscriptionCreate):
|
|
existing = db.query(Subscription).filter(Subscription.tenant_id == data.tenant_id).first()
|
|
if existing:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail={"message": "Tenant already has a subscription"},
|
|
)
|
|
|
|
sub = Subscription(**data.model_dump())
|
|
db.add(sub)
|
|
db.commit()
|
|
db.refresh(sub)
|
|
return sub
|
|
|
|
|
|
def update_subscription(db: Session, id: str, data: SubscriptionUpdate):
|
|
sub = db.query(Subscription).filter(Subscription.id == id).first()
|
|
if not sub:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(sub, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(sub)
|
|
return sub
|
|
|
|
|
|
def delete_subscription(db: Session, id: str):
|
|
sub = db.query(Subscription).filter(Subscription.id == id).first()
|
|
if not sub:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
|
|
db.delete(sub)
|
|
db.commit()
|