(params: {
orgMembershipLinks?: LinkEntity[];
subgraph: Subgraph<EntityRootType<HashEntity>>;
resolvedOrgs?: Org[];
userEntity: Entity<UserEntity>;
verifiableAddresses?: VerifiableIdentityAddress[];
})
| 362 | * @param params.orgMembershipLinks provides a minor optimization to avoid looking up membership links if they are already known |
| 363 | */ |
| 364 | export const constructUser = (params: { |
| 365 | orgMembershipLinks?: LinkEntity[]; |
| 366 | subgraph: Subgraph<EntityRootType<HashEntity>>; |
| 367 | resolvedOrgs?: Org[]; |
| 368 | userEntity: Entity<UserEntity>; |
| 369 | verifiableAddresses?: VerifiableIdentityAddress[]; |
| 370 | }): User => { |
| 371 | const { orgMembershipLinks, resolvedOrgs, subgraph, userEntity } = params; |
| 372 | |
| 373 | const { email } = simplifyProperties(userEntity.properties); |
| 374 | |
| 375 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- permissions means this may be undefined. @todo types to account for property-level permissions |
| 376 | const primaryEmailAddress = email?.[0] ?? ""; |
| 377 | |
| 378 | const normalizedPrimaryEmail = normalizeEmail(primaryEmailAddress); |
| 379 | |
| 380 | const isPrimaryEmailAddressVerified = |
| 381 | params.verifiableAddresses?.find( |
| 382 | ({ value }) => normalizeEmail(value) === normalizedPrimaryEmail, |
| 383 | )?.verified === true; |
| 384 | |
| 385 | const minimalUser = constructMinimalUser({ userEntity }); |
| 386 | |
| 387 | const orgMemberships = |
| 388 | orgMembershipLinks ?? |
| 389 | getOutgoingLinksForEntity( |
| 390 | subgraph, |
| 391 | userEntity.metadata.recordId.entityId, |
| 392 | intervalForTimestamp(currentTimestamp()), |
| 393 | ).filter((linkEntity) => |
| 394 | linkEntity.metadata.entityTypeIds.includes( |
| 395 | systemLinkEntityTypes.isMemberOf.linkEntityTypeId, |
| 396 | ), |
| 397 | ); |
| 398 | |
| 399 | const memberOf = orgMemberships.map((untypedLinkEntity) => { |
| 400 | const linkEntity = untypedLinkEntity as LinkEntity<IsMemberOf>; |
| 401 | |
| 402 | const { linkData, metadata } = linkEntity; |
| 403 | |
| 404 | const fullyResolvedOrg = resolvedOrgs?.find( |
| 405 | (org) => org.entity.metadata.recordId.entityId === linkData.rightEntityId, |
| 406 | ); |
| 407 | if (fullyResolvedOrg) { |
| 408 | return { |
| 409 | linkEntity, |
| 410 | org: fullyResolvedOrg, |
| 411 | }; |
| 412 | } |
| 413 | |
| 414 | const orgEntityRevisions = getRightEntityForLinkEntity( |
| 415 | subgraph, |
| 416 | metadata.recordId.entityId, |
| 417 | intervalForTimestamp(currentTimestamp()), |
| 418 | ); |
| 419 | |
| 420 | if (!orgEntityRevisions || orgEntityRevisions.length === 0) { |
| 421 | throw new Error( |
no test coverage detected