Skip to content

Commit

Permalink
Merge pull request #796 from bcgov/bugs/ecer-3893
Browse files Browse the repository at this point in the history
Bugs/ecer 3893
  • Loading branch information
farzadnadiri authored Jan 2, 2025
2 parents 0d0985b + 006df3d commit a7128df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ join reg in Registrants on cert.ecer_Registrantid.Id equals reg.Id

//Order by latest first (based on expiry date),
queryWithJoin = queryWithJoin
.OrderBy(r => r.cert.StatusCode)
.ThenByDescending(r => r.cert.ecer_ExpiryDate)
.ThenBy(r => r.cert.ecer_BaseCertificateTypeID);
.OrderBy(r => r.cert.StatusCode)
.ThenByDescending(r => r.cert.ecer_ExpiryDate)
.ThenBy(r => r.cert.ecer_BaseCertificateTypeID);

//Apply Pagination
if (query.PageSize > 0)
Expand All @@ -66,7 +66,15 @@ join reg in Registrants on cert.ecer_Registrantid.Id equals reg.Id
.Include(a => a.ecer_documenturl_CertificateId)
.Include(a => a.ecer_certificate_Registrantid)
.IncludeNested(a => a.ecer_certificateconditions_Registrantid)
.Execute().GroupBy(r => r.ecer_Registrantid.Id).Select(g => g.FirstOrDefault()); // Group by unique identifier (assuming RegistrantId)
.Execute().GroupBy(r => r.ecer_Registrantid.Id).Select(g => g
.OrderBy(r =>
r.StatusCode == ecer_Certificate_StatusCode.Active ? 1 :
r.StatusCode == ecer_Certificate_StatusCode.Cancelled ? 2 :
r.StatusCode == ecer_Certificate_StatusCode.Suspended ? 3 :
r.StatusCode == ecer_Certificate_StatusCode.Expired ? 4 :
5)
.ThenByDescending(r => r.ecer_ExpiryDate)
.ThenBy(r => r.ecer_BaseCertificateTypeID).FirstOrDefault()); // Group by unique identifier (assuming RegistrantId)

return mapper.Map<IEnumerable<Certification>>(results)!.ToList();
}
Expand Down
11 changes: 7 additions & 4 deletions src/ECER.Tests/Integration/Utilities/DataverseSdk/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class QueryTests : IAsyncLifetime
private readonly IConfigurationRoot configuration;
private ServiceClient serviceClient = null!;
private EcerContext dataverseContext = null!;
private static readonly Guid contactId = Guid.Parse("73127545-c481-ef11-899c-00090faa0001");
private static string applicationId = "36ae9353-28b6-4c15-827f-2fe14a609e70";
private static readonly Guid contactId = Guid.Parse("34852992-be02-4815-a4ef-9cd1f934109d");
private static string applicationId = "0e40773d-39a7-47cb-93a7-47fe46ec7e74";

private static readonly Guid[] applicationIds =
[
Expand Down Expand Up @@ -115,8 +115,11 @@ public void Join_OneToMany_NestedObjectApplication()
var result = results.FirstOrDefault();
result.ShouldNotBeNull();
result!.ecer_ecer_professionaldevelopment_Applicationi.ShouldNotBeNull();
var output = result.ecer_ecer_professionaldevelopment_Applicationi.First().ecer_bcgov_documenturl_ProfessionalDevelopmentId;
output.ShouldNotBeNull();

foreach (var item in result!.ecer_ecer_professionaldevelopment_Applicationi)
{
item.ecer_bcgov_documenturl_ProfessionalDevelopmentId.ShouldNotBeNull();
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,29 @@ public override IEnumerable<TEntity> Execute()
rootEntities.Any(e => e.GetType() == j.EnityType) ?
QuerySubEntities(j.RelatedLogicalEntityName, j.RelatedEntityForeignKeyAttributeName, keys) :
QuerySubEntities(j.RelatedLogicalEntityName, j.RelatedEntityForeignKeyAttributeName,
allEntities.FirstOrDefault(e => e.GetType() == j.EnityType) != null ? allEntities.FirstOrDefault(e => e.GetType() == j.EnityType)!.Id : Guid.Empty
allEntities.Any(e => e.GetType() == j.EnityType) ? allEntities.Where(e => e.GetType() == j.EnityType).Select(i => i.Id).ToArray() : Array.Empty<Guid>()
),

ManyToOneJoinData j => QuerySubEntities(j.RelatedLogicalEntityName, j.RelatedEntityForeignKeyAttributeName, rootEntities.Select(e => e.GetAttributeValue<EntityReference>(j.KeyAttributeName)!.Id).Distinct().ToArray()),
_ => throw new NotImplementedException()
};

relatedEntitiesMap.Add(join, entities);
allEntities.AddRange(entities.Entities);
}

foreach (var entity in rootEntities)
foreach (var entity in allEntities) // Process all entities, not just rootEntities
{
foreach (var related in relatedEntitiesMap)
{
if (related.Key is OneToManyJoinData o2mj)
{
// Identify the parent entity
var parentEntity = FindParentEntity(entity, o2mj);

if (parentEntity != null)
{
// Get related entities that belong to the identified parent
var relatedEntities = related.Value.Entities
.Where(e => e.GetAttributeValue<EntityReference>(o2mj.RelatedEntityForeignKeyAttributeName)!.Id == parentEntity.Id)
.ToArray();
Expand All @@ -93,9 +96,15 @@ public override IEnumerable<TEntity> Execute()
}
else if (related.Key is ManyToOneJoinData m2oj)
{
// Handle Many-to-One relationships
if (entity.GetAttributeValue<EntityReference>(m2oj.KeyAttributeName) == null) continue;
var key = entity.GetAttributeValue<EntityReference>(m2oj.KeyAttributeName)!.Id;
var relatedEntity = related.Value.Entities.SingleOrDefault(e => e.GetAttributeValue<Guid>(m2oj.RelatedEntityForeignKeyAttributeName) == key);
if (relatedEntity != null) AddRelatedEntities(entity, m2oj.RelationshipSchemaName, relatedEntity);

if (relatedEntity != null)
{
AddRelatedEntities(entity, m2oj.RelationshipSchemaName, relatedEntity);
}
}
else
{
Expand All @@ -110,32 +119,27 @@ public override IEnumerable<TEntity> Execute()

private Entity? FindParentEntity(Entity baseEntity, OneToManyJoinData joinData)
{
// Check if the baseEntity matches the target type
if (baseEntity.GetType() == joinData.EnityType)
{
return baseEntity;
}

// Iterate over all related entities
foreach (var relatedEntities in baseEntity.RelatedEntities.Values)
{
var parentEntity = relatedEntities.Entities
.FirstOrDefault(e => e.GetType() == joinData.EnityType);

if (parentEntity != null)
{
return parentEntity;
}

foreach (var entity in relatedEntities.Entities)
{
var nestedParent = FindParentEntity(entity, joinData);
if (nestedParent != null)
// Recursive call to find the parent in nested entities
var parentEntity = FindParentEntity(entity, joinData);
if (parentEntity != null)
{
return nestedParent;
return parentEntity;
}
}
}

return null;
return null; // Return null if no parent is found
}

protected virtual EntityCollection QuerySubEntities(string entityName, string keyAttributeName, params Guid[] keys)
Expand Down

0 comments on commit a7128df

Please sign in to comment.