Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…mployerapprenticeshipsservice into CON-1488-Employer-unsubscribe-alert-summary-notification-email
  • Loading branch information
reachash committed Aug 25, 2020
2 parents 8e7c2a5 + 9621fd8 commit 486d2e5
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 16 deletions.
5 changes: 5 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ resources:
endpoint: "GitHub (SFA)"

steps:
- task: UseDotNet@2
displayName: 'Use .NET Core sdk 2.2.x'
inputs:
version: 2.2.x

- task: gittools.gitversion.gitversion-task.GitVersion@5
displayName: GitVersion
inputs:
Expand Down
6 changes: 3 additions & 3 deletions azure/accounts.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"idaTenant": {
"type": "string"
},
"idaAudience": {
"accountsApiIdentifierUri": {
"type": "string"
},
"appServiceAllowedIPs": {
Expand Down Expand Up @@ -307,8 +307,8 @@
"value": "[parameters('idaTenant')]"
},
{
"name": "IdaAudience",
"value": "[parameters('idaAudience')]"
"name": "AccountsApiIdentifierUri",
"value": "[parameters('accountsApiIdentifierUri')]"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class AccountLegalEntityViewModel
public long AccountLegalEntityId { get; set; }
public string AccountLegalEntityPublicHashedId { get; set; }
public long LegalEntityId { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class AccountLegalEntity
public long AccountLegalEntityId { get; set; }
public string AccountLegalEntityPublicHashedId { get; set; }
public long LegalEntityId { get; set; }
public string Name { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/SFA.DAS.EmployerAccounts.Api.Types/Agreement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Agreement
public long Id { get; set; }
public DateTime? SignedDate { get; set; }
public string SignedByName { get; set; }
public long? SignedById { get; set; }
public string SignedByEmail { get; set; }
public EmployerAgreementStatus Status { get; set; }
public int TemplateVersionNumber { get; set; }
public AgreementType AgreementType { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/SFA.DAS.EmployerAccounts.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions
_ = app.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["idaTenant"],
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
ValidAudience = ConfigurationManager.AppSettings["idaAudience"]
ValidAudiences = ConfigurationManager.AppSettings["AccountsApiIdentifierUri"].ToString().Split(',')
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SignedAgreementEvent : Event
public Guid UserRef { get; set; }
public string OrganisationName { get; set; }
public long AgreementId { get; set; }
public long AccountLegalEntityId { get; set; }
public long LegalEntityId { get; set; }
public bool CohortCreated { get; set; }
public AgreementType AgreementType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class WhenISignAnEmployerAgreement
private const long LegalEntityId = 111333;
private const string OrganisationName = "Foo";
private const string HashedLegalEntityId = "2635JHG";
private const long AccountLegalEntityId = 9568456;
private const AgreementType AgreementType = Common.Domain.Types.AgreementType.NonLevyExpressionOfInterest;

[SetUp]
Expand Down Expand Up @@ -80,6 +81,7 @@ public void Setup()
LegalEntityName = OrganisationName,
AgreementType = AgreementType,
AccountId = AccountId,
AccountLegalEntityId = AccountLegalEntityId,
Id = AgreementId
};

Expand Down Expand Up @@ -230,6 +232,7 @@ public async Task ThenTheServiceShouldBeNotified()
message.AccountId.Should().Be(AccountId);
message.AgreementId.Should().Be(AgreementId);
message.OrganisationName.Should().Be(OrganisationName);
message.AccountLegalEntityId.Should().Be(AccountLegalEntityId);
message.LegalEntityId.Should().Be(LegalEntityId);
message.CohortCreated.Should().BeTrue();
message.UserName.Should().Be(_owner.FullName());
Expand All @@ -255,6 +258,7 @@ public async Task ThenIfICannotGetCommitmentsForTheAccountIStillNotifyTheService
message.AccountId.Should().Be(AccountId);
message.AgreementId.Should().Be(AgreementId);
message.OrganisationName.Should().Be(OrganisationName);
message.AccountLegalEntityId.Should().Be(AccountLegalEntityId);
message.LegalEntityId.Should().Be(LegalEntityId);
message.CohortCreated.Should().BeFalse();
message.UserName.Should().Be(_owner.FullName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SFA.DAS.EmployerAccounts.Data;
using SFA.DAS.EmployerAccounts.Mappings;
using SFA.DAS.EmployerAccounts.Models.Account;
using SFA.DAS.EmployerAccounts.Models.UserProfile;
using SFA.DAS.EmployerAccounts.Queries.GetLegalEntity;
using SFA.DAS.EmployerAccounts.TestCommon;
using FluentTestFixture = SFA.DAS.Testing.FluentTestFixture;
Expand All @@ -26,13 +27,24 @@ public Task Handle_WhenGettingLegalEntity_ThenShouldReturnLegalEntity()
return RunAsync(f => f.Handle(), (f, r) => r.Should().NotBeNull()
.And.Match<GetLegalEntityResponse>(r2 =>
r2.LegalEntity.LegalEntityId == f.LegalEntity.Id &&
r2.LegalEntity.Agreements.Count == 2 &&
r2.LegalEntity.Agreements.Count == 3 &&
r2.LegalEntity.Agreements.Any(a => a.TemplateVersionNumber == 1 && a.Status == Api.Types.EmployerAgreementStatus.Signed) &&
r2.LegalEntity.Agreements.Any(a => a.TemplateVersionNumber == 2 && a.Status == Api.Types.EmployerAgreementStatus.Pending) &&
r2.LegalEntity.Agreements.Any(a => a.TemplateVersionNumber == 2 && a.Status == Api.Types.EmployerAgreementStatus.Signed) &&
r2.LegalEntity.Agreements.Any(a => a.TemplateVersionNumber == 3 && a.Status == Api.Types.EmployerAgreementStatus.Pending) &&
r2.LegalEntity.AgreementStatus == Api.Types.EmployerAgreementStatus.Pending &&
r2.LegalEntity.Agreements.Any(a => a.AgreementType == f.LegalEntity.AccountLegalEntities.First().Agreements.First().Template.AgreementType)
));
}

[Test]
public async Task Handle_WhenGettingLegalEntity_ThenShouldReturnTheCorrectEmailOfUserWhoSignedTheAgreement()
{
var f = new GetLegalEntityQueryTestsFixture();
var result = await f.Handle();

result.LegalEntity.Agreements.First(a => a.TemplateVersionNumber == 1 && a.Status == Api.Types.EmployerAgreementStatus.Signed).SignedByEmail.Should().Be(f.UserB.Email);
result.LegalEntity.Agreements.First(a => a.TemplateVersionNumber == 2 && a.Status == Api.Types.EmployerAgreementStatus.Signed).SignedByEmail.Should().Be(f.UserA.Email);
}
}

public class GetLegalEntityQueryTestsFixture : FluentTestFixture
Expand All @@ -47,6 +59,10 @@ public class GetLegalEntityQueryTestsFixture : FluentTestFixture
public List<LegalEntity> LegalEntities { get; set; }
public List<AccountLegalEntity> AccountLegalEntities { get; set; }
public DbSetStub<AccountLegalEntity> AccountLegalEntitiesDbSet { get; set; }
public User UserA { get; set; }
public User UserB { get; set; }
public List<User> Users { get; set; }
public DbSetStub<User> UsersDbSet { get; set; }

public GetLegalEntityQueryTestsFixture()
{
Expand All @@ -64,17 +80,24 @@ public GetLegalEntityQueryTestsFixture()
AccountLegalEntities = new List<AccountLegalEntity>();
AccountLegalEntitiesDbSet = new DbSetStub<AccountLegalEntity>(AccountLegalEntities);

UserA = new User { Id = 1, Email = "[email protected]"};
UserB = new User { Id = 2, Email = "[email protected]"};
Users = new List<User> { UserA, UserB };
UsersDbSet = new DbSetStub<User>(Users);

Db.Setup(d => d.LegalEntities).Returns(LegalEntitiesDbSet);
Db.Setup(d => d.AccountLegalEntities).Returns(AccountLegalEntitiesDbSet);
Db.Setup(d => d.Users).Returns(UsersDbSet);

Handler = new GetLegalEntityQueryHandler(new Lazy<EmployerAccountsDbContext>(() => Db.Object), ConfigurationProvider);

SetAccount()
.SetLegalEntity()
.SetLegalAccountLegalEntity()
.AddLegalEntityAgreement(1, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Removed)
.AddLegalEntityAgreement(1, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Signed)
.AddLegalEntityAgreement(2, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Pending)
.AddLegalEntityAgreement(1, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Removed, UserA.Id)
.AddLegalEntityAgreement(1, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Signed, UserB.Id)
.AddLegalEntityAgreement(2, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Signed, UserA.Id)
.AddLegalEntityAgreement(3, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus.Pending)
.EvaluateSignedAndPendingAgreementIdsForAllAccountLegalEntities();
}

Expand Down Expand Up @@ -153,7 +176,7 @@ private GetLegalEntityQueryTestsFixture SetLegalAccountLegalEntity()
return this;
}

private GetLegalEntityQueryTestsFixture AddLegalEntityAgreement(int versionNumber, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus status)
private GetLegalEntityQueryTestsFixture AddLegalEntityAgreement(int versionNumber, EmployerAccounts.Models.EmployerAgreement.EmployerAgreementStatus status, long? signedUserId = null)
{
var newAgreement = new EmployerAgreement
{
Expand All @@ -162,7 +185,8 @@ private GetLegalEntityQueryTestsFixture AddLegalEntityAgreement(int versionNumbe
VersionNumber = versionNumber,
AgreementType = AgreementType.Levy
},
StatusId = status
StatusId = status,
SignedById = signedUserId
};

newAgreement.AccountLegalEntity = AccountLegalEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private async Task PublihAgreementSignedMessage(EmployerAgreementView agreement,
var commitments = await _commitmentService.GetEmployerCommitments(agreement.AccountId);
var accountHasCommitments = commitments?.Any() ?? false;

await PublishAgreementSignedMessage(agreement.AccountId, agreement.LegalEntityId, agreement.LegalEntityName,
await PublishAgreementSignedMessage(agreement.AccountId, agreement.AccountLegalEntityId, agreement.LegalEntityId, agreement.LegalEntityName,
agreement.Id, accountHasCommitments, owner.FullName(), owner.UserRef, agreement.AgreementType,
agreement.VersionNumber, correlationId);
}
Expand Down Expand Up @@ -141,14 +141,15 @@ private async Task SignAgreement(SignEmployerAgreementCommand message, long agre
}

private Task PublishAgreementSignedMessage(
long accountId, long legalEntityId, string legalEntityName, long agreementId,
long accountId, long accountLegalEntityId, long legalEntityId, string legalEntityName, long agreementId,
bool cohortCreated, string currentUserName, Guid currentUserRef,
AgreementType agreementType, int versionNumber, string correlationId)
{
return _eventPublisher.Publish(new SignedAgreementEvent
{
AccountId = accountId,
AgreementId = agreementId,
AccountLegalEntityId = accountLegalEntityId,
LegalEntityId = legalEntityId,
OrganisationName = legalEntityName,
CohortCreated = cohortCreated,
Expand Down
3 changes: 3 additions & 0 deletions src/SFA.DAS.EmployerAccounts/Mappings/AgreementMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public AgreementMappings()
.ForMember(
v => v.AgreementType,
o => o.MapFrom(a => a.Template.AgreementType))
.ForMember(
d => d.SignedByEmail,
o => o.Ignore());
;

CreateMap<EmployerAgreement, EmployerAgreementDto>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public async Task<GetLegalEntityResponse> Handle(GetLegalEntityQuery message)
var legalEntity = await _db.Value.AccountLegalEntities
.Where(l =>
l.LegalEntityId == message.LegalEntityId &&
l.Account.HashedId == message.AccountHashedId &&
l.Account.HashedId == message.AccountHashedId &&
(l.PendingAgreementId != null || l.SignedAgreementId != null) &&
l.Deleted == null)
l.Deleted == null)
.ProjectTo<LegalEntity>(_configurationProvider, new
{
accountHashedId = message.AccountHashedId
Expand All @@ -40,6 +40,8 @@ public async Task<GetLegalEntityResponse> Handle(GetLegalEntityQuery message)
.OrderByDescending(a => a.TemplateVersionNumber)
.FirstOrDefault();

await SetEmailAddressForSignatures(legalEntity);

if (legalEntity != null && latestAgreement != null)
{
legalEntity.AgreementSignedByName = latestAgreement.SignedByName;
Expand All @@ -52,5 +54,19 @@ public async Task<GetLegalEntityResponse> Handle(GetLegalEntityQuery message)
LegalEntity = legalEntity
};
}

private async Task SetEmailAddressForSignatures(LegalEntity legalEntity)
{
var userIds = legalEntity?.Agreements?.Where(a=>a.SignedById != null).Select(a => a.SignedById).ToArray();

if (userIds != null && userIds.Any())
{
var users = await _db.Value.Users.Where(u => userIds.Contains(u.Id)).Select(u => new { u.Id, u.Email }).ToListAsync();
legalEntity.Agreements.Where(a => a.SignedById != null).ToList().ForEach(a =>
{
a.SignedByEmail = users.FirstOrDefault(u => u.Id == a.SignedById)?.Email;
});
}
}
}
}

0 comments on commit 486d2e5

Please sign in to comment.