Skip to content

Commit

Permalink
update genereate response function
Browse files Browse the repository at this point in the history
  • Loading branch information
ruberino authored and andreasnp committed Dec 3, 2024
1 parent d801c9c commit fd9bb64
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
74 changes: 72 additions & 2 deletions src/actions/generateQuestionnaireResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import Constants from '../constants/index';
import itemType from '../constants/itemType';
import StatusConstants from '../constants/status';
import { createQuestionnaireResponseAnswer } from '../util/createQuestionnaireResponseAnswer';
import { getMinOccursExtensionValue } from '../util/extension';
import { getCalculatedExpressionExtension, getCopyExtension, getMinOccursExtensionValue } from '../util/extension';
import { evaluateFhirpathExpressionToGetString } from '@/util/fhirpathHelper';
import ItemType from '../constants/itemType';

export function generateQuestionnaireResponse(questionnaire: Questionnaire): QuestionnaireResponse | undefined {
const response: QuestionnaireResponse = {
Expand All @@ -31,7 +33,7 @@ export function generateQuestionnaireResponse(questionnaire: Questionnaire): Que
response.item.push(responseItem);
}
});
return response;
return evaluateCalculatedExpressions(questionnaire, response);
}

function addChildrenItemsToResponseItem(item: QuestionnaireItem, response: QuestionnaireResponseItem): void {
Expand Down Expand Up @@ -99,3 +101,71 @@ export function createQuestionnaireResponseItem(item: QuestionnaireItem): Questi

return responseItem;
}
function evaluateCalculatedExpressions(questionnaire: Questionnaire, response: QuestionnaireResponse): QuestionnaireResponse {
function traverseItems(qItems: QuestionnaireItem[], qrItems: QuestionnaireResponseItem[]): void {
qItems.forEach((qItem, index) => {
const qrItem = qrItems[index];
const expressionToEvaluate = getCopyExtension(qItem) ?? getCalculatedExpressionExtension(qItem);
if (expressionToEvaluate && expressionToEvaluate.valueString) {
const result = evaluateFhirpathExpressionToGetString(expressionToEvaluate, response);
if (result.length > 0) {
const calculatedValue = result[0];
const answer = qrItem.answer ? qrItem.answer[0] : {};
switch (qItem.type) {
case ItemType.BOOLEAN:
answer.valueBoolean = Boolean(calculatedValue);
break;
case ItemType.DECIMAL:
answer.valueDecimal = Number(calculatedValue);
break;
case ItemType.INTEGER:
answer.valueInteger = Number(calculatedValue);
break;
case ItemType.QUANTITY:
answer.valueQuantity = calculatedValue;
break;
case ItemType.DATE:
answer.valueDate = String(calculatedValue);
break;
case ItemType.DATETIME:
answer.valueDateTime = String(calculatedValue);
break;
case ItemType.TIME:
answer.valueTime = String(calculatedValue);
break;
case ItemType.TEXT:
case ItemType.STRING:
answer.valueString = String(calculatedValue);
break;
case ItemType.ATTATCHMENT:
answer.valueAttachment = calculatedValue;
break;
case ItemType.CHOICE:
case ItemType.OPENCHOICE:
answer.valueCoding = calculatedValue;
break;
default:
if (typeof answer === 'string') {
(answer as { valueString: string }).valueString = calculatedValue;
} else {
answer.valueCoding = calculatedValue;
}
break;
}
qrItem.answer = [answer];
}
}

// Recursively process child items
if (qItem.item && qrItem.item) {
traverseItems(qItem.item, qrItem.item);
}
});
}

if (questionnaire.item && response.item) {
traverseItems(questionnaire.item, response.item);
}

return response; // Return the updated response
}
13 changes: 6 additions & 7 deletions src/hooks/useGetAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ function getAnswerIfDataReceiver(
item: QuestionnaireItem,
extension: Extension
): QuestionnaireResponseItemAnswer | QuestionnaireResponseItemAnswer[] | undefined {
let result = evaluateFhirpathExpressionToGetString(extension, questionnaireResponse);
const result = evaluateFhirpathExpressionToGetString(extension, questionnaireResponse);
const processedResult = getCalculatedExpressionExtension(item) ? result.map((res: any) => res?.value ?? res) : result;

if (getCalculatedExpressionExtension(item)) {
result = result.map((m: any) => m.value as number);
}

return getQuestionnaireResponseItemAnswer(item.type, result);
return getQuestionnaireResponseItemAnswer(item.type, processedResult);
}

function getQuestionnaireResponseItemAnswer(
Expand Down Expand Up @@ -70,7 +67,9 @@ export const useGetAnswer = (
);
const dataRecieverExtension = item && getCopyExtension(item);

return dataRecieverExtension
const answer = dataRecieverExtension
? getAnswerIfDataReceiver(questionnaireResponse, item, dataRecieverExtension)
: getAnswerFromResponseItem(responseItem);

return answer;
};

0 comments on commit fd9bb64

Please sign in to comment.