public async Task <ActionResult> CreatePhsaParty(PhsaChangeModel changeModel) { if (changeModel == null) { ModelState.AddModelError("Party", "Could not create the Party, the passed in model cannot be null."); return(BadRequest(ApiResponse.BadRequest(ModelState))); } var validPartyTypes = await _partyService.GetPreApprovedRegistrationsAsync(firstName : User.GetFirstName(), lastName : User.GetLastName(), email : changeModel.Email); if (!changeModel.Validate(validPartyTypes)) { ModelState.AddModelError("Party", "Validation failed: Email and Phone Number are required, and at least one Pre-Approved PHSA Party Type must be specified."); return(BadRequest(ApiResponse.BadRequest(ModelState))); } if (await _partyService.CreateOrUpdatePartyAsync(changeModel, User) == -1) { return(StatusCode(StatusCodes.Status500InternalServerError, new { message = "Error when saving the Party." })); } if (!await _keycloakClient.UpdatePhsaUserInfo(User.GetPrimeUserId(), changeModel)) { return(StatusCode(StatusCodes.Status500InternalServerError, new { message = "Error when updating Keycloak." })); } return(Ok()); }
/// <summary> /// Updates the User's email, phone number, and phone extension in Keycloak. Will remove any attribute set to null. Also gives the User the role(s) relevant to the PartyType(s) selected. /// Returns true if the operation was successful. /// </summary> /// <param name="userId"></param> /// <param name="party"></param> public async Task <bool> UpdatePhsaUserInfo(Guid userId, PhsaChangeModel party) { var response = await _client.GetAsync($"users/{userId}"); if (!response.IsSuccessStatusCode) { var responseMessage = await response.Content.ReadAsStringAsync(); _logger.LogError($"Could not retrieve user {userId} from keycloak. Response message: {responseMessage}"); return(false); } var userRep = await response.Content.ReadAsAsync <UserInfoUpdateRepresentation>(); userRep.Email = party.Email; userRep.SetPhoneNumber(party.Phone); userRep.SetPhoneExtension(party.PhoneExtension); response = await _client.PutAsync($"users/{userId}", CreateStringContent(userRep)); if (!response.IsSuccessStatusCode) { var responseMessage = await response.Content.ReadAsStringAsync(); _logger.LogDebug($"Could not update the user {userId}. Response message: {responseMessage}"); return(false); } bool success = true; if (party.PartyTypes.Contains(PartyType.Labtech)) { success &= await AssignRealmRole(userId, Roles.PhsaLabtech); } if (party.PartyTypes.Contains(PartyType.Immunizer)) { success &= await AssignRealmRole(userId, Roles.PhsaImmunizer); } return(success); }