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

Move type checking to backend but make it more robust #345

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import FieldGrid from '../FieldGrid.tsx';
import DateField from './DateField.tsx';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';
import { checkStringIsDate } from '../../../../utils/parseInputs.ts';

interface DateItemProps
extends PropsWithQrItemChangeHandler<QuestionnaireResponseItem>,
Expand All @@ -48,16 +47,8 @@ function DateItem(props: DateItemProps) {
// Init input value
let dateString: string | null = null;

if (qrItem?.answer) {
if (qrItem?.answer[0].valueDate) {
dateString = qrItem.answer[0].valueDate;
}

if (qrItem?.answer[0].valueString) {
if (checkStringIsDate(qrItem.answer[0].valueString)) {
dateString = qrItem.answer[0].valueString;
}
}
if (qrItem?.answer && qrItem?.answer[0].valueDate) {
dateString = qrItem.answer[0].valueDate;
}
const dateDayJs = dateString ? dayjs(dateString) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import FieldGrid from '../FieldGrid.tsx';
import DateTimeField from './DateTimeField.tsx';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';
import { checkStringIsDateTime } from '../../../../utils/parseInputs.ts';

interface DateTimeItemProps
extends PropsWithQrItemChangeHandler<QuestionnaireResponseItem>,
Expand All @@ -47,16 +46,8 @@ function DateTimeItem(props: DateTimeItemProps) {

// Init input value
let dateTimeString: string | null = null;
if (qrItem?.answer) {
if (qrItem?.answer[0].valueDateTime) {
dateTimeString = qrItem.answer[0].valueDateTime;
}

if (qrItem?.answer[0].valueString) {
if (checkStringIsDateTime(qrItem.answer[0].valueString)) {
dateTimeString = qrItem.answer[0].valueString;
}
}
if (qrItem?.answer && qrItem?.answer[0].valueDateTime) {
dateTimeString = qrItem.answer[0].valueDateTime;
}
const dateTimeDayJs = dateTimeString ? dayjs(dateTimeString) : null;

Expand Down
13 changes: 0 additions & 13 deletions apps/smart-forms-app/src/features/renderer/utils/parseInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
* limitations under the License.
*/

import dayjs from 'dayjs';
import moment from 'moment';

export function parseValidInteger(input: string): number {
const validNumericString = parseValidNumericString(input);

Expand Down Expand Up @@ -59,13 +56,3 @@ export function parseDecimalStringWithPrecision(input: string, precision: number
export function parseDecimalStringToFloat(input: string, precision: number): number {
return parseFloat(parseFloat(input).toFixed(precision));
}

export function checkStringIsDate(value: string): boolean {
const acceptedFormats = ['YYYY', 'YYYY-MM', 'YYYY-MM-DD'];
return dayjs(value, acceptedFormats, true).isValid();
}

export function checkStringIsDateTime(value: string): boolean {
const acceptedFormats = ['YYYY-MM', 'YYYY-MM-DD', 'YYYY-MM-DDTHH:mm:ssZ'];
return moment(value, acceptedFormats, true).isValid();
}
2 changes: 2 additions & 0 deletions packages/sdc-populate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"author": "Sean Fong",
"license": "ISC",
"dependencies": {
"dayjs": "^1.11.7",
"moment": "^2.29.4",
"fhirclient": "^2.5.2",
"fhirpath": "^3.5.0"
},
Expand Down
74 changes: 64 additions & 10 deletions packages/sdc-populate/src/constructResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import type {
} from 'fhir/r4';
import type { InitialExpression, ValueSetPromise } from './interfaces/expressions.interface';
import { addValueSetAnswers, getValueSetPromise, resolvePromises } from './processValueSets';
import moment from 'moment';
import dayjs from 'dayjs';

/**
* Constructs a questionnaireResponse recursively from a specified questionnaire, its subject and its initialExpressions
Expand Down Expand Up @@ -221,24 +223,76 @@ function getAnswerValues(
if (answerOption) return answerOption;
}

if (typeof value === 'boolean') {
if (typeof value === 'boolean' && qItem.type === 'boolean') {
return { valueBoolean: value };
} else if (typeof value === 'object') {
return { valueCoding: value };
} else if (typeof value === 'number') {
return Number.isInteger(value) ? { valueInteger: value } : { valueDecimal: value };
} else {
// Process answerValueSets only if value is a string - so we don't make unnecessary $expand requests
if (qItem.answerValueSet && !qItem.answerValueSet.startsWith('#')) {
expandRequired = true;
}

if (typeof value === 'number') {
if (qItem.type === 'decimal') {
return { valueDecimal: value };
}
if (qItem.type === 'integer') {
return { valueInteger: value };
}
}

if (typeof value === 'object') {
return { valueCoding: value };
}

return { valueString: value };
// Value is string at this point
if (qItem.type === 'date' && checkIsDate(value)) {
return { valueDate: value };
}

if (qItem.type === 'dateTime' && checkIsDateTime(value)) {
return { valueDateTime: value };
}

if (qItem.type === 'time' && checkIsTime(value)) {
return { valueTime: value };
}

// Process answerValueSets only if value is a string - so we don't make unnecessary $expand requests
if (qItem.answerValueSet && !qItem.answerValueSet.startsWith('#')) {
expandRequired = true;
}

return { valueString: value };
});
return { newValues, expandRequired };
}

/**
* Check if an answer is a date in the formats YYYY, YYYY-MM, YYYY-MM-DD
*
* @author Sean Fong
*/
export function checkIsDate(value: string): boolean {
const acceptedFormats = ['YYYY-MM', 'YYYY-MM-DD'];
return dayjs(value, acceptedFormats, true).isValid();
}

/**
* Check if an answer is a datetime in the format YYYY, YYYY-MM, YYYY-MM-DD, YYYY-MM-DDThh:mm:ss+zz:zz
*
* @author Sean Fong
*/
export function checkIsDateTime(value: string): boolean {
const acceptedFormats = ['YYYY-MM', 'YYYY-MM-DD', 'YYYY-MM-DDTHH:mm:ssZ'];
return moment(value, acceptedFormats, true).isValid();
}

/**
* Check if an answer is in a time format - Regex: ([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?
*
* @author Sean Fong
*/
export function checkIsTime(value: string): boolean {
const timeRegex = /^([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?$/;
return timeRegex.test(value);
}

/**
* Constructed populated repeat group instances based on its child items' answers from initialExpressions
* Used with an itemPopulationContext extension at the parent-level and initialExpressions at the child-level
Expand Down
Loading