|
55 | 55 | from oss.src.services.exceptions import UnauthorizedException |
56 | 56 | from oss.src.services.db_manager import ( |
57 | 57 | get_user_with_email, |
58 | | - check_if_user_exists_and_create_organization, |
59 | 58 | check_if_user_invitation_exists, |
| 59 | + is_first_user_signup, |
| 60 | + get_oss_organization, |
| 61 | + setup_oss_organization_for_first_user, |
60 | 62 | ) |
61 | 63 |
|
62 | 64 | log = get_module_logger(__name__) |
@@ -236,28 +238,53 @@ async def _create_account(email: str, uid: str) -> bool: |
236 | 238 | "email": email, |
237 | 239 | } |
238 | 240 |
|
239 | | - # For OSS: compute organization before calling create_accounts |
240 | 241 | # For EE: organization is created inside create_accounts |
| 242 | + # For OSS: we need to handle first user specially to avoid FK violation |
241 | 243 | if is_ee(): |
242 | 244 | await create_accounts(payload) |
243 | 245 | else: |
244 | | - # OSS: Compute or get the single organization |
245 | | - organization_db = await check_if_user_exists_and_create_organization( |
246 | | - user_email=email |
247 | | - ) |
| 246 | + # OSS: Check if this is the first user signup |
| 247 | + first_user = await is_first_user_signup() |
| 248 | + |
| 249 | + if first_user: |
| 250 | + # First user: Create user first, then organization |
| 251 | + # This avoids the FK violation where org.owner_id references non-existent user |
| 252 | + user_db = await create_accounts(payload) |
| 253 | + |
| 254 | + # Now create organization with the real user ID |
| 255 | + organization_db = await setup_oss_organization_for_first_user( |
| 256 | + user_id=user_db.id, |
| 257 | + user_email=email, |
| 258 | + ) |
248 | 259 |
|
249 | | - # Verify user can join (invitation check) |
250 | | - user_invitation_exists = await check_if_user_invitation_exists( |
251 | | - email=email, |
252 | | - organization_id=str(organization_db.id), |
253 | | - ) |
254 | | - if not user_invitation_exists: |
255 | | - raise UnauthorizedException( |
256 | | - detail="You need to be invited by the organization owner to gain access." |
| 260 | + # Assign user to organization |
| 261 | + from oss.src.services.db_manager import _assign_user_to_organization_oss |
| 262 | + |
| 263 | + await _assign_user_to_organization_oss( |
| 264 | + user_db=user_db, |
| 265 | + organization_id=str(organization_db.id), |
| 266 | + email=email, |
257 | 267 | ) |
| 268 | + else: |
| 269 | + # Not first user: Get existing organization and check invitation |
| 270 | + organization_db = await get_oss_organization() |
| 271 | + if not organization_db: |
| 272 | + raise UnauthorizedException( |
| 273 | + detail="No organization found. Please contact the administrator." |
| 274 | + ) |
258 | 275 |
|
259 | | - payload["organization_id"] = str(organization_db.id) |
260 | | - await create_accounts(payload) |
| 276 | + # Verify user can join (invitation check) |
| 277 | + user_invitation_exists = await check_if_user_invitation_exists( |
| 278 | + email=email, |
| 279 | + organization_id=str(organization_db.id), |
| 280 | + ) |
| 281 | + if not user_invitation_exists: |
| 282 | + raise UnauthorizedException( |
| 283 | + detail="You need to be invited by the organization owner to gain access." |
| 284 | + ) |
| 285 | + |
| 286 | + payload["organization_id"] = str(organization_db.id) |
| 287 | + await create_accounts(payload) |
261 | 288 |
|
262 | 289 | if env.posthog.enabled and env.posthog.api_key: |
263 | 290 | try: |
|
0 commit comments