Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
refactor: types for add care plan and add allergies
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Aug 20, 2020
1 parent dc2c607 commit 6f74103
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/patients/care-plans/AddCarePlanModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('Add Care Plan Modal', () => {
})

it('should save care plan when the save button is clicked and close', async () => {
const expectedCreatedDate = new Date()
Date.now = jest.fn().mockReturnValue(expectedCreatedDate)
const expectedCarePlan = {
id: '123',
title: 'some title',
Expand All @@ -70,6 +72,7 @@ describe('Add Care Plan Modal', () => {
endDate: new Date().toISOString(),
status: CarePlanStatus.Active,
intent: CarePlanIntent.Proposal,
createdOn: expectedCreatedDate,
}

const { wrapper } = setup()
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/patients/hooks/useAddCarePlan.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('use add care plan', () => {
})

it('should add a care plan to the patient', async () => {
const expectedCreatedDate = new Date()
Date.now = jest.fn().mockReturnValue(expectedCreatedDate)
const expectedCarePlan: CarePlan = {
id: 'some id',
description: 'some description',
Expand All @@ -21,7 +23,7 @@ describe('use add care plan', () => {
title: 'some title',
intent: CarePlanIntent.Option,
status: CarePlanStatus.Active,
createdOn: new Date().toISOString(),
createdOn: expectedCreatedDate.toISOString(),
diagnosisId: 'someDiagnosis',
note: 'some note',
}
Expand Down
3 changes: 1 addition & 2 deletions src/patients/allergies/NewAllergyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useState, useEffect } from 'react'

import TextInputWithLabelFormGroup from '../../shared/components/input/TextInputWithLabelFormGroup'
import useTranslator from '../../shared/hooks/useTranslator'
import Allergy from '../../shared/model/Allergy'
import useAddAllergy from '../hooks/useAddAllergy'
import { AllergyError } from '../util/validate-allergy'

Expand Down Expand Up @@ -32,7 +31,7 @@ const NewAllergyModal = (props: NewAllergyModalProps) => {

const onSaveButtonClick = async () => {
try {
await mutate({ patientId, allergy: allergy as Allergy })
await mutate({ patientId, allergy })
onCloseButtonClick()
} catch (e) {
setAllergyError(e)
Expand Down
6 changes: 4 additions & 2 deletions src/patients/care-plans/AddCarePlanModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addMonths } from 'date-fns'
import React, { useState, useEffect } from 'react'

import useTranslator from '../../shared/hooks/useTranslator'
import CarePlan from '../../shared/model/CarePlan'
import CarePlan, { CarePlanIntent, CarePlanStatus } from '../../shared/model/CarePlan'
import Patient from '../../shared/model/Patient'
import useAddCarePlan from '../hooks/useAddCarePlan'
import { CarePlanError } from '../util/validate-careplan'
Expand All @@ -22,6 +22,8 @@ const initialCarePlanState = {
endDate: addMonths(new Date(), 1).toISOString(),
note: '',
diagnosisId: '',
status: CarePlanStatus.Active,
intent: CarePlanIntent.Plan,
}

const AddCarePlanModal = (props: Props) => {
Expand All @@ -45,7 +47,7 @@ const AddCarePlanModal = (props: Props) => {

const onSaveButtonClick = async () => {
try {
await mutate({ patientId: patient.id, carePlan: carePlan as CarePlan })
await mutate({ patientId: patient.id, carePlan })
onClose()
} catch (e) {
setCarePlanError(e)
Expand Down
4 changes: 2 additions & 2 deletions src/patients/hooks/useAddAllergy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import validateAllergy from '../util/validate-allergy'

interface AddAllergyRequest {
patientId: string
allergy: Allergy
allergy: Omit<Allergy, 'id'>
}

async function addAllergy(request: AddAllergyRequest): Promise<Allergy[]> {
Expand All @@ -17,7 +17,7 @@ async function addAllergy(request: AddAllergyRequest): Promise<Allergy[]> {
if (isEmpty(error)) {
const patient = await PatientRepository.find(request.patientId)
const allergies = patient.allergies ? [...patient.allergies] : []
const newAllergy = {
const newAllergy: Allergy = {
id: uuid(),
...request.allergy,
}
Expand Down
5 changes: 3 additions & 2 deletions src/patients/hooks/useAddCarePlan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import validateCarePlan from '../util/validate-careplan'

interface AddCarePlanRequest {
patientId: string
carePlan: CarePlan
carePlan: Omit<CarePlan, 'id' | 'createdOn'>
}

async function addCarePlan(request: AddCarePlanRequest): Promise<CarePlan[]> {
Expand All @@ -18,8 +18,9 @@ async function addCarePlan(request: AddCarePlanRequest): Promise<CarePlan[]> {
const patient = await PatientRepository.find(request.patientId)
const carePlans = patient.carePlans ? [...patient.carePlans] : []

const newCarePlan = {
const newCarePlan: CarePlan = {
id: uuid(),
createdOn: new Date(Date.now()).toISOString(),
...request.carePlan,
}
carePlans.push(newCarePlan)
Expand Down
2 changes: 1 addition & 1 deletion src/patients/util/validate-allergy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AllergyError extends Error {
}
}

export default function validateAllergy(allergy: Allergy) {
export default function validateAllergy(allergy: Partial<Allergy>) {
const error: any = {}
if (!allergy.name) {
error.nameError = 'patient.allergies.error.nameRequired'
Expand Down
2 changes: 1 addition & 1 deletion src/patients/util/validate-careplan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CarePlanError extends Error {
}
}

export default function validateCarePlan(carePlan: CarePlan): CarePlanError {
export default function validateCarePlan(carePlan: Partial<CarePlan>): CarePlanError {
const error = {} as CarePlanError

if (!carePlan.title) {
Expand Down

1 comment on commit 6f74103

@vercel
Copy link

@vercel vercel bot commented on 6f74103 Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.