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

Update appointments #35

Merged
merged 4 commits into from
Aug 24, 2024
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
52 changes: 6 additions & 46 deletions app/(dashboard)/patients/[id]/AppointmentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import { type ComponentProps } from 'react'
import { z } from 'zod'
import { type Appointment } from '@/app/(dashboard)/patients/utils'
import {
FHIRAppointmentStatus,
stringifyAppointmentStatus,
} from '@/modules/firebase/models/medication'
import { Button } from '@/packages/design-system/src/components/Button'
import { DatePicker } from '@/packages/design-system/src/components/DatePicker'
import {
Expand All @@ -21,23 +17,16 @@
DialogHeader,
DialogTitle,
} from '@/packages/design-system/src/components/Dialog'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/packages/design-system/src/components/Select'
import { Input } from '@/packages/design-system/src/components/Input'

Check warning on line 20 in app/(dashboard)/patients/[id]/AppointmentForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/AppointmentForm.tsx#L20

Added line #L20 was not covered by tests
import { Textarea } from '@/packages/design-system/src/components/Textarea'
import { Field } from '@/packages/design-system/src/forms/Field'
import { useForm } from '@/packages/design-system/src/forms/useForm'

export const appointmentFormSchema = z.object({
status: z.nativeEnum(FHIRAppointmentStatus),
start: z.date(),
end: z.date(),
comment: z.string().nullable(),
patientInstruction: z.string().nullable(),
providerName: z.string(),
})

export type AppointmentFormSchema = z.infer<typeof appointmentFormSchema>
Expand All @@ -55,11 +44,10 @@
const form = useForm({
formSchema: appointmentFormSchema,
defaultValues: {
status: appointment?.status,
start: appointment ? new Date(appointment.start) : undefined,
end: appointment ? new Date(appointment.end) : undefined,
comment: appointment?.comment ?? null,
patientInstruction: appointment?.patientInstruction ?? null,
providerName: appointment?.providerName ?? '',
},
})

Expand All @@ -69,25 +57,6 @@

