( organizationId: string, executor: DbClient = db )
| 60 | * downstream refresh / bounds computations. |
| 61 | */ |
| 62 | export async function getPooledOrgCurrentPeriodCost( |
| 63 | organizationId: string, |
| 64 | executor: DbClient = db |
| 65 | ): Promise<{ memberIds: string[]; currentPeriodCost: number; lastPeriodCost: number }> { |
| 66 | const rows = await executor |
| 67 | .select({ |
| 68 | userId: member.userId, |
| 69 | currentPeriodCost: userStats.currentPeriodCost, |
| 70 | lastPeriodCost: userStats.lastPeriodCost, |
| 71 | }) |
| 72 | .from(member) |
| 73 | .leftJoin(userStats, eq(member.userId, userStats.userId)) |
| 74 | .where(eq(member.organizationId, organizationId)) |
| 75 | |
| 76 | let pooled = new Decimal(0) |
| 77 | let lastPeriodCost = new Decimal(0) |
| 78 | const memberIds: string[] = [] |
| 79 | for (const row of rows) { |
| 80 | memberIds.push(row.userId) |
| 81 | pooled = pooled.plus(toDecimal(row.currentPeriodCost)) |
| 82 | lastPeriodCost = lastPeriodCost.plus(toDecimal(row.lastPeriodCost)) |
| 83 | } |
| 84 | |
| 85 | return { |
| 86 | memberIds, |
| 87 | currentPeriodCost: toNumber(pooled), |
| 88 | lastPeriodCost: toNumber(lastPeriodCost), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Calculates the effective usage limit for an organization-scoped plan. |
no test coverage detected