organization_service.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """
  2. Organization management service.
  3. """
  4. from sqlalchemy import func, select
  5. from sqlalchemy.ext.asyncio import AsyncSession
  6. from app.models.organization import Organization
  7. from app.schemas.organization import OrganizationCreate, OrganizationUpdate
  8. async def create_organization(
  9. db: AsyncSession,
  10. data: OrganizationCreate,
  11. ) -> Organization:
  12. """
  13. Create a new organization.
  14. Args:
  15. db: Database session
  16. data: Organization creation data
  17. Returns:
  18. Created organization
  19. """
  20. org = Organization(
  21. name=data.name,
  22. contact_email=data.contact_email,
  23. contact_phone=data.contact_phone,
  24. wifi_enabled=data.wifi_enabled,
  25. ble_enabled=data.ble_enabled,
  26. status="active", # Superadmin creates active orgs
  27. )
  28. db.add(org)
  29. await db.commit()
  30. await db.refresh(org)
  31. return org
  32. async def get_organization(db: AsyncSession, organization_id: int) -> Organization | None:
  33. """
  34. Get organization by ID.
  35. Args:
  36. db: Database session
  37. organization_id: Organization ID
  38. Returns:
  39. Organization or None
  40. """
  41. result = await db.execute(
  42. select(Organization).where(Organization.id == organization_id)
  43. )
  44. return result.scalar_one_or_none()
  45. async def list_organizations(
  46. db: AsyncSession,
  47. skip: int = 0,
  48. limit: int = 100,
  49. status: str | None = None,
  50. ) -> tuple[list[Organization], int]:
  51. """
  52. List organizations with pagination.
  53. Args:
  54. db: Database session
  55. skip: Number of records to skip
  56. limit: Maximum number of records to return
  57. status: Filter by status (optional)
  58. Returns:
  59. Tuple of (organizations list, total count)
  60. """
  61. # Build query
  62. query = select(Organization)
  63. if status:
  64. query = query.where(Organization.status == status)
  65. # Get total count
  66. count_query = select(func.count()).select_from(Organization)
  67. if status:
  68. count_query = count_query.where(Organization.status == status)
  69. total_result = await db.execute(count_query)
  70. total = total_result.scalar_one()
  71. # Get paginated results
  72. query = query.offset(skip).limit(limit).order_by(Organization.created_at.desc())
  73. result = await db.execute(query)
  74. organizations = list(result.scalars().all())
  75. return organizations, total
  76. async def update_organization(
  77. db: AsyncSession,
  78. organization_id: int,
  79. data: OrganizationUpdate,
  80. ) -> Organization | None:
  81. """
  82. Update organization.
  83. Args:
  84. db: Database session
  85. organization_id: Organization ID
  86. data: Update data
  87. Returns:
  88. Updated organization or None if not found
  89. """
  90. result = await db.execute(
  91. select(Organization).where(Organization.id == organization_id)
  92. )
  93. org = result.scalar_one_or_none()
  94. if not org:
  95. return None
  96. # Update fields
  97. update_data = data.model_dump(exclude_unset=True)
  98. for field, value in update_data.items():
  99. setattr(org, field, value)
  100. await db.commit()
  101. await db.refresh(org)
  102. return org
  103. async def delete_organization(
  104. db: AsyncSession,
  105. organization_id: int,
  106. ) -> bool:
  107. """
  108. Delete organization.
  109. Args:
  110. db: Database session
  111. organization_id: Organization ID
  112. Returns:
  113. True if deleted, False if not found
  114. """
  115. result = await db.execute(
  116. select(Organization).where(Organization.id == organization_id)
  117. )
  118. org = result.scalar_one_or_none()
  119. if not org:
  120. return False
  121. await db.delete(org)
  122. await db.commit()
  123. return True