(
graphAPI: GraphApi,
authentication: AuthenticationContext,
{
entityTypeIds,
propertyPatches,
additionalAllowedPropertyBaseUrls,
...params
}: PatchEntityParameters,
/**
* @todo H-3091: returning a specific 'this' will not be correct if the entityTypeId has been changed as part of
* the update. I tried using generics to enforce that a new EntityProperties must be provided if the entityTypeId
* is changed, but it isn't causing a compiler error:
*
* public async patch<NewPropertyMap extends EntityProperties = PropertyMap>(
* // PatchEntityParams sets a specific VersionedUrl based on NewPropertyMap, but this is not enforced by the
* compiler params: PatchEntityParameters<NewPropertyMap>,
* )
*/
)
| 1175 | } |
| 1176 | |
| 1177 | public async patch( |
| 1178 | graphAPI: GraphApi, |
| 1179 | authentication: AuthenticationContext, |
| 1180 | { |
| 1181 | entityTypeIds, |
| 1182 | propertyPatches, |
| 1183 | additionalAllowedPropertyBaseUrls, |
| 1184 | ...params |
| 1185 | }: PatchEntityParameters, |
| 1186 | /** |
| 1187 | * @todo H-3091: returning a specific 'this' will not be correct if the entityTypeId has been changed as part of |
| 1188 | * the update. I tried using generics to enforce that a new EntityProperties must be provided if the entityTypeId |
| 1189 | * is changed, but it isn't causing a compiler error: |
| 1190 | * |
| 1191 | * public async patch<NewPropertyMap extends EntityProperties = PropertyMap>( |
| 1192 | * // PatchEntityParams sets a specific VersionedUrl based on NewPropertyMap, but this is not enforced by the |
| 1193 | * compiler params: PatchEntityParameters<NewPropertyMap>, |
| 1194 | * ) |
| 1195 | */ |
| 1196 | ): Promise<this> { |
| 1197 | if (propertyPatches) { |
| 1198 | const isUserEntity = this.metadata.entityTypeIds.some( |
| 1199 | (id) => extractBaseUrl(id) === userEntityTypeBaseUrl, |
| 1200 | ); |
| 1201 | |
| 1202 | if (isUserEntity) { |
| 1203 | for (const patch of propertyPatches) { |
| 1204 | const targetBaseUrl = patch.path[0] as BaseUrl | undefined; |
| 1205 | if (targetBaseUrl === undefined) { |
| 1206 | throw new Error( |
| 1207 | "Cannot replace the entire property object on a user entity", |
| 1208 | ); |
| 1209 | } |
| 1210 | |
| 1211 | if ( |
| 1212 | !userSelfUpdatablePropertyBaseUrls.has(targetBaseUrl) && |
| 1213 | !additionalAllowedPropertyBaseUrls?.has(targetBaseUrl) |
| 1214 | ) { |
| 1215 | throw new Error( |
| 1216 | `Property patch targeting '${targetBaseUrl}' is not allowed on a user entity. Allowed properties: ${[...userSelfUpdatablePropertyBaseUrls, ...(additionalAllowedPropertyBaseUrls ?? [])].join(", ")}`, |
| 1217 | ); |
| 1218 | } |
| 1219 | |
| 1220 | if (targetBaseUrl === displayNamePropertyBaseUrl) { |
| 1221 | if (patch.op === "remove") { |
| 1222 | throw new Error("Cannot remove the display name of a user"); |
| 1223 | } |
| 1224 | |
| 1225 | const rawName = patch.property.value; |
| 1226 | if (typeof rawName === "string") { |
| 1227 | const trimmedName = rawName.trim(); |
| 1228 | const validation = validateDisplayName(trimmedName); |
| 1229 | if (validation !== true) { |
| 1230 | throw new Error(validation); |
| 1231 | } |
| 1232 | if (trimmedName !== rawName) { |
| 1233 | patch.property.value = trimmedName; |
| 1234 | } |
no test coverage detected