Skip to content

Commit

Permalink
fix(#9429): pass 500 reports to forms contact summar (#9436)
Browse files Browse the repository at this point in the history
  • Loading branch information
dianabarsan authored Sep 13, 2024
1 parent 25d027a commit 806b590
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
8 changes: 8 additions & 0 deletions tests/e2e/default/enketo/config/contact-summary-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
cards: [],
fields: [],
context: {
// eslint-disable-next-line no-undef
nbr_reports: reports.length,
}
};
46 changes: 46 additions & 0 deletions tests/e2e/default/enketo/contact-summary-reports.wdio-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const commonPage = require('@page-objects/default/common/common.wdio.page');
const utils = require('@utils');
const { cookieLogin } = require('@page-objects/default/login/login.wdio.page');
const commonEnketoPage = require('@page-objects/default/enketo/common-enketo.wdio.page');
const chtConfUtils = require('@utils/cht-conf');
const path = require('path');
const placeFactory = require('@factories/cht/contacts/place');
const personFactory = require('@factories/cht/contacts/person');
const reportsFactory = require('@factories/cht/reports/generic-report');

describe('Contact summary', () => {

const places = placeFactory.generateHierarchy();
const clinic = places.get('clinic');
const patient = personFactory.build({
name: 'Patient',
phone: '+50683444444',
patient_id: '12345',
parent: { _id: clinic._id, parent: clinic.parent }
});
const reports = Array
.from({ length: 100 })
.map(() => reportsFactory.report().build(
{ form: 'home_visit', content_type: 'xml' },
{ patient }
));

before(async () => {
await commonEnketoPage.uploadForm('contact-summary-reports');
await chtConfUtils.initializeConfigDir();
const contactSummaryFile = path.join(__dirname, 'config/contact-summary-reports.js');
const { contactSummary } = await chtConfUtils.compileNoolsConfig({ contactSummary: contactSummaryFile });
await utils.updateSettings({ contact_summary: contactSummary }, true);

await utils.saveDocs([...places.values(), patient, ...reports]);

await cookieLogin();
});

it('should load all reports', async () => {
await commonPage.goToPeople(patient._id);
await commonPage.openFastActionReport('contact-summary-reports', true);

expect(await commonEnketoPage.getInputValue('Number of reports')).to.equal(reports.length.toString());
});
});
51 changes: 51 additions & 0 deletions tests/e2e/default/enketo/forms/contact-summary-reports.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml">
<h:head>
<h:title>Just a form</h:title>
<model>
<instance>
<contact-summary-reports id="contact-summary-reports" prefix="J1!contact-summary-reports!" delimiter="#" version="2020-08-14_10-05">
<inputs>
<meta>
<location>
<lat/>
<long/>
<error/>
<message/>
</location>
</meta>
<user>
<contact_id/>
<facility_id/>
</user>
</inputs>
<contact>
<parent>PARENT</parent>
<type>person</type>
<name/>
<phone/>
<role/>
<external_id/>
<notes/>
<meta tag="hidden">
<created_by/>
<created_by_person_uuid/>
<created_by_place_uuid/>
</meta>
</contact>
<nbr_reports/>
<meta tag="hidden">
<instanceID/>
</meta>
</contact-summary-reports>
</instance>
<instance id="contact-summary"/>
<bind nodeset="/contact-summary-reports/nbr_reports" type="string" calculate="instance('contact-summary')/context/nbr_reports"/>
</model>
</h:head>
<h:body class="pages">
<input ref="/contact-summary-reports/nbr_reports">
<label>Number of reports:</label>
</input>
</h:body>
</h:html>
4 changes: 4 additions & 0 deletions tests/page-objects/default/enketo/common-enketo.wdio.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const uploadForm = async (formName, saveDoc = true) => {
internalId: formName,
title: formName,
type: 'form',
context: {
person: true,
place: true,
},
_attachments: {
xml: {
content_type: 'application/octet-stream',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Injectable, NgZone } from '@angular/core';
import {
groupBy as _groupBy,
partial as _partial,
find as _find,
} from 'lodash-es';
import { find as _find, groupBy as _groupBy, partial as _partial, } from 'lodash-es';

import registrationUtils from '@medic/registration-utils';

Expand Down Expand Up @@ -315,7 +311,7 @@ export class ContactViewModelGeneratorService {

private _loadReports(model, forms) {
const contacts = [ model.doc ];
model.children.forEach(group => {
model.children?.forEach(group => {
if (group?.type?.person) {
group.contacts.forEach(contact => contacts.push(contact.doc));
}
Expand Down
9 changes: 3 additions & 6 deletions webapp/src/ts/services/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ContactSaveService } from '@mm-services/contact-save.service';
import { reduce as _reduce } from 'lodash-es';
import { ContactTypesService } from '@mm-services/contact-types.service';
import { TargetAggregatesService } from '@mm-services/target-aggregates.service';
import { ContactViewModelGeneratorService } from '@mm-services/contact-view-model-generator.service';

/**
* Service for interacting with forms. This is the primary entry-point for CHT code to render forms and save the
Expand Down Expand Up @@ -58,6 +59,7 @@ export class FormService {
private chtDatasourceService: CHTDatasourceService,
private enketoService: EnketoService,
private targetAggregatesService: TargetAggregatesService,
private contactViewModelGeneratorService: ContactViewModelGeneratorService,
) {
this.inited = this.init();
this.globalActions = new GlobalActions(store);
Expand Down Expand Up @@ -127,12 +129,7 @@ export class FormService {
}

private getContactReports(contact) {
const subjectIds = [contact._id];
const shortCode = contact.patient_id || contact.place_id;
if (shortCode) {
subjectIds.push(shortCode);
}
return this.searchService.search('reports', { subjectIds: subjectIds }, { include_docs: true });
return this.contactViewModelGeneratorService.loadReports({ doc: contact }, []);
}

private getTargetDocs(contact) {
Expand Down
13 changes: 9 additions & 4 deletions webapp/tests/karma/ts/services/form.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { ExtractLineageService } from '@mm-services/extract-lineage.service';
import { EnketoTranslationService } from '@mm-services/enketo-translation.service';
import * as FileManager from '../../../../src/js/enketo/file-manager.js';
import { TargetAggregatesService } from '@mm-services/target-aggregates.service';
import { ContactViewModelGeneratorService } from '@mm-services/contact-view-model-generator.service';

describe('Form service', () => {
// return a mock form ready for putting in #dbContent
Expand Down Expand Up @@ -92,6 +93,7 @@ describe('Form service', () => {
let feedbackService;
let extractLineageService;
let targetAggregatesService;
let contactViewModelGeneratorService;

beforeEach(() => {
enketoInit = sinon.stub();
Expand Down Expand Up @@ -159,6 +161,7 @@ describe('Form service', () => {
feedbackService = { submit: sinon.stub() };
extractLineageService = { extract: ExtractLineageService.prototype.extract };
targetAggregatesService = { getTargetDocs: sinon.stub() };
contactViewModelGeneratorService = { loadReports: sinon.stub() };

TestBed.configureTestingModule({
providers: [
Expand Down Expand Up @@ -189,6 +192,7 @@ describe('Form service', () => {
{ provide: FeedbackService, useValue: feedbackService },
{ provide: ExtractLineageService, useValue: extractLineageService },
{ provide: TargetAggregatesService, useValue: targetAggregatesService },
{ provide: ContactViewModelGeneratorService, useValue: contactViewModelGeneratorService },
],
});

Expand Down Expand Up @@ -371,7 +375,7 @@ describe('Form service', () => {
}
};
ContactSummary.resolves({ context: { pregnant: true } });
Search.resolves([{ _id: 'somereport' }]);
contactViewModelGeneratorService.loadReports.resolves([{ _id: 'somereport' }]);
targetAggregatesService.getTargetDocs.resolves([{ _id: 't1' }, { _id: 't2' }]);
LineageModelGenerator.contact.resolves({ lineage: [{ _id: 'someparent' }] });
const formContext = new EnketoFormContext('div', 'report', mockEnketoDoc('myform'), instanceData);
Expand All @@ -383,9 +387,10 @@ describe('Form service', () => {
expect(summary.id).to.equal('contact-summary');
const xmlStr = new XMLSerializer().serializeToString(summary.xml);
expect(xmlStr).to.equal('<context><pregnant>true</pregnant></context>');
expect(Search.callCount).to.equal(1);
expect(Search.args[0][0]).to.equal('reports');
expect(Search.args[0][1].subjectIds).to.deep.equal(['fffff', '44509']);
expect(contactViewModelGeneratorService.loadReports.callCount).to.equal(1);
expect(contactViewModelGeneratorService.loadReports.args[0]).to.deep.equal(
[ { doc: instanceData.contact }, [] ]
);
expect(LineageModelGenerator.contact.callCount).to.equal(1);
expect(LineageModelGenerator.contact.args[0][0]).to.equal('fffff');
expect(targetAggregatesService.getTargetDocs.callCount).to.equal(1);
Expand Down

0 comments on commit 806b590

Please sign in to comment.