( entityType: 'user' | 'organization', entityId: string, executor: DbClient = db )
| 28 | * org owner as a user-id proxy. |
| 29 | */ |
| 30 | export async function getCreditBalanceForEntity( |
| 31 | entityType: 'user' | 'organization', |
| 32 | entityId: string, |
| 33 | executor: DbClient = db |
| 34 | ): Promise<number> { |
| 35 | if (entityType === 'organization') { |
| 36 | const rows = await executor |
| 37 | .select({ creditBalance: organization.creditBalance }) |
| 38 | .from(organization) |
| 39 | .where(eq(organization.id, entityId)) |
| 40 | .limit(1) |
| 41 | return rows.length > 0 ? toNumber(toDecimal(rows[0].creditBalance)) : 0 |
| 42 | } |
| 43 | |
| 44 | const rows = await executor |
| 45 | .select({ creditBalance: userStats.creditBalance }) |
| 46 | .from(userStats) |
| 47 | .where(eq(userStats.userId, entityId)) |
| 48 | .limit(1) |
| 49 | return rows.length > 0 ? toNumber(toDecimal(rows[0].creditBalance)) : 0 |
| 50 | } |
| 51 | |
| 52 | export async function getCreditBalance( |
| 53 | userId: string, |
no test coverage detected