""" 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