Skip to content

Commit

Permalink
#1181 - get all subjects that have matching approval status
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Dec 6, 2023
1 parent 8a7275d commit f6c8fb0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class BaseIntegrationTest {
return this;
}

logQueries() {
GlobalContext.getInstance().db.setLogQueries(true);
}

log(...params) {
console["debug"]("\x1b[43m\x1b[30m%s\x1b[0m", ...params);
}
Expand Down
100 changes: 100 additions & 0 deletions packages/openchs-android/integrationTest/EntityApprovalServiceTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import BaseIntegrationTest from "./BaseIntegrationTest";
import {Concept, EntityApprovalStatus, Individual, ProgramEnrolment} from "openchs-models";
import TestEntityApprovalStatusFactory from "../test/model/approval/TestEntityApprovalStatusFactory";
import TestSubjectFactory from "../test/model/txn/TestSubjectFactory";
import TestObsFactory from "../test/model/TestObsFactory";
import General from "../src/utility/General";
import TestOrganisationService from "./service/TestOrganisationService";
import TestMetadataService from "./service/TestMetadataService";
import EntityApprovalStatusService from "../src/service/EntityApprovalStatusService";
import {assert} from "chai";
import TestConceptFactory from "../test/model/TestConceptFactory";
import {JSONStringify} from "../src/utility/JsonStringify";
import TestProgramEnrolmentFactory from "../test/model/txn/TestProgramEnrolmentFactory";
import moment from "moment/moment";

class EntityApprovalServiceTest extends BaseIntegrationTest {
setup(): this {
super.setup();
this.executeInWrite((db) => {
this.organisationData = TestOrganisationService.setupOrganisation(db);
this.concept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text}));
this.metadata = TestMetadataService.create(db);

const subject1Id = General.randomUUID();
const subject2Id = General.randomUUID();
const subject3Id = General.randomUUID();
const enrolmentId = General.randomUUID();

const subject1EAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.Subject,
entityUUID: subject1Id,
entityTypeUuid: this.metadata.subjectType.uuid,
approvalStatus: this.metadata.approvedStatus
}));
const subject1 = db.create(Individual, TestSubjectFactory.createWithDefaults({
uuid: subject1Id,
subjectType: this.metadata.subjectType,
address: this.organisationData.addressLevel,
firstName: "XYZ",
lastName: "bar",
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("ABC"))})],
approvalStatuses: [subject1EAS]
}));

const subject2EAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.Subject,
entityUUID: subject2Id,
entityTypeUuid: this.metadata.subjectType.uuid,
approvalStatus: this.metadata.approvedStatus
}));
const subject2 = db.create(Individual, TestSubjectFactory.createWithDefaults({
uuid: subject2Id,
subjectType: this.metadata.subjectType,
address: this.organisationData.addressLevel2,
firstName: "ABC",
lastName: "bar2",
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("DEF"))})],
approvalStatuses: [subject2EAS]
}));

const subject3 = db.create(Individual, TestSubjectFactory.createWithDefaults({
uuid: subject3Id,
subjectType: this.metadata.subjectType,
address: this.organisationData.addressLevel2,
firstName: "EFG",
lastName: "bar2",
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("DEF"))})],
approvalStatuses: [subject2EAS]
}));
const enrolmentEAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.ProgramEnrolment,
entityUUID: enrolmentId,
entityTypeUuid: this.metadata.program.uuid,
approvalStatus: this.metadata.approvedStatus
}));
const programEnrolment = db.create(ProgramEnrolment, TestProgramEnrolmentFactory.create({
uuid: enrolmentId,
program: this.metadata.program,
subject: subject3,
enrolmentDateTime: moment().add(-10, "day").toDate(),
latestEntityApprovalStatus: null,
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("DEFPRG"))})],
approvalStatuses: [enrolmentEAS]
}));
});

this.service = this.getService(EntityApprovalStatusService);
}

getSubjectEASes() {
this.logQueries();
const subjects = this.service.getAllSubjects(this.metadata.approvedStatus.status, null);
assert.equal(subjects.length, 3);
assert.equal(subjects[0].firstName, "ABC");
assert.equal(subjects[1].firstName, "EFG");
assert.equal(subjects[2].firstName, "XYZ");
}
}

export default EntityApprovalServiceTest;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import UtilTest from "./UtilTest";
import UserInfoServiceTest from "./UserInfoServiceTest";
import RealmProxyTest from "./RealmProxyTest";
import ReportCardServiceIntegrationTest from "./ReportCardServiceIntegrationTest";
import EntityApprovalServiceTest from "./EntityApprovalServiceTest";

