(userId: string)
| 16 | * If the user belongs to an organization, also checks whether the org owner is blocked. |
| 17 | */ |
| 18 | export async function getEffectiveBillingStatus(userId: string): Promise<EffectiveBillingStatus> { |
| 19 | const userStatsRows = await db |
| 20 | .select({ |
| 21 | blocked: userStats.billingBlocked, |
| 22 | blockedReason: userStats.billingBlockedReason, |
| 23 | }) |
| 24 | .from(userStats) |
| 25 | .where(eq(userStats.userId, userId)) |
| 26 | .limit(1) |
| 27 | |
| 28 | const userBlocked = userStatsRows.length > 0 ? !!userStatsRows[0].blocked : false |
| 29 | const userBlockedReason = userStatsRows.length > 0 ? userStatsRows[0].blockedReason : null |
| 30 | |
| 31 | if (userBlocked) { |
| 32 | return { |
| 33 | billingBlocked: true, |
| 34 | billingBlockedReason: userBlockedReason, |
| 35 | blockedByOrgOwner: false, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const memberships = await db |
| 40 | .select({ organizationId: member.organizationId }) |
| 41 | .from(member) |
| 42 | .where(eq(member.userId, userId)) |
| 43 | |
| 44 | const ownerResults = await Promise.all( |
| 45 | memberships.map((m) => |
| 46 | db |
| 47 | .select({ userId: member.userId }) |
| 48 | .from(member) |
| 49 | .where(and(eq(member.organizationId, m.organizationId), eq(member.role, 'owner'))) |
| 50 | .limit(1) |
| 51 | ) |
| 52 | ) |
| 53 | |
| 54 | const otherOwnerIds = ownerResults |
| 55 | .filter((owners) => owners.length > 0 && owners[0].userId !== userId) |
| 56 | .map((owners) => owners[0].userId) |
| 57 | |
| 58 | if (otherOwnerIds.length > 0) { |
| 59 | const ownerStatsResults = await Promise.all( |
| 60 | otherOwnerIds.map((ownerId) => |
| 61 | db |
| 62 | .select({ |
| 63 | blocked: userStats.billingBlocked, |
| 64 | blockedReason: userStats.billingBlockedReason, |
| 65 | }) |
| 66 | .from(userStats) |
| 67 | .where(eq(userStats.userId, ownerId)) |
| 68 | .limit(1) |
| 69 | ) |
| 70 | ) |
| 71 | |
| 72 | for (const stats of ownerStatsResults) { |
| 73 | if (stats.length > 0 && stats[0].blocked) { |
| 74 | return { |
| 75 | billingBlocked: true, |
no test coverage detected