diff --git a/packages/openchs-android/integrationTest/BaseIntegrationTest.js b/packages/openchs-android/integrationTest/BaseIntegrationTest.js index 71d547d7f..03a5aaa3f 100644 --- a/packages/openchs-android/integrationTest/BaseIntegrationTest.js +++ b/packages/openchs-android/integrationTest/BaseIntegrationTest.js @@ -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); } diff --git a/packages/openchs-android/integrationTest/EntityApprovalServiceTest.js b/packages/openchs-android/integrationTest/EntityApprovalServiceTest.js new file mode 100644 index 000000000..000897503 --- /dev/null +++ b/packages/openchs-android/integrationTest/EntityApprovalServiceTest.js @@ -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; diff --git a/packages/openchs-android/integrationTest/IntegrationTestApp.js b/packages/openchs-android/integrationTest/IntegrationTestApp.js index 38f5e7202..104bfc762 100644 --- a/packages/openchs-android/integrationTest/IntegrationTestApp.js +++ b/packages/openchs-android/integrationTest/IntegrationTestApp.js @@ -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, @@ -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}; } diff --git a/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js b/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js index edeb9f5e2..ad15b624d 100644 --- a/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js +++ b/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js @@ -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"; @@ -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"; @@ -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, @@ -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, diff --git a/packages/openchs-android/integrationTest/service/TestMetadataService.js b/packages/openchs-android/integrationTest/service/TestMetadataService.js index c78a4db4d..6a399c05b 100644 --- a/packages/openchs-android/integrationTest/service/TestMetadataService.js +++ b/packages/openchs-android/integrationTest/service/TestMetadataService.js @@ -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; diff --git a/packages/openchs-android/src/service/EntityApprovalStatusService.js b/packages/openchs-android/src/service/EntityApprovalStatusService.js index b7bd7f9e8..64874a6db 100644 --- a/packages/openchs-android/src/service/EntityApprovalStatusService.js +++ b/packages/openchs-android/src/service/EntityApprovalStatusService.js @@ -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) => { diff --git a/packages/openchs-android/src/service/customDashboard/ReportCardService.js b/packages/openchs-android/src/service/customDashboard/ReportCardService.js index bb447e156..5f73d493e 100644 --- a/packages/openchs-android/src/service/customDashboard/ReportCardService.js +++ b/packages/openchs-android/src/service/customDashboard/ReportCardService.js @@ -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) : @@ -151,7 +150,6 @@ class ReportCardService extends BaseService { const {schema, filterQuery} = schemaAndQueryFilter; return this.getService(EntityApprovalStatusService).getAllEntitiesWithStatus(status, schema, filterQuery) } - } export default ReportCardService