(
organizationId: string,
sub: {
plan: string | null
seats: number | null
periodStart: Date | null
periodEnd: Date | null
},
currentUsage: number,
preloadedMemberIds?: string[]
)
| 174 | } |
| 175 | |
| 176 | async function applyOrgRefresh( |
| 177 | organizationId: string, |
| 178 | sub: { |
| 179 | plan: string | null |
| 180 | seats: number | null |
| 181 | periodStart: Date | null |
| 182 | periodEnd: Date | null |
| 183 | }, |
| 184 | currentUsage: number, |
| 185 | preloadedMemberIds?: string[] |
| 186 | ): Promise<number> { |
| 187 | if (!isPaid(sub.plan) || !sub.periodStart) { |
| 188 | return currentUsage |
| 189 | } |
| 190 | |
| 191 | const memberIds = |
| 192 | preloadedMemberIds ?? (await getPooledOrgCurrentPeriodCost(organizationId)).memberIds |
| 193 | if (memberIds.length === 0) return currentUsage |
| 194 | |
| 195 | const planDollars = getPlanTierDollars(sub.plan) |
| 196 | if (planDollars <= 0) return currentUsage |
| 197 | |
| 198 | const userBounds = await getOrgMemberRefreshBounds(organizationId, sub.periodStart) |
| 199 | const refresh = await computeDailyRefreshConsumed({ |
| 200 | userIds: memberIds, |
| 201 | periodStart: sub.periodStart, |
| 202 | periodEnd: sub.periodEnd ?? null, |
| 203 | planDollars, |
| 204 | seats: sub.seats || 1, |
| 205 | userBounds: Object.keys(userBounds).length > 0 ? userBounds : undefined, |
| 206 | billingEntity: { type: 'organization', id: organizationId }, |
| 207 | }) |
| 208 | |
| 209 | return Math.max(0, currentUsage - refresh) |
| 210 | } |
| 211 | |
| 212 | function buildUsageData(params: { |
| 213 | currentUsage: number |
no test coverage detected