Get an organization by ID optimized for detail view. Only loads users (needed for detail view), not projects or invites.
(cls, orm: orm.Session, org_id: str | UUID)
| 490 | |
| 491 | @classmethod |
| 492 | def get_by_id_for_detail(cls, orm: orm.Session, org_id: str | UUID) -> Optional['OrgModel']: |
| 493 | """ |
| 494 | Get an organization by ID optimized for detail view. |
| 495 | Only loads users (needed for detail view), not projects or invites. |
| 496 | """ |
| 497 | |
| 498 | org = ( |
| 499 | orm.query(cls) |
| 500 | .options( |
| 501 | joinedload(cls.users), |
| 502 | # Use lazyload to prevent automatic loading but avoid errors |
| 503 | lazyload(cls.projects), |
| 504 | lazyload(cls.invites), |
| 505 | ) |
| 506 | .filter(cls.id == normalize_uuid(org_id)) |
| 507 | .first() |
| 508 | ) |
| 509 | |
| 510 | if org: |
| 511 | # Calculate counts without loading all data |
| 512 | from sqlalchemy import func |
| 513 | |
| 514 | invite_count = ( |
| 515 | orm.query(func.count(OrgInviteModel.invitee_email)) |
| 516 | .filter(OrgInviteModel.org_id == org.id) |
| 517 | .scalar() |
| 518 | ) |
| 519 | project_count = ( |
| 520 | orm.query(func.count(ProjectModel.id)).filter(ProjectModel.org_id == org.id).scalar() |
| 521 | ) |
| 522 | |
| 523 | org._member_count = len(org.users) + invite_count |
| 524 | org._user_count = len(org.users) # Store user count separately for billing |
| 525 | org._invite_count = invite_count # Store invite count separately |
| 526 | org._project_count = project_count |
| 527 | |
| 528 | return org |
| 529 | |
| 530 | @classmethod |
| 531 | def get_by_id_for_permission_check( |
no test coverage detected