| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- """
- Organization management service.
- """
- from sqlalchemy import func, select
- from sqlalchemy.ext.asyncio import AsyncSession
- from app.models.organization import Organization
- from app.schemas.organization import OrganizationCreate, OrganizationUpdate
- async def create_organization(
- db: AsyncSession,
- data: OrganizationCreate,
- ) -> Organization:
- """
- Create a new organization.
- Args:
- db: Database session
- data: Organization creation data
- Returns:
- Created organization
- """
- org = Organization(
- name=data.name,
- contact_email=data.contact_email,
- contact_phone=data.contact_phone,
- wifi_enabled=data.wifi_enabled,
- ble_enabled=data.ble_enabled,
- status="active", # Superadmin creates active orgs
- )
- db.add(org)
- await db.commit()
- await db.refresh(org)
- return org
- async def get_organization(db: AsyncSession, organization_id: int) -> Organization | None:
- """
- Get organization by ID.
- Args:
- db: Database session
- organization_id: Organization ID
- Returns:
- Organization or None
- """
- result = await db.execute(
- select(Organization).where(Organization.id == organization_id)
- )
- return result.scalar_one_or_none()
- async def list_organizations(
- db: AsyncSession,
- skip: int = 0,
- limit: int = 100,
- status: str | None = None,
- ) -> tuple[list[Organization], int]:
- """
- List organizations with pagination.
- Args:
- db: Database session
- skip: Number of records to skip
- limit: Maximum number of records to return
- status: Filter by status (optional)
- Returns:
- Tuple of (organizations list, total count)
- """
- # Build query
- query = select(Organization)
- if status:
- query = query.where(Organization.status == status)
- # Get total count
- count_query = select(func.count()).select_from(Organization)
- if status:
- count_query = count_query.where(Organization.status == status)
- total_result = await db.execute(count_query)
- total = total_result.scalar_one()
- # Get paginated results
- query = query.offset(skip).limit(limit).order_by(Organization.created_at.desc())
- result = await db.execute(query)
- organizations = list(result.scalars().all())
- return organizations, total
- async def update_organization(
- db: AsyncSession,
- organization_id: int,
- data: OrganizationUpdate,
- ) -> Organization | None:
- """
- Update organization.
- Args:
- db: Database session
- organization_id: Organization ID
- data: Update data
- Returns:
- Updated organization or None if not found
- """
- result = await db.execute(
- select(Organization).where(Organization.id == organization_id)
- )
- org = result.scalar_one_or_none()
- if not org:
- return None
- # Update fields
- update_data = data.model_dump(exclude_unset=True)
- for field, value in update_data.items():
- setattr(org, field, value)
- await db.commit()
- await db.refresh(org)
- return org
- async def delete_organization(
- db: AsyncSession,
- organization_id: int,
- ) -> bool:
- """
- Delete organization.
- Args:
- db: Database session
- organization_id: Organization ID
- Returns:
- True if deleted, False if not found
- """
- result = await db.execute(
- select(Organization).where(Organization.id == organization_id)
- )
- org = result.scalar_one_or_none()
- if not org:
- return False
- await db.delete(org)
- await db.commit()
- return True
|