(userId: string)
| 290 | * In non-production environments, returns true for convenience. |
| 291 | */ |
| 292 | export async function isEnterpriseOrgAdminOrOwner(userId: string): Promise<boolean> { |
| 293 | try { |
| 294 | if (!isBillingEnabled) { |
| 295 | return true |
| 296 | } |
| 297 | |
| 298 | const [memberRecord] = await db |
| 299 | .select({ |
| 300 | organizationId: member.organizationId, |
| 301 | role: member.role, |
| 302 | }) |
| 303 | .from(member) |
| 304 | .where(eq(member.userId, userId)) |
| 305 | .limit(1) |
| 306 | |
| 307 | if (!memberRecord) { |
| 308 | return false |
| 309 | } |
| 310 | |
| 311 | if (memberRecord.role !== 'owner' && memberRecord.role !== 'admin') { |
| 312 | return false |
| 313 | } |
| 314 | |
| 315 | const billingStatus = await getEffectiveBillingStatus(userId) |
| 316 | if (billingStatus.billingBlocked) { |
| 317 | return false |
| 318 | } |
| 319 | |
| 320 | const orgSub = await getOrganizationSubscriptionUsable(memberRecord.organizationId) |
| 321 | |
| 322 | const isEnterprise = orgSub && checkEnterprisePlan(orgSub) |
| 323 | |
| 324 | if (isEnterprise) { |
| 325 | logger.info('User is enterprise org admin/owner', { |
| 326 | userId, |
| 327 | organizationId: memberRecord.organizationId, |
| 328 | role: memberRecord.role, |
| 329 | }) |
| 330 | } |
| 331 | |
| 332 | return !!isEnterprise |
| 333 | } catch (error) { |
| 334 | logger.error('Error checking enterprise org admin/owner status', { error, userId }) |
| 335 | return false |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Check if user is an admin or owner of a team or enterprise organization |
no test coverage detected