Skip to content

Commit

Permalink
code reviews changes
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldkibet committed Oct 15, 2024
1 parent 2e3567e commit 700df52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export interface LabOrderConcept {
mappings?: Array<Mapping>;
answers?: Array<OpenmrsResource>;
setMembers?: Array<LabOrderConcept>;
hiNormal?: number;
hiAbsolute?: number;
hiCritical?: number;
lowNormal?: number;
lowAbsolute?: number;
lowCritical?: number;
hiNormal?: number | null | undefined;
hiAbsolute?: number | null | undefined;
hiCritical?: number | null | undefined;
lowNormal?: number | null | undefined;
lowAbsolute?: number | null | undefined;
lowCritical?: number | null | undefined;
units?: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ type SchemaRecord = Record<string, z.ZodType>;
export const useLabResultsFormSchema = (labOrderConceptUuid: string) => {
const { concept, isLoading: isLoadingConcept } = useOrderConceptByUuid(labOrderConceptUuid);

if (isLoadingConcept) {
if (isLoadingConcept || !concept) {
if (!concept) {
console.warn(`Couldn't load concept ${labOrderConceptUuid}`);
}
return z.object({});
}

Expand Down Expand Up @@ -89,8 +92,8 @@ const createSchema = (labOrderConcept: LabOrderConcept): z.ZodType => {
*/
const createNumericSchema = (
labOrderConcept: LabOrderConcept,
upperLimit: number | undefined,
lowerLimit: number | undefined,
upperLimit: number | null | undefined,
lowerLimit: number | null | undefined,
): z.ZodType => {
let baseSchema = z
.preprocess((val) => {
Expand All @@ -102,23 +105,28 @@ const createNumericSchema = (
message: `${labOrderConcept.display} must be a valid number`,
});

if (lowerLimit === null && upperLimit === null) {
const hasLowerLimit = lowerLimit !== null && lowerLimit !== undefined;
const hasUpperLimit = upperLimit !== null && upperLimit !== undefined;

if (!hasLowerLimit && !hasUpperLimit) {
return baseSchema;
}

if (lowerLimit !== null && upperLimit !== null) {
if (hasLowerLimit && hasUpperLimit) {
return baseSchema.refine((val) => val === undefined || (val >= lowerLimit && val <= upperLimit), {
message: `${labOrderConcept.display} must be between ${lowerLimit} and ${upperLimit}`,
});
}

if (lowerLimit !== null) {
if (hasLowerLimit) {
return baseSchema.refine((val) => val === undefined || val >= lowerLimit, {
message: `${labOrderConcept.display} must be greater than or equal to ${lowerLimit}`,
});
}

return baseSchema.refine((val) => val === undefined || val <= upperLimit!, {
message: `${labOrderConcept.display} must be less than or equal to ${upperLimit}`,
});
if (hasUpperLimit) {
return baseSchema.refine((val) => val === undefined || val <= upperLimit, {
message: `${labOrderConcept.display} must be less than or equal to ${upperLimit}`,
});
}
};

0 comments on commit 700df52

Please sign in to comment.