Skip to content

Commit

Permalink
Merge pull request #328 from aehrc/fix/populate
Browse files Browse the repository at this point in the history
Fix population for repeat group items requiring valueset resolving
  • Loading branch information
fongsean authored Jul 20, 2023
2 parents bb3c348 + 21dff0d commit 2e03982
Show file tree
Hide file tree
Showing 4 changed files with 1,562 additions and 2,092 deletions.
2 changes: 1 addition & 1 deletion apps/smart-forms-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"allotment": "^1.19.0",
"dayjs": "^1.11.7",
"fhirclient": "^2.5.2",
"fhirpath": "^3.5.0",
"fhirpath": "^3.6.0",
"html-react-parser": "^3.0.16",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
Expand Down

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 67 additions & 38 deletions packages/sdc-populate/src/constructResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function constructResponse(
// In second step, resolves all promises in parallel and populate valueSet answers by comparing their codes
const topLevelQRItems: QuestionnaireResponseItem[] = [];
for (const qItem of topLevelQItems) {
const newTopLevelQRItem = constructResponseItem(
const newTopLevelQRItem = constructResponseItemRecursive(
qItem,
{
linkId: qItem.linkId,
Expand Down Expand Up @@ -102,7 +102,7 @@ export async function constructResponse(
*
* @author Sean Fong
*/
function constructResponseItem(
function constructResponseItemRecursive(
qItem: QuestionnaireItem,
qrItem: QuestionnaireResponseItem,
initialExpressions: Record<string, InitialExpression>,
Expand All @@ -117,48 +117,70 @@ function constructResponseItem(
// If qItem is a repeat group, populate instances of repeat groups containing child items
if (qItem.type === 'group' && qItem.repeats) {
// Create number of repeat group instances based on the number of answers that the first child item has
return constructRepeatGroupInstances(qItem, initialExpressions);
} else {
// Otherwise loop through qItem as usual
items.forEach((item) => {
const newQrItem = constructResponseItem(item, qrItem, initialExpressions, valueSetPromises);

if (Array.isArray(newQrItem)) {
qrItems.push(...newQrItem);
} else if (newQrItem) {
qrItems.push(newQrItem);
}
});
return constructRepeatGroupInstances(qItem, initialExpressions, valueSetPromises);
}

// Populate answers from initialExpressions if present
const initialExpression = initialExpressions[qItem.linkId];
let populatedAnswers: QuestionnaireResponseItemAnswer[] | undefined;
if (initialExpression) {
const initialValues = initialExpression.value;
// Otherwise loop through qItem as usual
items.forEach((item) => {
const newQrItem = constructResponseItemRecursive(
item,
qrItem,
initialExpressions,
valueSetPromises
);

if (Array.isArray(newQrItem)) {
qrItems.push(...newQrItem);
} else if (newQrItem) {
qrItems.push(newQrItem);
}
});

if (initialValues && initialValues.length) {
const { newValues, expandRequired } = getAnswerValues(initialValues, qItem);
populatedAnswers = newValues;
return constructGroupItem(qItem, qrItems, initialExpressions, valueSetPromises);
}

if (expandRequired && qItem.answerValueSet) {
const valueSetUrl = qItem.answerValueSet;
getValueSetPromise(qItem, valueSetUrl, valueSetPromises);
}
return constructSingleItem(qItem, initialExpressions, valueSetPromises);
}

function constructGroupItem(
qItem: QuestionnaireItem,
qrItems: QuestionnaireResponseItem[],
initialExpressions: Record<string, InitialExpression>,
valueSetPromises: Record<string, ValueSetPromise>
): QuestionnaireResponseItem | null {
// Populate answers from initialExpressions if present
const initialExpression = initialExpressions[qItem.linkId];
let populatedAnswers: QuestionnaireResponseItemAnswer[] | undefined;
if (initialExpression) {
const initialValues = initialExpression.value;

if (initialValues && initialValues.length) {
const { newValues, expandRequired } = getAnswerValues(initialValues, qItem);
populatedAnswers = newValues;

if (expandRequired && qItem.answerValueSet) {
const valueSetUrl = qItem.answerValueSet;
getValueSetPromise(qItem, valueSetUrl, valueSetPromises);
}
}

return qrItems.length > 0 || populatedAnswers
? {
linkId: qItem.linkId,
text: qItem.text,
item: qrItems,
...(qrItems.length > 0 ? { item: qrItems } : {}),
...(populatedAnswers ? { answer: populatedAnswers } : {})
}
: null;
}

return qrItems.length > 0 || populatedAnswers
? {
linkId: qItem.linkId,
text: qItem.text,
item: qrItems,
...(qrItems.length > 0 ? { item: qrItems } : {}),
...(populatedAnswers ? { answer: populatedAnswers } : {})
}
: null;
}

function constructSingleItem(
qItem: QuestionnaireItem,
initialExpressions: Record<string, InitialExpression>,
valueSetPromises: Record<string, ValueSetPromise>
): QuestionnaireResponseItem | null {
// Populate answers from initialExpressions if present
const initialExpression = initialExpressions[qItem.linkId];
if (initialExpression) {
Expand Down Expand Up @@ -253,7 +275,8 @@ export function checkIsDateTime(value: string): boolean {
*/
function constructRepeatGroupInstances(
qRepeatGroupParent: QuestionnaireItem,
initialExpressions: Record<string, InitialExpression>
initialExpressions: Record<string, InitialExpression>,
valueSetPromises: Record<string, ValueSetPromise>
): QuestionnaireResponseItem[] {
if (!qRepeatGroupParent.item) return [];

Expand All @@ -267,7 +290,13 @@ function constructRepeatGroupInstances(

const initialValues = initialExpression.value;
if (initialValues && initialValues.length > 0 && initialValues[0] !== '') {
const { newValues } = getAnswerValues(initialValues, childItem);
const { newValues, expandRequired } = getAnswerValues(initialValues, childItem);

if (expandRequired && childItem.answerValueSet) {
const valueSetUrl = childItem.answerValueSet;
getValueSetPromise(childItem, valueSetUrl, valueSetPromises);
}

childItemAnswers[i] = newValues;
} else {
childItemAnswers[i] = [];
Expand Down

0 comments on commit 2e03982

Please sign in to comment.