(userId: string)
| 345 | * In non-production environments, returns true for convenience. |
| 346 | */ |
| 347 | export async function isTeamOrgAdminOrOwner(userId: string): Promise<boolean> { |
| 348 | try { |
| 349 | if (!isBillingEnabled) { |
| 350 | return true |
| 351 | } |
| 352 | |
| 353 | const [memberRecord] = await db |
| 354 | .select({ |
| 355 | organizationId: member.organizationId, |
| 356 | role: member.role, |
| 357 | }) |
| 358 | .from(member) |
| 359 | .where(eq(member.userId, userId)) |
| 360 | .limit(1) |
| 361 | |
| 362 | if (!memberRecord) { |
| 363 | return false |
| 364 | } |
| 365 | |
| 366 | if (memberRecord.role !== 'owner' && memberRecord.role !== 'admin') { |
| 367 | return false |
| 368 | } |
| 369 | |
| 370 | const billingStatus = await getEffectiveBillingStatus(userId) |
| 371 | if (billingStatus.billingBlocked) { |
| 372 | return false |
| 373 | } |
| 374 | |
| 375 | const orgSub = await getOrganizationSubscriptionUsable(memberRecord.organizationId) |
| 376 | |
| 377 | const hasTeamPlan = orgSub && (checkTeamPlan(orgSub) || checkEnterprisePlan(orgSub)) |
| 378 | |
| 379 | if (hasTeamPlan) { |
| 380 | logger.info('User is team org admin/owner', { |
| 381 | userId, |
| 382 | organizationId: memberRecord.organizationId, |
| 383 | role: memberRecord.role, |
| 384 | plan: orgSub.plan, |
| 385 | }) |
| 386 | } |
| 387 | |
| 388 | return !!hasTeamPlan |
| 389 | } catch (error) { |
| 390 | logger.error('Error checking team org admin/owner status', { error, userId }) |
| 391 | return false |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Check if an organization has team or enterprise plan |
no test coverage detected