Skip to content

Commit

Permalink
avniproject/avni-client#1181 - there can be multiple entities of the …
Browse files Browse the repository at this point in the history
…same type with same EAS
  • Loading branch information
petmongrels committed Jan 8, 2024
1 parent b5a5cd4 commit fc23ad3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Checklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Checklist extends BaseEntity {
}

addApprovalDescendantsWithLatestStatus(approvalStatus_status, list) {
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.items, approvalStatus_status, "baseDate", list);
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.items, approvalStatus_status, list, "uuid");
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/EntityApprovalStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import ApprovalStatus from "./ApprovalStatus";
import _ from 'lodash';
import SchemaNames from "./SchemaNames";

function getMatchingApprovalStatusEntities(entities, approvalStatus_status) {
return entities.filter((x) => !_.isNil(x.latestEntityApprovalStatus) && x.latestEntityApprovalStatus.hasStatus(approvalStatus_status));
}

class EntityApprovalStatus extends BaseEntity {
static schema = {
name: SchemaNames.EntityApprovalStatus,
Expand All @@ -30,15 +34,10 @@ class EntityApprovalStatus extends BaseEntity {
ChecklistItem: "ChecklistItem",
};

static getMatchingApprovalStatusEntity(entities, approvalStatus_status, latestFieldPath) {
return _.max(entities.filter((x) => !_.isNil(x.latestEntityApprovalStatus) && x.latestEntityApprovalStatus.hasStatus(approvalStatus_status)), (y) => _.get(y, latestFieldPath));
}

static addMatchingApprovalStatusEntity(entities, approvalStatus_status, latestFieldPath, list, groupingByPath) {
static addMatchingApprovalStatusEntity(entities, approvalStatus_status, list, groupingByPath) {
Object.values(_.groupBy(entities, (x) => _.get(x, groupingByPath))).forEach((y) => {
const latestEntity = EntityApprovalStatus.getMatchingApprovalStatusEntity(y, approvalStatus_status, latestFieldPath)
if (!_.isNil(latestEntity))
list.push(latestEntity);
const latestEntitiesOfSameType = getMatchingApprovalStatusEntities(y, approvalStatus_status);
list.push(...latestEntitiesOfSameType.map((x) => _.identity(x)));
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/Individual.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,8 @@ class Individual extends BaseEntity {
let descendants = [];
if (!_.isNil(this.latestEntityApprovalStatus) && this.latestEntityApprovalStatus.hasStatus(approvalStatus_status))
descendants.push(this);
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEncounters(), approvalStatus_status, "encounterDateTime", descendants, "encounterType.uuid");
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEnrolments(), approvalStatus_status, "enrolmentDateTime", descendants, "program.uuid");
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEncounters(), approvalStatus_status, descendants, "encounterType.uuid");
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEnrolments(), approvalStatus_status, descendants, "program.uuid");

this.nonVoidedEnrolments().forEach((enrolment) => {
const latestEntity = enrolment.addApprovalDescendantsWithLatestStatus(approvalStatus_status, descendants)
Expand Down
2 changes: 1 addition & 1 deletion src/ProgramEnrolment.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ class ProgramEnrolment extends BaseEntity {
}

addApprovalDescendantsWithLatestStatus(approvalStatus_status, list) {
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEncounters(), approvalStatus_status, "encounterDateTime", list, "encounterType.uuid");
EntityApprovalStatus.addMatchingApprovalStatusEntity(this.nonVoidedEncounters(), approvalStatus_status, list, "encounterType.uuid");
this.checklists.filter((x) => !x.voided).forEach((x) => {
x.addApprovalDescendantsWithLatestStatus(approvalStatus_status, list);
});
Expand Down

0 comments on commit fc23ad3

Please sign in to comment.