( organizationId: string, reason: BillingBlockReason )
| 88 | * A payment_failed block won't overwrite an existing dispute block |
| 89 | */ |
| 90 | export async function blockOrgMembers( |
| 91 | organizationId: string, |
| 92 | reason: BillingBlockReason |
| 93 | ): Promise<number> { |
| 94 | const memberIds = await getOrgMemberIds(organizationId) |
| 95 | |
| 96 | if (memberIds.length === 0) { |
| 97 | return 0 |
| 98 | } |
| 99 | |
| 100 | // Don't overwrite dispute blocks with payment_failed (dispute is higher priority) |
| 101 | const whereClause = |
| 102 | reason === 'payment_failed' |
| 103 | ? and( |
| 104 | inArray(userStats.userId, memberIds), |
| 105 | or(ne(userStats.billingBlockedReason, 'dispute'), isNull(userStats.billingBlockedReason)) |
| 106 | ) |
| 107 | : inArray(userStats.userId, memberIds) |
| 108 | |
| 109 | const result = await db |
| 110 | .update(userStats) |
| 111 | .set({ billingBlocked: true, billingBlockedReason: reason }) |
| 112 | .where(whereClause) |
| 113 | .returning({ userId: userStats.userId }) |
| 114 | |
| 115 | return result.length |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Unblock all members of an organization blocked for a specific reason |
no test coverage detected