Skip to content

Commit

Permalink
Merge pull request #542 from bcgov/stories/ecer-3259
Browse files Browse the repository at this point in the history
Stories/ecer 3259
  • Loading branch information
farzadnadiri authored Oct 5, 2024
2 parents 3f1e7b8 + ca274f0 commit 1dd5b19
Show file tree
Hide file tree
Showing 23 changed files with 570 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static async Task<int> Main(string[] args)
.RequireClaim(RegistryPortalClaims.IdenityProvider)
.RequireClaim(ClaimTypes.Name)
.RequireClaim(RegistryPortalClaims.UserId)
.RequireClaim("verified", "True")
.RequireClaim(RegistryPortalClaims.Verified, "True")
.RequireAuthenticatedUser();
})
.AddPolicy("registry_new_user", policy =>
Expand Down Expand Up @@ -132,25 +132,27 @@ private static async Task<int> Main(string[] args)

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI();
}

app.UseHealthChecks();
app.UseObservabilityMiddleware();
app.UseDisableHttpVerbsMiddleware(app.Configuration.GetValue("DisabledHttpVerbs", string.Empty));
app.UseRequestDecompression();
app.UseExceptionHandler();
app.UseResponseCompression();
app.UseCsp();
app.UseSecurityHeaders();
app.UseDisableHttpVerbsMiddleware(app.Configuration.GetValue("DisabledHttpVerbs", string.Empty));
app.UseCsp();
app.UseCors();
app.UseStaticFiles();
app.MapFallbackToFile("index.html");
app.UseCors();
app.UseOutputCache();
app.UseResponseCaching();
app.UseAuthentication();
app.UseAuthorization();

app.UseSwagger();
if (app.Environment.IsDevelopment())
app.UseSwaggerUI();

app.RegisterApiEndpoints();

await app.RunAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ECER.Utilities.ObjectStorage.Providers.S3;
using Microsoft.Extensions.Configuration;
using ECER.Utilities.ObjectStorage.Providers;
using ECER.Utilities.DataverseSdk.Queries;

namespace ECER.Resources.Accounts.Communications;

Expand Down Expand Up @@ -68,20 +69,20 @@ public async Task<CommunicationResult> Query(UserCommunicationQuery query)
int paginatedTotalCommunicationCount = 0;
if (query.PageNumber > 0)
{
paginatedTotalCommunicationCount = communications.Select(item => item.Id).ToList().Count;
communications = communications.OrderByDescending(item => item.ecer_LatestMessageNotifiedDate).Skip((query.PageNumber - 1) * query.PageSize).Take(query.PageSize);
paginatedTotalCommunicationCount = context.From(communications).Aggregate().Count();
communications = communications.OrderByDescending(item => item.ecer_LatestMessageNotifiedDate).Skip(query.PageNumber).Take(query.PageSize);
}
else
{
communications = communications.OrderByDescending(item => item.ecer_DateNotified);
}
var results = context.From(communications).Join().Include(c => c.ecer_bcgov_documenturl_CommunicationId_ecer_communication).Execute();

context.LoadProperties(communications, ecer_Communication.Fields.ecer_bcgov_documenturl_CommunicationId_ecer_communication);
var finalCommunications = mapper.Map<IEnumerable<Communication>>(results)!.ToList();