return (
<form onSubmit={handleSubmit}>
<Field
control={form.control}
name="status"
label="Status"
render={({ field }) => (
<Select onValueChange={field.onChange} {...field}>
<SelectTrigger>
<SelectValue placeholder="Status" />
</SelectTrigger>
<SelectContent>
{Object.values(FHIRAppointmentStatus).map((status) => (
<SelectItem key={status} value={status}>
{stringifyAppointmentStatus(status)}
</SelectItem>
))}
</SelectContent>
</Select>
)}
/>
<Field
control={form.control}
name="start"
Expand All @@ -105,18 +74,9 @@
/>
<Field
control={form.control}
name="end"
label="End"
render={({ field }) => (
<DatePicker
mode="single"
selected={field.value}
onSelect={(date) => field.onChange(date)}
defaultMonth={field.value}
fromDate={new Date()}
showTimePicker
/>
)}
name="providerName"
label="Provider"
render={({ field }) => <Input {...field} />}

Check warning on line 79 in app/(dashboard)/patients/[id]/AppointmentForm.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/AppointmentForm.tsx#L79

Added line #L79 was not covered by tests
/>
<Field
control={form.control}
Expand Down
11 changes: 3 additions & 8 deletions app/(dashboard)/patients/[id]/Appointments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
AppointmentsData,
Appointment,
} from '@/app/(dashboard)/patients/utils'
import { stringifyAppointmentStatus } from '@/modules/firebase/models/medication'
import { Button } from '@/packages/design-system/src/components/Button'
import {
DataTable,
Expand All @@ -42,18 +41,14 @@
header: 'Created',
cell: dateColumn,
}),
columnHelper.accessor('status', {
header: 'Status',
cell: (props) => stringifyAppointmentStatus(props.getValue()),
columnHelper.accessor('providerName', {
header: 'Provider name',
cell: (props) => props.getValue(),

Check warning on line 46 in app/(dashboard)/patients/[id]/Appointments.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/Appointments.tsx#L46

Added line #L46 was not covered by tests
}),
columnHelper.accessor('start', {
header: 'Start',
cell: dateTimeColumn,
}),
columnHelper.accessor('end', {
header: 'End',
cell: dateTimeColumn,
}),
columnHelper.display({
id: 'actions',
cell: (props) => (
Expand Down
28 changes: 23 additions & 5 deletions app/(dashboard)/patients/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
//
'use server'
import { addDoc, deleteDoc, setDoc } from '@firebase/firestore'
import { addHours } from 'date-fns'

Check warning on line 10 in app/(dashboard)/patients/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/actions.tsx#L10

Added line #L10 was not covered by tests
import { revalidatePath } from 'next/cache'
import { type AllergyFormSchema } from '@/app/(dashboard)/patients/[id]/AllergyForm'
import { type AppointmentFormSchema } from '@/app/(dashboard)/patients/[id]/AppointmentForm'
import { type LabFormSchema } from '@/app/(dashboard)/patients/[id]/LabForm'
import { getUnitOfObservationType } from '@/app/(dashboard)/patients/clientUtils'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'
import { AllergyType } from '@/modules/firebase/models/allergy'
import { ExtensionURL } from '@/modules/firebase/models/baseTypes'

Check warning on line 18 in app/(dashboard)/patients/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/actions.tsx#L18

Added line #L18 was not covered by tests
import {
FHIRAllergyIntoleranceCriticality,
FHIRAllergyIntoleranceType,
FHIRAppointmentStatus,
FHIRObservationStatus,
} from '@/modules/firebase/models/medication'
import {
Expand Down Expand Up @@ -193,13 +196,28 @@
return 'success'
}

const getAppointmentData = (payload: AppointmentFormSchema) => ({
status: payload.status,
const getAppointmentData = (

Check warning on line 199 in app/(dashboard)/patients/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/actions.tsx#L199

Added line #L199 was not covered by tests
payload: AppointmentFormSchema & { userId: string },
) => ({

Check warning on line 201 in app/(dashboard)/patients/actions.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/actions.tsx#L201

Added line #L201 was not covered by tests
status: FHIRAppointmentStatus.booked,
start: payload.start.toISOString(),
end: payload.end.toISOString(),
end: addHours(payload.start, 1).toISOString(),
comment: payload.comment,
patientInstruction: payload.patientInstruction,
participant: [],
extension: [
{ url: ExtensionURL.providerName, valueString: payload.providerName },
],
participant: [
{
actor: {
display: null,
type: null,
identifier: null,
reference: `users/${payload.userId}`,
},
type: null,
},
],
})

export const createAppointment = async (
Expand Down Expand Up @@ -238,7 +256,7 @@
appointmentId: payload.appointmentId,
}),
getAppointmentData(payload),
{ mergeFields: ['created'] },
{ merge: true },
)
revalidatePath(routes.patients.patient(payload.userId))
return 'success'
Expand Down
9 changes: 8 additions & 1 deletion app/(dashboard)/patients/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { query, where } from 'firebase/firestore'
import { getAuthenticatedOnlyApp } from '@/modules/firebase/guards'
import { AllergyType } from '@/modules/firebase/models/allergy'
import { ExtensionURL } from '@/modules/firebase/models/baseTypes'

Check warning on line 12 in app/(dashboard)/patients/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/utils.ts#L12

Added line #L12 was not covered by tests
import {
type FHIRAllergyIntolerance,
FHIRAllergyIntoleranceCriticality,
Expand Down Expand Up @@ -205,9 +206,15 @@
resourceType: ResourceType
}) => {
const { refs } = await getAuthenticatedOnlyApp()
const appointments = await getDocsData(
const rawAppointments = await getDocsData(

Check warning on line 209 in app/(dashboard)/patients/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/utils.ts#L209

Added line #L209 was not covered by tests
refs.appointments({ userId, resourceType }),
)
const appointments = rawAppointments.map((appointment) => ({

Check warning on line 212 in app/(dashboard)/patients/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/utils.ts#L212

Added line #L212 was not covered by tests
...appointment,
providerName: appointment.extension?.find(
(extension) => extension.url === (ExtensionURL.providerName as string),
)?.valueString,

Check warning on line 216 in app/(dashboard)/patients/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/utils.ts#L215-L216

Added lines #L215 - L216 were not covered by tests
}))
return { appointments, userId, resourceType }
}

Expand Down
13 changes: 9 additions & 4 deletions modules/firebase/models/baseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
valueQuantities?: FHIRSimpleQuantity[]
valueReference?: FHIRReference<unknown>
valueMedicationRequest?: FHIRMedicationRequest
valueString?: string
}

export enum ExtensionURL {
providerName = 'http://engagehf.bdh.stanford.edu/fhir/StructureDefinition/Appointment/extension/providerName',

Check warning on line 38 in modules/firebase/models/baseTypes.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/models/baseTypes.ts#L38

Added line #L38 was not covered by tests
}

export interface FHIRPeriod {
Expand All @@ -53,10 +58,10 @@
//
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface FHIRReference<T> {
reference?: string
type?: string
identifier?: string
display?: string
reference?: string | null
type?: string | null
identifier?: string | null
display?: string | null
}

export interface FHIRSimpleQuantity {
Expand Down
7 changes: 2 additions & 5 deletions modules/firebase/models/medication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
//
// SPDX-License-Identifier: MIT
//

import { startCase } from 'es-toolkit'
import {
type FHIRSimpleQuantity,
type FHIRCodeableConcept,
Expand All @@ -15,6 +13,7 @@ import {
type FHIRReference,
type FHIRPeriod,
type FHIRResource,
type FHIRExtension,
} from './baseTypes.js'

export interface FHIRMedication extends FHIRElement {
Expand Down Expand Up @@ -125,10 +124,8 @@ export enum FHIRAppointmentStatus {
waitlist = 'waitlist',
}

export const stringifyAppointmentStatus = (status: FHIRAppointmentStatus) =>
startCase(status)

export interface FHIRAppointment {
extension?: FHIRExtension[]
status: FHIRAppointmentStatus
created: string
start: string
Expand Down
Loading