( ...args: Parameters<UpdateEntityFunction<Properties>> )
| 469 | * Update an entity. |
| 470 | */ |
| 471 | export const updateEntity = async < |
| 472 | Properties extends TypeIdsAndPropertiesForEntity, |
| 473 | >( |
| 474 | ...args: Parameters<UpdateEntityFunction<Properties>> |
| 475 | ): ReturnType<UpdateEntityFunction<Properties>> => { |
| 476 | const [context, authentication, params] = args; |
| 477 | const { entity, entityTypeIds, propertyPatches } = params; |
| 478 | |
| 479 | for (const beforeUpdateHook of beforeUpdateEntityHooks) { |
| 480 | if (entity.metadata.entityTypeIds.includes(beforeUpdateHook.entityTypeId)) { |
| 481 | await beforeUpdateHook.callback({ |
| 482 | context, |
| 483 | previousEntity: entity, |
| 484 | propertyPatches: propertyPatches ?? [], |
| 485 | authentication, |
| 486 | }); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | const { graphApi } = context; |
| 491 | const { actorId } = authentication; |
| 492 | |
| 493 | /** |
| 494 | * The SDK's patch method auto-enforces the base user property whitelist. |
| 495 | * Here we extend it with properties that have special authorization |
| 496 | * (validated by the before-update hook which runs above). |
| 497 | * |
| 498 | * - enabledFeatureFlags: the hook checks admin privileges |
| 499 | * - shortname: the hook allows it only for incomplete users (first-time signup) |
| 500 | */ |
| 501 | const additionalAllowedUrls = new Set([enabledFeatureFlagsPropertyBaseUrl]); |
| 502 | |
| 503 | const { shortname } = simplifyProperties<UserProperties>( |
| 504 | entity.properties as UserProperties, |
| 505 | ); |
| 506 | if (!shortname) { |
| 507 | additionalAllowedUrls.add(shortnamePropertyBaseUrl); |
| 508 | } |
| 509 | |
| 510 | const updatedEntity = await entity.patch( |
| 511 | graphApi, |
| 512 | { actorId }, |
| 513 | { |
| 514 | entityTypeIds, |
| 515 | draft: params.draft, |
| 516 | propertyPatches, |
| 517 | provenance: context.provenance, |
| 518 | archived: params.archived, |
| 519 | additionalAllowedPropertyBaseUrls: additionalAllowedUrls, |
| 520 | }, |
| 521 | ); |
| 522 | |
| 523 | for (const afterUpdateHook of afterUpdateEntityHooks) { |
| 524 | if (entity.metadata.entityTypeIds.includes(afterUpdateHook.entityTypeId)) { |
| 525 | void afterUpdateHook.callback({ |
| 526 | context, |
| 527 | previousEntity: entity, |
| 528 | propertyPatches: propertyPatches ?? [], |
no test coverage detected