Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Migrate offline form concept label loading to new bulk endpoint #767

Merged
5 changes: 3 additions & 2 deletions packages/esm-form-entry-app/src/app/offline/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import escapeRegExp from 'lodash-es/escapeRegExp';
import { FormSchemaService } from '../form-schema/form-schema.service';
import { FormEncounter, FormSchema } from '../types';
import { ConceptService } from '../services/concept.service';

export function setupStaticDataOfflinePrecaching() {
subscribePrecacheStaticDependencies(async () => {
Expand Down Expand Up @@ -90,8 +91,8 @@ async function getCacheableFormUrls(formUuid: string) {

const conceptLang = (window as any).i18next?.language?.substring(0, 2).toLowerCase() || 'en';
const requiredConceptIdentifiers = FormSchemaService.getUnlabeledConceptIdentifiersFromSchema(formSchema);
const conceptUrls = requiredConceptIdentifiers.map(
(identifier) => `/ws/rest/v1/concept/${identifier}?v=full&lang=${conceptLang}`,
const conceptUrls = ConceptService.getRelativeConceptLabelUrls(requiredConceptIdentifiers, conceptLang).map(
(relativeUrl) => `/ws/rest/v1/${relativeUrl}`,
);

return [
Expand Down
22 changes: 15 additions & 7 deletions packages/esm-form-entry-app/src/app/services/concept.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ export class ConceptService {
}

public searchBulkConceptByUUID(conceptUuids: Array<string>, lang: string): Observable<Array<ConceptMetadata>> {
const chunkSize = 100;
const observablesArray = [];
const slicedConceptUuids = conceptUuids.reduceRight(
(acc, currentValue, i, array) => [...acc, array.splice(0, chunkSize)],
[],
);
const relativeConceptLabelUrls = ConceptService.getRelativeConceptLabelUrls(conceptUuids, lang);

for (const conceptUuidsChunk of slicedConceptUuids) {
for (const relativeConceptLabelUrl of relativeConceptLabelUrls) {
observablesArray.push(
this.http
.get<ListResult<ConceptMetadata>>(this.getUrl() + `?references=` + conceptUuidsChunk.join(','), {
.get<ListResult<ConceptMetadata>>(this.getUrl() + relativeConceptLabelUrl, {
headers: this.headers,
})
.pipe(
Expand All @@ -57,4 +53,16 @@ export class ConceptService {
}),
);
}

/**
* Partitions the given concept UUIDs into relative URLs pointing to the concept
* bulk fetching endpoint.
* @param conceptUuids The concept UUIDs to be partitioned into bulk fetching URLs.
*/
public static getRelativeConceptLabelUrls(conceptUuids: Array<string>, lang: string) {
const chunkSize = 100;
return conceptUuids
.reduceRight((acc, _, __, array) => [...acc, array.splice(0, chunkSize)], [])
.map((uuidPartition) => `concept?references=${uuidPartition.join(',')}`);
}
}