Skip to content
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

FAI-958 AAN Hub API Condition to check if the Member already exist before Creation #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NUnit.Framework;
using SFA.DAS.AANHub.Application.Admins.Commands.CreateAdminMember;
using SFA.DAS.AANHub.Domain.Entities;
using SFA.DAS.AANHub.Domain.Interfaces;
using SFA.DAS.AANHub.Domain.Interfaces.Repositories;
using SFA.DAS.Testing.AutoFixture;
using static SFA.DAS.AANHub.Domain.Common.Constants;
Expand All @@ -16,15 +17,40 @@ public class CreateAdminMemberCommandHandlerTests
[MoqAutoData]
public async Task Handle_AddsNewAdmin(
[Frozen] Mock<IMembersWriteRepository> membersWriteRepository,
[Frozen] Mock<IMembersReadRepository> membersReadRepository,
[Frozen] Mock<IAuditWriteRepository> auditWriteRepository,
CreateAdminMemberCommandHandler sut,
CreateAdminMemberCommand command)
{
membersReadRepository.Setup(p => p.GetMemberByEmail(command.Email!)).ReturnsAsync((Member)null!);

var response = await sut.Handle(command, new CancellationToken());
response.Result.MemberId.Should().Be(command.MemberId);

membersWriteRepository.Verify(p => p.Create(It.Is<Member>(x => x.Id == command.MemberId)));
membersWriteRepository.Verify(p => p.Create(It.Is<Member>(x => x.UserType == MembershipUserType.Admin)));
auditWriteRepository.Verify(p => p.Create(It.Is<Audit>(x => x.ActionedBy == command.MemberId)));
}

[Test]
[MoqAutoData]
public async Task Handle_ExistingAdmin(
[Frozen] Mock<IMembersWriteRepository> membersWriteRepository,
[Frozen] Mock<IMembersReadRepository> membersReadRepository,
[Frozen] Mock<IAuditWriteRepository> auditWriteRepository,
[Frozen] Mock<IAanDataContext> anDataContext,
CreateAdminMemberCommandHandler sut,
CreateAdminMemberCommand command)
{
membersReadRepository.Setup(p => p.GetMemberByEmail(command.Email!)).ReturnsAsync(command);

var response = await sut.Handle(command, new CancellationToken());
response.Result.MemberId.Should().Be(command.MemberId);
balaji-j marked this conversation as resolved.
Show resolved Hide resolved

membersWriteRepository.Verify(p => p.Create(It.Is<Member>(x => x.Id == command.MemberId)), Times.Never);
membersWriteRepository.Verify(p => p.Create(It.Is<Member>(x => x.UserType == MembershipUserType.Admin)), Times.Never);
auditWriteRepository.Verify(p => p.Create(It.Is<Audit>(x => x.ActionedBy == command.MemberId)), Times.Never);

anDataContext.Verify(p => p.SaveChangesAsync(new CancellationToken()), Times.Never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,29 @@ public class CreateAdminMemberCommandHandler : IRequestHandler<CreateAdminMember
private readonly IAanDataContext _aanDataContext;
private readonly IAuditWriteRepository _auditWriteRepository;
private readonly IMembersWriteRepository _membersWriteRepository;
private readonly IMembersReadRepository _membersReadRepository;

public CreateAdminMemberCommandHandler(IMembersWriteRepository membersWriteRepository,
IAanDataContext aanDataContext, IAuditWriteRepository auditWriteRepository)
IAanDataContext aanDataContext, IAuditWriteRepository auditWriteRepository,
IMembersReadRepository membersReadRepository)
{
_membersWriteRepository = membersWriteRepository;
_aanDataContext = aanDataContext;
_auditWriteRepository = auditWriteRepository;
_membersReadRepository = membersReadRepository;
}

public async Task<ValidatedResponse<CreateMemberCommandResponse>> Handle(CreateAdminMemberCommand command,
CancellationToken cancellationToken)
{
Member member = command;

//condition to check if the member already exist in the repo.
var user = await _membersReadRepository.GetMemberByEmail(member.Email);

if (user != null)
return new ValidatedResponse<CreateMemberCommandResponse>(new CreateMemberCommandResponse(user.Id));

_membersWriteRepository.Create(member);

_auditWriteRepository.Create(new Audit
Expand Down
Loading