var finalCommunications = communications.ToList();
return new CommunicationResult
{
Communications = mapper.Map<IEnumerable<Communication>>(finalCommunications),
Communications = finalCommunications,
TotalMessagesCount = query.PageNumber > 0 ? paginatedTotalCommunicationCount : finalCommunications.Count,
};
}
Expand Down
36 changes: 18 additions & 18 deletions src/ECER.Resources.Accounts/Registrants/RegistrantRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using ECER.Utilities.DataverseSdk.Model;
using ECER.Utilities.DataverseSdk.Queries;
using ECER.Utilities.ObjectStorage.Providers;
using ECER.Utilities.ObjectStorage.Providers.S3;
using Microsoft.Extensions.Configuration;
Expand All @@ -13,7 +14,7 @@ public async Task<string> Create(Registrant registrant, CancellationToken ct)
{
await Task.CompletedTask;

var contact = string.IsNullOrEmpty(registrant.Id)? null : context.ContactSet.SingleOrDefault(c => c.ContactId.Equals(Guid.Parse(registrant.Id)));
var contact = string.IsNullOrEmpty(registrant.Id) ? null : context.ContactSet.SingleOrDefault(c => c.ContactId.Equals(Guid.Parse(registrant.Id)));

if (contact == null)
{
Expand Down Expand Up @@ -49,30 +50,29 @@ public async Task<string> Create(Registrant registrant, CancellationToken ct)
public async Task<IEnumerable<Registrant>> Query(RegistrantQuery query, CancellationToken ct)
{
await Task.CompletedTask;

var qry = context.ContactSet;
var contacts = context.ContactSet;

if (query.ByIdentity != null)
{
qry = from contact in context.ContactSet
join authentication in context.ecer_AuthenticationSet on contact.ContactId equals authentication.ecer_Customerid.Id
where authentication.ecer_IdentityProvider == query.ByIdentity.IdentityProvider && authentication.ecer_ExternalID == query.ByIdentity.UserId
select contact;
contacts = from contact in context.ContactSet
join authentication in context.ecer_AuthenticationSet on contact.ContactId equals authentication.ecer_Customerid.Id
where authentication.ecer_IdentityProvider == query.ByIdentity.IdentityProvider && authentication.ecer_ExternalID == query.ByIdentity.UserId
select contact;
}

if (query.ByUserId != null) qry = qry.Where(r => r.ContactId.Equals(Guid.Parse(query.ByUserId)));
if (query.ByUserId != null) contacts = contacts.Where(r => r.ContactId.Equals(Guid.Parse(query.ByUserId)));

if (query.ByLastName != null) qry = qry.Where(r => r.LastName.Equals(query.ByLastName));
if (query.ByRegistrationNumber != null) qry = qry.Where(r => r.ecer_ClientID.Equals(query.ByRegistrationNumber));
if (query.ByDateOfBirth != null) qry = qry.Where(r => r.BirthDate == query.ByDateOfBirth.Value.ToDateTime(TimeOnly.MinValue).Date);
if (query.ByLastName != null) contacts = contacts.Where(r => r.LastName.Equals(query.ByLastName));
if (query.ByRegistrationNumber != null) contacts = contacts.Where(r => r.ecer_ClientID.Equals(query.ByRegistrationNumber));
if (query.ByDateOfBirth != null) contacts = contacts.Where(r => r.BirthDate == query.ByDateOfBirth.Value.ToDateTime(TimeOnly.MinValue).Date);

var contacts = qry.ToList();
foreach (var contact in contacts)
{
context.LoadProperty(contact, nameof(Contact.ecer_contact_ecer_authentication_455));
context.LoadProperty(contact, nameof(Contact.ecer_previousname_Contactid));
}
return mapper.Map<IEnumerable<Registrant>>(contacts)!;
var results = context.From(contacts)
.Join()
.Include(a => a.ecer_contact_ecer_authentication_455)
.Include(a => a.ecer_previousname_Contactid)
.Execute();

return mapper.Map<IEnumerable<Registrant>>(results)!.ToList();
}

public async Task Save(Registrant registrant, CancellationToken ct)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using ECER.Resources.Documents.PortalInvitations;
using ECER.Utilities.DataverseSdk.Model;
using ECER.Utilities.DataverseSdk.Queries;
using ECER.Utilities.ObjectStorage.Providers;
using Microsoft.Extensions.Configuration;
using Microsoft.Xrm.Sdk.Client;
Expand Down Expand Up @@ -42,6 +43,7 @@ public async Task<IEnumerable<Application>> Query(ApplicationQuery query, Cancel
if (query.ByApplicantId != null) applications = applications.Where(r => r.ecer_Applicantid.Id == Guid.Parse(query.ByApplicantId));

var results = context.From(applications)
.Join()
.Include(a => a.ecer_transcript_Applicationid)
.Include(a => a.ecer_workexperienceref_Applicationid_ecer)
.Include(a => a.ecer_characterreference_Applicationid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AutoMapper;
using ECER.Utilities.DataverseSdk.Model;
using Microsoft.Xrm.Sdk.Client;
using ECER.Utilities.DataverseSdk.Queries;

namespace ECER.Resources.Documents.Certifications;

Expand All @@ -27,8 +27,13 @@ public async Task<IEnumerable<Certification>> Query(UserCertificationQuery query
if (query.ByApplicantId != null) Certifications = Certifications.Where(r => r.ecer_Registrantid.Id == Guid.Parse(query.ByApplicantId));

Certifications = Certifications.OrderByDescending(r => r.ecer_ExpiryDate);
context.LoadProperties(Certifications, ecer_Certificate.Fields.ecer_certifiedlevel_CertificateId);
context.LoadProperties(Certifications, ecer_Certificate.Fields.ecer_documenturl_CertificateId);
return mapper.Map<IEnumerable<Certification>>(Certifications.ToList());

var results = context.From(Certifications)
.Join()
.Include(a => a.ecer_certifiedlevel_CertificateId)
.Include(a => a.ecer_documenturl_CertificateId)
.Execute();

return mapper.Map<IEnumerable<Certification>>(results)!.ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using AutoMapper;
using ECER.Utilities.DataverseSdk.Model;


namespace ECER.Resources.Documents.MetadataResources;

internal sealed class MetadataResourceRepository : IMetadataResourceRepository
{

private readonly EcerContext context;
private readonly IMapper mapper;

Expand All @@ -15,6 +13,7 @@ public MetadataResourceRepository(EcerContext context, IMapper mapper)
this.context = context;
this.mapper = mapper;
}

public async Task<IEnumerable<Province>> QueryProvinces(ProvincesQuery query, CancellationToken cancellationToken)
{
await Task.CompletedTask;
Expand Down
15 changes: 10 additions & 5 deletions src/ECER.Tests/Integration/AuthenticationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
using Alba;
using ECER.Utilities.DataverseSdk.Model;
using ECER.Utilities.Security;
using System.Security.Claims;

namespace ECER.Tests.Integration;

public static class AuthenticationHelper
{
public static Scenario WithNewUser(this Scenario scenario, UserIdentity identity) => scenario.WithUser(identity);
public static Scenario WithNewUser(this Scenario scenario, UserIdentity identity) => scenario.WithUser(identity, null);

public static Scenario WithExistingUser(this Scenario scenario, UserIdentity identity, string userId) => scenario.WithUser(identity, userId);
public static Scenario WithExistingUser(this Scenario scenario, UserIdentity identity, Contact user) => scenario.WithUser(identity, user);

private static Scenario WithUser(this Scenario scenario, UserIdentity identity, string? userId = null)
private static Scenario WithUser(this Scenario scenario, UserIdentity identity, Contact? user)
{
ArgumentNullException.ThrowIfNull(identity);

scenario.WithClaim(RegistryPortalClaims.IdenityProvider, identity.IdentityProvider);
scenario.WithClaim(ClaimTypes.Name, identity.UserId);
scenario.WithClaim(ClaimTypes.NameIdentifier, identity.UserId);
if (!string.IsNullOrEmpty(userId)) scenario.WithClaim(RegistryPortalClaims.UserId, userId);

if (user != null)
{
scenario.WithClaim(RegistryPortalClaims.UserId, user.Id.ToString());
bool verified = user.ecer_IsVerified ?? false;
scenario.WithClaim(RegistryPortalClaims.Verified, verified.ToString());
}
return scenario;
}

Expand Down
Loading

0 comments on commit 1dd5b19

Please sign in to comment.