(workosUserId: string)
| 203 | * Returns null if the user has no memberships. |
| 204 | */ |
| 205 | export async function resolvePreferredOrganization(workosUserId: string): Promise<string | null> { |
| 206 | const result = await query<{ workos_organization_id: string }>( |
| 207 | `SELECT om.workos_organization_id |
| 208 | FROM organization_memberships om |
| 209 | JOIN organizations o ON o.workos_organization_id = om.workos_organization_id |
| 210 | WHERE om.workos_user_id = $1 |
| 211 | ORDER BY |
| 212 | CASE WHEN o.subscription_status = 'active' THEN 0 ELSE 1 END, |
| 213 | om.created_at DESC |
| 214 | LIMIT 1`, |
| 215 | [workosUserId] |
| 216 | ); |
| 217 | return result.rows[0]?.workos_organization_id ?? null; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Backfill primary_organization_id for a user if not already set. |
no outgoing calls
no test coverage detected