@@ -6,10 +6,10 @@ import {
66  officersGroupTestingId , 
77}  from  "../../common/config.js" ; 
88import  { 
9-   BaseError , 
109  EntraFetchError , 
1110  EntraGroupError , 
1211  EntraInvitationError , 
12+   EntraPatchError , 
1313  InternalServerError , 
1414}  from  "../../common/errors/index.js" ; 
1515import  {  getSecretValue  }  from  "../plugins/auth.js" ; 
@@ -18,8 +18,8 @@ import { getItemFromCache, insertItemIntoCache } from "./cache.js";
1818import  { 
1919  EntraGroupActions , 
2020  EntraInvitationResponse , 
21+   ProfilePatchRequest , 
2122}  from  "../../common/types/iam.js" ; 
22- import  {  FastifyInstance  }  from  "fastify" ; 
2323import  {  UserProfileDataBase  }  from  "common/types/msGraphApi.js" ; 
2424import  {  SecretsManagerClient  }  from  "@aws-sdk/client-secrets-manager" ; 
2525import  {  DynamoDBClient  }  from  "@aws-sdk/client-dynamodb" ; 
@@ -397,3 +397,49 @@ export async function getUserProfile(
397397    } ) ; 
398398  } 
399399} 
400+ 
401+ /** 
402+  * Patches the profile of a user from Entra ID. 
403+  * @param  token - Entra ID token authorized to perform this action. 
404+  * @param  userId - The user ID to patch the profile for. 
405+  * @throws  {EntraUserError } If setting the user profile fails. 
406+  * @returns  {Promise<void> } nothing 
407+  */ 
408+ export  async  function  patchUserProfile ( 
409+   token : string , 
410+   email : string , 
411+   userId : string , 
412+   data : ProfilePatchRequest , 
413+ ) : Promise < void >  { 
414+   try  { 
415+     const  url  =  `https://graph.microsoft.com/v1.0/users/${ userId }  ` ; 
416+     const  response  =  await  fetch ( url ,  { 
417+       method : "PATCH" , 
418+       headers : { 
419+         Authorization : `Bearer ${ token }  ` , 
420+         "Content-Type" : "application/json" , 
421+       } , 
422+       body : JSON . stringify ( data ) , 
423+     } ) ; 
424+ 
425+     if  ( ! response . ok )  { 
426+       const  errorData  =  ( await  response . json ( ) )  as  { 
427+         error ?: {  message ?: string  } ; 
428+       } ; 
429+       throw  new  EntraPatchError ( { 
430+         message : errorData ?. error ?. message  ??  response . statusText , 
431+         email, 
432+       } ) ; 
433+     } 
434+     return ; 
435+   }  catch  ( error )  { 
436+     if  ( error  instanceof  EntraPatchError )  { 
437+       throw  error ; 
438+     } 
439+ 
440+     throw  new  EntraPatchError ( { 
441+       message : error  instanceof  Error  ? error . message  : String ( error ) , 
442+       email, 
443+     } ) ; 
444+   } 
445+ } 
0 commit comments