- 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.
119 lines
3.3 KiB
Python
119 lines
3.3 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.subscription import Subscription
|
|
from app.models.tenant import Tenant
|
|
from app.models.user import User
|
|
from app.schemas.tenant import TenantCreate, TenantUpdate
|
|
|
|
|
|
def get_tenant_list(db: Session):
|
|
return db.query(Tenant).filter(Tenant.deleted_at.is_(None)).all()
|
|
|
|
|
|
def get_tenant_by_id(db: Session, id: str):
|
|
tenant = db.query(Tenant).filter(Tenant.id == id, Tenant.deleted_at.is_(None)).first()
|
|
if not tenant:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
return tenant
|
|
|
|
|
|
def create_tenant(db: Session, data: TenantCreate):
|
|
existing = db.query(Tenant).filter(Tenant.slug == data.slug).first()
|
|
if existing:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail={"message": "Slug already exists"},
|
|
)
|
|
|
|
try:
|
|
tenant = Tenant(
|
|
name=data.name,
|
|
slug=data.slug,
|
|
business_type_id=data.business_type_id,
|
|
phone=data.phone,
|
|
address=data.address,
|
|
email=data.email,
|
|
)
|
|
db.add(tenant)
|
|
db.flush()
|
|
|
|
subscription = Subscription(
|
|
tenant_id=tenant.id,
|
|
plan_id=data.plan_id,
|
|
limits=data.limits,
|
|
)
|
|
db.add(subscription)
|
|
db.commit()
|
|
except:
|
|
db.rollback()
|
|
raise
|
|
|
|
db.refresh(tenant)
|
|
return tenant
|
|
|
|
|
|
def update_tenant(db: Session, id: str, data: TenantUpdate):
|
|
tenant = db.query(Tenant).filter(Tenant.id == id, Tenant.deleted_at.is_(None)).first()
|
|
if not tenant:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
|
|
if "slug" in update_data:
|
|
dup = db.query(Tenant).filter(Tenant.slug == update_data["slug"], Tenant.id != id).first()
|
|
if dup:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail={"message": "Slug already exists"},
|
|
)
|
|
|
|
for field, value in update_data.items():
|
|
setattr(tenant, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(tenant)
|
|
return tenant
|
|
|
|
|
|
def delete_tenant(db: Session, id: str):
|
|
tenant = db.query(Tenant).filter(Tenant.id == id, Tenant.deleted_at.is_(None)).first()
|
|
if not tenant:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "The item does not exist"},
|
|
)
|
|
|
|
tenant.deleted_at = datetime.now()
|
|
db.commit()
|
|
|
|
|
|
def assign_user_to_tenant(db: Session, tenant_id: str, user_id: str):
|
|
tenant = db.query(Tenant).filter(Tenant.id == tenant_id).first()
|
|
if not tenant:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "Tenant not found"},
|
|
)
|
|
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": "User not found"},
|
|
)
|
|
|
|
user.tenant_id = tenant_id
|
|
user.status = "ACTIVE"
|
|
db.commit()
|
|
|
|
return {"message": "User assigned to tenant"}
|