in src/Services/User/User.Infrastructure/Identity/IdentityService.cs [151:213]
public async Task<Result> AddToMedicalRoleAsync(string userToken, string healthSecurityId)
{
var medicalRoleName = Roles.Medical.ToString("G");
logger.LogInformation("Start assignment of user with token {userToken} to {medicalRole} role.",
userToken,
medicalRoleName);
(Result searchResult, ApplicationUser user) = await FindUserByTokenAsync(userToken);
if (!searchResult.Succeeded)
{
logger.LogError("Error on assignment user with token {userToken}. Errors: {@message}",
userToken,
searchResult.Errors);
return searchResult;
}
if (await userManager.IsInRoleAsync(user, medicalRoleName))
{
logger.LogInformation("User with token {userToken} already assignment to {medicalRole} role.",
userToken,
medicalRoleName);
return Result.Success();
}
if (!medicalRegistrationRepository.TryRegistration(healthSecurityId))
{
Result result = Result.Failure(new InnerError(ErrorTarget.InvalidHealthSecurityId,
"Health security id is already taken or expired."));
logger.LogWarning("Unsuccessful result on validation of the security Id {healthSecurityId}. Errors: {@message}",
healthSecurityId,
result.Errors);
return result;
}
IdentityResult roleAssignmentResult = await userManager.AddToRoleAsync(user, medicalRoleName);
if (!roleAssignmentResult.Succeeded)
{
medicalRegistrationRepository.RollBackRegistration(healthSecurityId);
Result result = roleAssignmentResult.ToApplicationResult();
logger.LogError("Error on assignment user with token {userToken} to {medicalRole} role. Errors: {@message}",
userToken,
medicalRoleName,
result.Errors);
return result;
}
logger.LogInformation("Successfully assigned {userName} user with token {userToken} to {medicalRole} role..",
user.UserName,
userToken,
medicalRoleName);
return Result.Success();
}