-
Notifications
You must be signed in to change notification settings - Fork 56
[Feat]- Get and organizations owned and belonged to by the user for admin #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lankiman
wants to merge
15
commits into
hngprojects:dev
Choose a base branch
from
lankiman:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2f859d0
feat(admin):major - add endpoints to retrieve user-owned and affiliat…
lankiman 12b8823
chore: removed secret keys from appsettings
lankiman 425e2fd
chore:implmented test for get user owned organizations and organizati…
lankiman 41fff18
fix: updated previos whitespacing fix
lankiman 0855dc6
fix: updated previos whitespacing fix
lankiman a9d61d7
fix: fixed test failure for admin user orginazation query handler
lankiman 1c4ee43
(fix)-minor - fixed white spaceformating issue for files specified by ci
lankiman a03bf53
(fix)-minor - fixed white spaceformating issue for files specified by ci
lankiman 517bdf5
fix:minor - implmented formating rules for ci validation
lankiman c2f51f3
fix(source control) fix merge conflicts when trying to commit
lankiman 8781fdb
fix(formating) - fixed whitespace formating for application test.csproj
lankiman 952dfdb
fix(formating)- fixed formating issue for files specified by ci
lankiman 32c0957
fix(formating)- fixed formating issue for files specified by ci
lankiman 263a8a5
fix(formating)- fixed formating issue for files specified by ci
lankiman 503bbc0
fix(formating)- fixed formating issue for files specified by ci
lankiman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...lication.Test/Features/SuperAdmin/GetUsersMemberOrgnazationsByUserIdQueryHandlerShould.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using AutoMapper; | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Features.SuperAdmin.Handlers; | ||
using Hng.Application.Features.SuperAdmin.Queries; | ||
using Hng.Application.Shared.Dtos; | ||
using Hng.Domain.Entities; | ||
using Hng.Infrastructure.Repository.Interface; | ||
using Moq; | ||
using System.Linq.Expressions; | ||
using Xunit; | ||
|
||
public class GetUsersMemberOrganizationsByUserIdQueryHandlerShould | ||
{ | ||
private readonly Mock<IRepository<User>> _mockRepository; | ||
private readonly IMapper _mapper; | ||
private readonly GetUsersMemberOrganizationsByUserIdQueryHandler handler; | ||
|
||
public GetUsersMemberOrganizationsByUserIdQueryHandlerShould() | ||
{ | ||
_mockRepository = new Mock<IRepository<User>>(); | ||
|
||
// Set up AutoMapper with your profiles | ||
var config = new MapperConfiguration(cfg => | ||
{ | ||
// Add your AutoMapper profiles here | ||
cfg.CreateMap<User, UserSuperDto>(); | ||
cfg.CreateMap<Organization, OrganizationDto>(); | ||
}); | ||
|
||
_mapper = config.CreateMapper(); | ||
handler = new GetUsersMemberOrganizationsByUserIdQueryHandler(_mockRepository.Object, _mapper); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnNullWhenUserIsNotFound() | ||
{ | ||
_mockRepository.Setup(repo => repo.GetBySpec( | ||
It.IsAny<Expression<Func<User, bool>>>(), | ||
It.IsAny<Expression<Func<User, object>>[]>() | ||
)).ReturnsAsync((User)null); | ||
|
||
var result = await handler.Handle( | ||
new GetUsersMemberOrganizationsByUserIdQuery(Guid.NewGuid(), new BaseQueryParameters()), | ||
CancellationToken.None | ||
); | ||
|
||
Assert.Null(result); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...lication.Test/Features/SuperAdmin/GetUsersOwnedOrganizationsByUserIdQueryHandlerShould.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using AutoMapper; | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Features.SuperAdmin.Handlers; | ||
using Hng.Application.Features.SuperAdmin.Queries; | ||
using Hng.Application.Shared.Dtos; | ||
using Hng.Domain.Entities; | ||
using Hng.Infrastructure.Repository.Interface; | ||
using Moq; | ||
using System.Linq.Expressions; | ||
using Xunit; | ||
|
||
namespace Hng.Application.Test.Features.SuperAdmin | ||
{ | ||
public class GetUsersMemberOrganizationsByUserIdQueryHandlerShould | ||
{ | ||
private readonly Mock<IRepository<User>> _mockRepository; | ||
private readonly IMapper _mapper; | ||
private readonly GetUsersMemberOrganizationsByUserIdQueryHandler handler; | ||
|
||
public GetUsersMemberOrganizationsByUserIdQueryHandlerShould() | ||
{ | ||
_mockRepository = new Mock<IRepository<User>>(); | ||
|
||
var config = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<User, UserSuperDto>(); | ||
cfg.CreateMap<Domain.Entities.Organization, OrganizationDto>(); | ||
}); | ||
_mapper = config.CreateMapper(); | ||
|
||
handler = new GetUsersMemberOrganizationsByUserIdQueryHandler(_mockRepository.Object, _mapper); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnNullWhenUserIsNotFound() | ||
{ | ||
_mockRepository.Setup(repo => repo.GetBySpec( | ||
It.IsAny<Expression<Func<User, bool>>>(), | ||
It.IsAny<Expression<Func<User, object>>[]>() | ||
)).ReturnsAsync((User)null); | ||
|
||
var result = await handler.Handle(new GetUsersMemberOrganizationsByUserIdQuery(Guid.NewGuid(), new BaseQueryParameters()), CancellationToken.None); | ||
|
||
Assert.Null(result); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Hng.Application/Features/SuperAdmin/Authorization/IsSuperAdminHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Hng.Application.Shared.Dtos; | ||
using Hng.Domain.Entities; | ||
using Hng.Infrastructure.Repository.Interface; | ||
using Hng.Infrastructure.Services.Interfaces; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Authorization; | ||
|
||
public class IsSuperAdminHandler : AuthorizationHandler<IsSuperAdminRequirement> | ||
{ | ||
private readonly IRepository<User> _userRepository; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IAuthenticationService _authenticationService; | ||
|
||
public IsSuperAdminHandler(IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IAuthenticationService authenticationService) | ||
{ | ||
_userRepository = userRepository; | ||
_httpContextAccessor = httpContextAccessor; | ||
_authenticationService = authenticationService; | ||
} | ||
|
||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, IsSuperAdminRequirement requirement) | ||
{ | ||
|
||
var userId = await _authenticationService.GetCurrentUserAsync(); | ||
|
||
if (!Guid.TryParse(userId.ToString(), out _)) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
var user = await _userRepository.GetBySpec(u => u.Id == userId); | ||
|
||
Console.WriteLine($"This is the User : {user}"); | ||
|
||
if (user != null && user.IsSuperAdmin) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
else | ||
{ | ||
var response = new StatusCodeResponse() | ||
{ | ||
StatusCode = StatusCodes.Status403Forbidden, | ||
Message = "You are not Authorized to access this resource" | ||
}; | ||
_httpContextAccessor.HttpContext.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
await _httpContextAccessor.HttpContext.Response.WriteAsJsonAsync(response); | ||
context.Fail(); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Hng.Application/Features/SuperAdmin/Authorization/IsSuperAdminRequirement.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Authorization; | ||
|
||
public class IsSuperAdminRequirement : IAuthorizationRequirement | ||
{ | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/Hng.Application/Features/SuperAdmin/Dto/OrganizationDto.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Dto; | ||
|
||
public class OrganizationDto | ||
{ | ||
|
||
[JsonPropertyName("name")] public string Name { get; set; } | ||
|
||
[JsonPropertyName("description")] public string Description { get; set; } | ||
|
||
[JsonPropertyName("slug")] public string Slug { get; set; } | ||
|
||
[JsonPropertyName("email")] public string Email { get; set; } | ||
|
||
[JsonPropertyName("industry")] public string Industry { get; set; } | ||
|
||
[JsonPropertyName("type")] public string Type { get; set; } | ||
|
||
[JsonPropertyName("country")] public string Country { get; set; } | ||
|
||
[JsonPropertyName("address")] public string Address { get; set; } | ||
|
||
[JsonPropertyName("state")] public string State { get; set; } | ||
|
||
[JsonPropertyName("is_active")] public bool IsActive { get; set; } | ||
|
||
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } | ||
|
||
} | ||
|
41 changes: 41 additions & 0 deletions
41
...plication/Features/SuperAdmin/Handlers/GetUsersMemberOrganizationsByUserIdQueryHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using AutoMapper; | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Features.SuperAdmin.Queries; | ||
using Hng.Application.Shared.Dtos; | ||
using Hng.Domain.Entities; | ||
using Hng.Infrastructure.Repository.Interface; | ||
using Hng.Infrastructure.Services.Interfaces; | ||
using MediatR; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Handlers; | ||
|
||
public class GetUsersMemberOrganizationsByUserIdQueryHandler : IRequestHandler<GetUsersMemberOrganizationsByUserIdQuery, PagedListDto<OrganizationDto>> | ||
{ | ||
private readonly IRepository<User> _userRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public GetUsersMemberOrganizationsByUserIdQueryHandler(IRepository<User> userRepository, IMapper mapper) | ||
{ | ||
_userRepository = userRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task<PagedListDto<OrganizationDto>> Handle(GetUsersMemberOrganizationsByUserIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
var user = await _userRepository.GetBySpec(user => user.Id == request.UserId, user => user.Organizations); | ||
|
||
if (user == null) | ||
{ | ||
return null; | ||
} | ||
var userMemberOrganizations = user.Organizations.Where(org => org.OwnerId != user.Id).ToList(); | ||
|
||
var mappedOrganizations = _mapper.Map<List<OrganizationDto>>(userMemberOrganizations); | ||
|
||
// var mappedProducts = _mapper.Map<IEnumerable<ProductDto>>(products); | ||
|
||
var organizationResult = PagedListDto<OrganizationDto>.ToPagedList(mappedOrganizations, request.UserMemberOrganizationsQueryParameter.Offset, request.UserMemberOrganizationsQueryParameter.Limit); | ||
|
||
return organizationResult; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...pplication/Features/SuperAdmin/Handlers/GetUsersOwnedOrganizationsByUserIdQueryHandler.cs
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user is not found, you can return an empty list instead of the null. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using AutoMapper; | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Features.SuperAdmin.Queries; | ||
using Hng.Application.Shared.Dtos; | ||
using Hng.Domain.Entities; | ||
using Hng.Infrastructure.Repository.Interface; | ||
using Hng.Infrastructure.Services.Interfaces; | ||
using MediatR; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Handlers; | ||
|
||
public class GetUsersOwnedOrganizationsByUserIdQueryHandler : IRequestHandler<GetUsersOwnedOrganizationsByUserIdQuery, PagedListDto<OrganizationDto>> | ||
{ | ||
private readonly IRepository<User> _userRepository; | ||
private readonly IMapper _mapper; | ||
|
||
public GetUsersOwnedOrganizationsByUserIdQueryHandler(IRepository<User> userRepository, IMapper mapper) | ||
{ | ||
_userRepository = userRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task<PagedListDto<OrganizationDto>> Handle(GetUsersOwnedOrganizationsByUserIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
var user = await _userRepository.GetBySpec(user => user.Id == request.UserId, user => user.Organizations); | ||
|
||
if (user == null) | ||
{ | ||
return null; | ||
} | ||
var userOwnedOrganizations = user.Organizations.Where(org => org.OwnerId == user.Id).ToList(); | ||
|
||
var mappedOrganizations = _mapper.Map<List<OrganizationDto>>(userOwnedOrganizations); | ||
|
||
var organizationResult = PagedListDto<OrganizationDto>.ToPagedList(mappedOrganizations, request.UserOwnedOrganizationsQueryParameter.Offset, request.UserOwnedOrganizationsQueryParameter.Limit); | ||
|
||
return organizationResult; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Hng.Application/Features/SuperAdmin/Queries/GetUsersMemberOrganizationByUserIdQuery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Shared.Dtos; | ||
using MediatR; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Queries; | ||
|
||
public class GetUsersMemberOrganizationsByUserIdQuery : IRequest<PagedListDto<OrganizationDto>> | ||
{ | ||
public GetUsersMemberOrganizationsByUserIdQuery(Guid userId, BaseQueryParameters userMemberOrganizationsQueryParameter) | ||
{ | ||
UserId = userId; | ||
UserMemberOrganizationsQueryParameter = userMemberOrganizationsQueryParameter; | ||
} | ||
public Guid UserId { get; set; } | ||
public BaseQueryParameters UserMemberOrganizationsQueryParameter { get; set; } | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Hng.Application/Features/SuperAdmin/Queries/GetUsersOwnedOrganizationsByUserIdQuery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Hng.Application.Features.SuperAdmin.Dto; | ||
using Hng.Application.Shared.Dtos; | ||
using MediatR; | ||
|
||
namespace Hng.Application.Features.SuperAdmin.Queries; | ||
|
||
public class GetUsersOwnedOrganizationsByUserIdQuery : IRequest<PagedListDto<OrganizationDto>> | ||
{ | ||
public GetUsersOwnedOrganizationsByUserIdQuery(Guid userId, BaseQueryParameters userOwnedOrganizationsQueryParameter) | ||
{ | ||
UserId = userId; | ||
UserOwnedOrganizationsQueryParameter = userOwnedOrganizationsQueryParameter; | ||
} | ||
public Guid UserId { get; set; } | ||
public BaseQueryParameters UserOwnedOrganizationsQueryParameter { get; set; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The field 'GetUsersMemberOrganizationsByUserIdQueryHandler._authenticationService' is never used