const itemCommonStyle = {
padding: 10,
Expand Down Expand Up @@ -62,7 +63,7 @@ class IntegrationTestApp extends Component {
super(props, context);
FileSystem.init();
this.getBean = this.getBean.bind(this);
this.integrationTestRunner = new IntegrationTestRunner(ReportCardServiceIntegrationTest, UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest, RealmProxyTest);
this.integrationTestRunner = new IntegrationTestRunner(EntityApprovalServiceTest, ReportCardServiceIntegrationTest, UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest, RealmProxyTest);
this.state = {isInitialisationDone: false, integrationTests: this.integrationTestRunner.testSuite};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import BaseIntegrationTest from "./BaseIntegrationTest";
import {
ApprovalStatus, Concept,
CustomFilter,
Encounter,
EncounterType,
EntityApprovalStatus,
Individual,
Program,
ProgramEncounter,
ProgramEnrolment,
ReportCard,
StandardReportCardType,
SubjectType
} from "openchs-models";
import TestSubjectTypeFactory from "../test/model/TestSubjectTypeFactory";
import {Concept, CustomFilter, Encounter, EntityApprovalStatus, Individual, ProgramEncounter, ProgramEnrolment, ReportCard, StandardReportCardType} from "openchs-models";
import TestSubjectFactory from "../test/model/txn/TestSubjectFactory";
import TestObsFactory from "../test/model/TestObsFactory";
import ReportCardService from "../src/service/customDashboard/ReportCardService";
Expand All @@ -23,11 +9,8 @@ import TestDashboardReportFilterFactory from "../test/model/reportNDashboard/Tes
import {assert} from "chai";
import General from "../src/utility/General";
import TestEntityApprovalStatusFactory from "../test/model/approval/TestEntityApprovalStatusFactory";
import TestApprovalStatusFactory from "../test/model/approval/TestApprovalStatusFactory";
import TestEncounterFactory from "../test/model/txn/TestEncounterFactory";
import TestEncounterTypeFactory from "../test/model/TestEncounterTypeFactory";
import moment from "moment";
import TestProgramFactory from '../test/model/TestProgramFactory';
import TestProgramEnrolmentFactory from '../test/model/txn/TestProgramEnrolmentFactory';
import TestProgramEncounterFactory from '../test/model/txn/TestProgramEncounterFactory';
import TestChecklistService from "./service/TestChecklistService";
Expand Down Expand Up @@ -60,7 +43,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
entityType: EntityApprovalStatus.entityType.Subject,
entityUUID: subject1Id,
entityTypeUuid: this.metadata.subjectType.uuid,
approvalStatus: this.metadata.approvalStatus
approvalStatus: this.metadata.approvedStatus
}));
const encEAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.Encounter,
Expand Down Expand Up @@ -130,7 +113,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
entityType: EntityApprovalStatus.entityType.ProgramEnrolment,
entityUUID: programEnrolmentId2,
entityTypeUuid: this.metadata.program.uuid,
approvalStatus: this.metadata.enrolmentApprovalStatus
approvalStatus: this.metadata.approvedStatus
}));
const programEnc2EAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.ProgramEncounter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class TestMetadataService {
metadata.program = db.create(Program, TestProgramFactory.create({name: 'Child'}));
metadata.programEncounterType = db.create(EncounterType, TestEncounterTypeFactory.create({name: "Birth form"}));
metadata.encounterType = db.create(EncounterType, TestEncounterTypeFactory.create({name: "Bar"}));
metadata.approvalStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({}));
metadata.enrolmentApprovalStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({}));
metadata.approvedStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({}));
metadata.pendingStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({status: ApprovalStatus.statuses.Pending}));
metadata.rejectedStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({status: ApprovalStatus.statuses.Rejected}));
return metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ class EntityApprovalStatusService extends BaseService {
return savedStatus;
}

getAllSubjects(approvalStatus_status, reportFilters) {
const addressFilter = DashboardReportFilter.getAddressFilter(reportFilters);
let entities = RealmQueryService.filterBasedOnAddress(Individual.schema.name, this.getAll(Individual.schema.name), addressFilter);
entities = entities.filtered(`((latestEntityApprovalStatus.approvalStatus.status = $0 and voided = false)
or (enrolments.latestEntityApprovalStatus.approvalStatus.status = $1 and enrolments.voided = false)
or (encounters.latestEntityApprovalStatus.approvalStatus.status = $2 and encounters.voided = false)
or (enrolments.encounters.latestEntityApprovalStatus.approvalStatus.status = $3 and enrolments.encounters.voided = false)
or (enrolments.checklists.items.latestEntityApprovalStatus.approvalStatus.status = $4 and enrolments.voided = false)) SORT(firstName ASC)`,
approvalStatus_status, approvalStatus_status, approvalStatus_status, approvalStatus_status, approvalStatus_status);
return entities;
}

getAllEntitiesForReports(approvalStatus_status, reportFilters) {
const applicableEntitiesSchema = EntityApprovalStatus.getApprovalEntitiesSchema();
const result = _.map(applicableEntitiesSchema, (schema) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class ReportCardService extends BaseService {

getReportCardCount(reportCard, reportFilters) {
General.logDebug("ReportCardService", `Executing report card: ${reportCard.name}`);
General.logDebugTemp("ReportCardService", JSONStringify(reportFilters, 5));
const standardReportCardType = reportCard.standardReportCardType;
switch (true) {
case _.isNil(standardReportCardType) :
Expand Down Expand Up @@ -151,7 +150,6 @@ class ReportCardService extends BaseService {
const {schema, filterQuery} = schemaAndQueryFilter;
return this.getService(EntityApprovalStatusService).getAllEntitiesWithStatus(status, schema, filterQuery)
}

}

export default ReportCardService

0 comments on commit f6c8fb0

Please sign in to comment.