Skip to content

Commit

Permalink
return role in personal info
Browse files Browse the repository at this point in the history
  • Loading branch information
DmyMi committed Nov 25, 2024
1 parent a382d75 commit b42cfe6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace OutOfSchool.BusinessLogic.Models;

public class ShortUserDto : BaseUserDto
{
[DataType(DataType.EmailAddress)]
public string UserName { get; set; }

public string Role { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ public static class CommunicationConstants
public const string BlockRegionAdmin = "regionadmin/block/";

public const string ReinviteRegionAdmin = "regionadmin/reinvite/";

public const int BufferSize = 1024;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OutOfSchool.Common.Communication.ICommunication;
Expand Down Expand Up @@ -94,22 +93,19 @@ public virtual async Task<Either<TError, T>> SendRequest<T, TError>(

requestMessage.Method = HttpMethodService.GetHttpMethodType(request);

logger.LogDebug("Sending request to {RequestMessage}", requestMessage);

var response = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead)
using var response = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
{
// Error response should be small, but still no need to waste time as default handler is ignoring it
var errorBody = errorHandler == null ? null : await response.Content.ReadAsStringAsync();
var errorBody = errorHandler == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var error = new CommunicationError(response.StatusCode)
{
Body = errorBody,
};
logger.LogDebug("Error body: {Body}", errorBody);
logger.LogError("Remote service error: {StatusCode}", response.StatusCode);
return await HandleErrorAsync(error, "Remote service error", errorHandler);
return await HandleErrorAsync(error, "Remote service error", errorHandler).ConfigureAwait(false);
}

await using var stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync()
Expand All @@ -120,12 +116,12 @@ public virtual async Task<Either<TError, T>> SendRequest<T, TError>(
catch (HttpRequestException ex)
{
logger.LogError(ex, "Networking error");
return await HandleExceptionAsync(ex, errorHandler);
return await HandleExceptionAsync(ex, errorHandler).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError(ex, "Unknown error");
return await HandleExceptionAsync(ex, errorHandler);
return await HandleExceptionAsync(ex, errorHandler).ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ public async Task GetPersonalInfo_WhenUserIsNotParent_ReturnsOkObjectResult()
// Arrange
var userId = Guid.NewGuid().ToString();

httpContext.Setup(x => x.User.FindFirst("sub"))
.Returns(new Claim(ClaimTypes.NameIdentifier, userId));

httpContext.Setup(x => x.User.IsInRole("parent"))
.Returns(false);

var controller = new PersonalInfoController(
userService.Object,
parentService.Object,
Expand All @@ -82,9 +76,10 @@ public async Task GetPersonalInfo_WhenUserIsNotParent_ReturnsOkObjectResult()
ControllerContext = new ControllerContext { HttpContext = httpContext.Object },
};

parentService.Setup(x => x.GetPersonalInfoByUserId(userId)).ReturnsAsync(new ShortUserDto());
userService.Setup(x => x.GetById(userId)).ReturnsAsync(new ShortUserDto());
currentUserService.Setup(c => c.UserId).Returns(userId);
currentUserService.Setup(c => c.IsInRole(Role.Parent)).Returns(false);
currentUserService.Setup(c => c.UserRole).Returns("provider");

// Act
var result = await controller.GetPersonalInfo().ConfigureAwait(false) as OkObjectResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ public async Task<IActionResult> GetPersonalInfo()
{
var info = currentUserService.IsInRole(Role.Parent)
? await parentService.GetPersonalInfoByUserId(currentUserService.UserId)
: null;
: await userService.GetById(currentUserService.UserId);

return Ok(info ?? (await userService.GetById(currentUserService.UserId)));
if (string.IsNullOrWhiteSpace(info.Role))
{
info.Role = currentUserService.UserRole;
}
return Ok(info);
}


Expand Down

0 comments on commit b42cfe6

Please sign in to comment.