| 152 | */ |
| 153 | const identityDirectory = |
| 154 | (organizationId: string, context: Context.Context<WorkOSClient>): AdminIdentityDirectory => |
| 155 | (externalIds) => |
| 156 | Effect.gen(function* () { |
| 157 | const workos = yield* WorkOSClient; |
| 158 | const memberships = yield* workos.listOrgMembers(organizationId); |
| 159 | const wanted = new Set(externalIds); |
| 160 | const memberIds = memberships.data |
| 161 | .map((membership) => membership.userId) |
| 162 | .filter((userId) => wanted.has(userId)); |
| 163 | |
| 164 | const resolved = yield* Effect.all( |
| 165 | memberIds.map((userId) => |
| 166 | workos.getUser(userId).pipe( |
| 167 | Effect.map( |
| 168 | (user) => |
| 169 | [ |
| 170 | userId, |
| 171 | { |
| 172 | email: user.email, |
| 173 | displayName: [user.firstName, user.lastName].filter(Boolean).join(" ") || null, |
| 174 | }, |
| 175 | ] as const, |
| 176 | ), |
| 177 | // One unreadable user must not cost the whole page its names. |
| 178 | Effect.catchCause(() => Effect.succeed(null)), |
| 179 | ), |
| 180 | ), |
| 181 | { concurrency: IDENTITY_CONCURRENCY }, |
| 182 | ); |
| 183 | |
| 184 | const identities = new Map<string, AdminUserIdentity>(); |
| 185 | for (const entry of resolved) if (entry) identities.set(entry[0], entry[1]); |
| 186 | return identities; |
| 187 | }).pipe(Effect.provideContext(context)); |
| 188 | |
| 189 | /** |
| 190 | * Cloud's REVERSE directory lookup: email → the WorkOS `user_...` id. |