diff --git a/src/scheduling/appointments/service/Appointments.tsx b/src/scheduling/appointments/service/Appointments.tsx
index 451c2a1df7..c57dda8725 100644
--- a/src/scheduling/appointments/service/Appointments.tsx
+++ b/src/scheduling/appointments/service/Appointments.tsx
@@ -1,5 +1,6 @@
import axios from 'axios'
import dotenv from 'dotenv'
+import Appointment from '../../../shared/model/Appointment'
dotenv.config()
const endpoint = `${process.env.REACT_APP_ENDPOINT}`
@@ -11,50 +12,223 @@ const config = {
},
}
-export const getAllPatients = () => {
+export const getAppointment = () => {
return axios
- .get(`${endpoint}/Patient?_count=10&_offset=1`, config)
+ .get(`${endpoint}/Appointment?practitioner=140857915539458`, config)
.then(async (response) => {
console.log(response)
const data = await response.data
console.log(data)
- return data
+ console.log(data.data.entry)
+ return data.data.entry
})
.catch((error) => {
console.log(error)
})
}
-export const getAppointment = () => {
+export const getAppointmentId = (id: number) => {
return axios
- .get(
- `${endpoint}/Appointment?departmentId=150&practiceId=195903&practitioner=140857915539458&patient=140919926030337`,
- config,
- )
+ .get(`${endpoint}/Appointment/${id}`, config)
.then(async (response) => {
console.log(response)
const data = await response.data
console.log(data)
- console.log(data.data.entry)
- return data.data.entry
+ console.log(data.data)
+ return data.data
})
.catch((error) => {
console.log(error)
})
}
-export const getPatientNameById = (id: number) => {
+export const createAppointment = (appointment: Appointment) => {
+ console.log(appointment)
+
+ const data = {
+ data: {
+ resourceType: 'Appointment',
+ start: new Date(appointment.start),
+ minutesDuration: appointment.minutesDuration,
+ description: '',
+ meta: {},
+ created: new Date(Date.now()),
+ status: appointment.status ? appointment.status : 'Scheduled',
+ participant: [
+ {
+ actor: {
+ reference: `Patient/${appointment.patientId}`,
+ },
+ },
+ {
+ actor: {
+ reference: 'Practitioner/140857915539458',
+ },
+ },
+ {
+ actor: {
+ reference: 'HealthcareService/140857911017476',
+ },
+ },
+ ],
+ extension: [
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/slot-type',
+ valueString: 'appointment',
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/appointment-mode',
+ valueString: 'IN_PERSON',
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/status',
+ extension: [
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/status',
+ valueString: `${appointment.status ? appointment.status : 'Scheduled'}`,
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/room',
+ valueString: null,
+ },
+ ],
+ },
+ ],
+ appointmentType: {
+ coding: [
+ {
+ system: 'https://hl7.org/fhir/v2/ValueSet/appointment-type',
+ code: `${
+ appointment.appointmentType ? appointment.appointmentType.text : 'Routine Visit'
+ }`,
+ display: `${
+ appointment.appointmentType ? appointment.appointmentType.text : 'Routine Visit'
+ }`,
+ },
+ ],
+ text: `${appointment.appointmentType ? appointment.appointmentType.text : 'Routine Visit'}`,
+ },
+ patientInstruction: null,
+ contained: [],
+ },
+ }
+
+ console.log(data)
+
return axios
- .get(`${endpoint}/Patient/${id}`, config)
+ .post(`${endpoint}/Appointment`, data, config)
.then(async (response) => {
console.log(response)
- const data = await response.data
- console.log(data)
- console.log(data.data.name[0].given.join(' '))
- const name = data.data.name[0].given.join(' ')
- return name
+ const data = response.data
+ console.log(data.data)
+ return data.data
+ })
+ .catch((error) => {
+ console.log(error)
+ return { id: 404, status: 'error' }
+ })
+}
+
+export const updateAppointment = (appointment: Appointment) => {
+ console.log(appointment)
+ const data = {
+ data: {
+ resourceType: 'Appointment',
+ start: new Date(appointment.start),
+ minutesDuration: appointment.minutesDuration,
+ description: '',
+ meta: {},
+ created: new Date(Date.now()),
+ status: appointment.status,
+ participant: [
+ {
+ actor: {
+ reference: `Patient/${
+ appointment.patientId
+ ? appointment.patientId
+ : appointment?.participant[0].actor.reference.substring(8)
+ }`,
+ },
+ },
+ {
+ actor: {
+ reference: 'Practitioner/140857915539458',
+ },
+ },
+ {
+ actor: {
+ reference: 'HealthcareService/140857911017476',
+ },
+ },
+ ],
+ extension: [
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/slot-type',
+ valueString: 'appointment',
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/appointment-mode',
+ valueString: 'IN_PERSON',
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/status',
+ extension: [
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/status',
+ valueString: `${appointment.status ? appointment.status : 'Scheduled'}`,
+ },
+ {
+ url: 'http://xcaliber-fhir/structureDefinition/room',
+ valueString: null,
+ },
+ ],
+ },
+ ],
+ appointmentType: {
+ coding: [
+ {
+ system: 'https://hl7.org/fhir/v2/ValueSet/appointment-type',
+ code: `${
+ appointment.appointmentType.text ? appointment.appointmentType.text : 'Routine visit'
+ }`,
+ display: `${
+ appointment.appointmentType.text ? appointment.appointmentType.text : 'Routine visit'
+ }`,
+ },
+ ],
+ text: `${
+ appointment.appointmentType.text ? appointment.appointmentType.text : 'Routine visit'
+ }`,
+ },
+ patientInstruction: null,
+ contained: [],
+ },
+ }
+
+ console.log(data)
+ return axios
+ .put(`${endpoint}/Appointment/${appointment.id}`, data, config)
+ .then(async (response) => {
+ console.log(response)
+ const data = response.data
+ console.log(data.data)
+ return data.data
+ })
+ .catch((error) => {
+ console.log(error)
+ return { id: 404, status: 'error' }
+ })
+}
+
+export const deleteAppointment = (id: number) => {
+ return axios
+ .delete(`${endpoint}/Appointment/${id}`, config)
+ .then((response) => {
+ console.log(response)
+ return 'success'
})
.catch((error) => {
console.log(error)
+ return 'error'
})
}
diff --git a/src/scheduling/appointments/service/Patients.tsx b/src/scheduling/appointments/service/Patients.tsx
new file mode 100644
index 0000000000..00cec176d2
--- /dev/null
+++ b/src/scheduling/appointments/service/Patients.tsx
@@ -0,0 +1,42 @@
+import axios from 'axios'
+import dotenv from 'dotenv'
+dotenv.config()
+
+const endpoint = `${process.env.REACT_APP_ENDPOINT}`
+
+const config = {
+ headers: {
+ Authorization: `${process.env.REACT_APP_AUTHORIZATION}`,
+ 'x-source-id': `${process.env.REACT_APP_XSOURCEID}`,
+ },
+}
+
+export const getAllPatients = () => {
+ return axios
+ .get(`${endpoint}/Patient`, config)
+ .then(async (response) => {
+ console.log(response)
+ const data = await response.data
+ console.log(data)
+ return data.data.entry
+ })
+ .catch((error) => {
+ console.log(error)
+ })
+}
+
+export const getPatientNameById = (id: number) => {
+ return axios
+ .get(`${endpoint}/Patient/${id}`, config)
+ .then(async (response) => {
+ console.log(response)
+ const data = await response.data
+ console.log(data)
+ console.log(data.data.name[0].given.join(' '))
+ const name = data.data.name[0].given.join(' ')
+ return name
+ })
+ .catch((error) => {
+ console.log(error)
+ })
+}
diff --git a/src/scheduling/appointments/util/scheduling-appointment.util.ts b/src/scheduling/appointments/util/scheduling-appointment.util.ts
index ac049e500f..2f6145f8a6 100644
--- a/src/scheduling/appointments/util/scheduling-appointment.util.ts
+++ b/src/scheduling/appointments/util/scheduling-appointment.util.ts
@@ -17,9 +17,11 @@ export function getAppointmentLabel(appointment: Appointment | undefined) {
return ''
}
- const { id, startDateTime, endDateTime } = appointment
+ const { id, start,minutesDuration} = appointment
- return startDateTime && endDateTime
- ? `${toLocaleString(new Date(startDateTime))} - ${toLocaleString(new Date(endDateTime))}`
+ return start
+ ? `${toLocaleString(new Date(start))} - ${toLocaleString(new Date(
+ new Date(start).setMinutes(new Date(start).getMinutes() + minutesDuration),
+ ))}`
: id
}
diff --git a/src/scheduling/appointments/util/validate-appointment.ts b/src/scheduling/appointments/util/validate-appointment.ts
index b490b5ca43..f47a8af231 100644
--- a/src/scheduling/appointments/util/validate-appointment.ts
+++ b/src/scheduling/appointments/util/validate-appointment.ts
@@ -18,10 +18,12 @@ export class AppointmentError extends Error {
export default function validateAppointment(appointment: Appointment): AppointmentError {
const newError: any = {}
- if (!appointment.patient) {
+ if (!appointment.patientId) {
newError.patient = 'scheduling.appointment.errors.patientRequired'
}
- if (isBefore(new Date(appointment.endDateTime), new Date(appointment.startDateTime))) {
+ if (isBefore(new Date(
+ new Date(appointment.start).setMinutes(new Date(appointment.start).getMinutes() + appointment.minutesDuration),
+ ), new Date(appointment.start))) {
newError.startDateTime = 'scheduling.appointment.errors.startDateMustBeBeforeEndDate'
}
diff --git a/src/scheduling/appointments/view/ViewAppointment.tsx b/src/scheduling/appointments/view/ViewAppointment.tsx
index 23b5518ecb..947ded3065 100644
--- a/src/scheduling/appointments/view/ViewAppointment.tsx
+++ b/src/scheduling/appointments/view/ViewAppointment.tsx
@@ -2,18 +2,24 @@ import { Spinner, Button, Modal, Toast } from '@hospitalrun/components'
import React, { useCallback, useEffect, useState } from 'react'
import { useSelector } from 'react-redux'
import { useHistory, useParams } from 'react-router-dom'
-
import useAddBreadcrumbs from '../../../page-header/breadcrumbs/useAddBreadcrumbs'
import { useButtonToolbarSetter } from '../../../page-header/button-toolbar/ButtonBarProvider'
import { useUpdateTitle } from '../../../page-header/title/TitleContext'
-import usePatient from '../../../patients/hooks/usePatient'
+import Loading from '../../../shared/components/Loading'
+// import usePatient from '../../../patients/hooks/usePatient'
import useTranslator from '../../../shared/hooks/useTranslator'
+import Appointment from '../../../shared/model/Appointment'
+import Patient from '../../../shared/model/Patient'
import Permissions from '../../../shared/model/Permissions'
import { RootState } from '../../../shared/store'
-import useAppointment from '../../hooks/useAppointment'
-import useDeleteAppointment from '../../hooks/useDeleteAppointment'
+// import useAppointment from '../../hooks/useAppointment'
+// import useDeleteAppointment from '../../hooks/useDeleteAppointment'
import AppointmentDetailForm from '../AppointmentDetailForm'
+import { deleteAppointment, getAppointmentId } from '../service/Appointments'
+import { getPatientNameById } from '../service/Patients'
import { getAppointmentLabel } from '../util/scheduling-appointment.util'
+// import { getAppointmentLabel } from '../util/scheduling-appointment.util'
+// import { Appointment } from '../ViewAppointments'
const ViewAppointment = () => {
const { t } = useTranslator()
@@ -27,13 +33,43 @@ const ViewAppointment = () => {
const { id } = useParams
()
const history = useHistory()
- const [deleteMutate] = useDeleteAppointment()
+ // const [deleteMutate] = useDeleteAppointment()
const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false)
const setButtonToolBar = useButtonToolbarSetter()
const { permissions } = useSelector((state: RootState) => state.user)
- const { data: appointment } = useAppointment(id)
- const { data: patient } = usePatient(appointment ? appointment.patient : id)
+ // const { data: appointment } = useAppointment(id)
+
+ const [appointment, setAppointment] = useState()
+ const [patientName, setPatientName] = useState()
+ const [isLoading, setIsLoading] = useState(true)
+
+ const appointmentFunc = async () => {
+ setAppointment(await getAppointmentId(id))
+ setIsLoading(false)
+ }
+
+ const patientFunc = async () => {
+ setPatientName(
+ await getPatientNameById(
+ parseInt(String(appointment?.participant[0].actor.reference.substr(8))),
+ ),
+ )
+ }
+
+ useEffect(() => {
+ console.log(id)
+ appointmentFunc()
+ }, [])
+
+ useEffect(() => {
+ if (appointment) {
+ console.log('view appointment', appointment)
+ patientFunc()
+ }
+ }, [appointment])
+
+ // const { data: patient } = usePatient(appointment ? appointment.patient : id)
const breadcrumbs = [
{ i18nKey: 'scheduling.appointments.label', location: '/appointments' },
{ text: appointment ? getAppointmentLabel(appointment) : '', location: `/patients/${id}` },
@@ -45,15 +81,27 @@ const ViewAppointment = () => {
setShowDeleteConfirmation(true)
}
- const onDeleteConfirmationButtonClick = () => {
+ const onDeleteConfirmationButtonClick = async () => {
if (!appointment) {
return
}
- deleteMutate({ appointmentId: appointment.id }).then(() => {
+ console.log(appointment)
+
+ // deleteMutate({ appointmentId: appointment.id }).then(() => {
+ // history.push('/appointments')
+ // Toast('success', t('states.success'), t('scheduling.appointment.successfullyDeleted'))
+ // })
+
+ let status = await deleteAppointment(parseInt(appointment.id))
+
+ if (status === 'success') {
history.push('/appointments')
Toast('success', t('states.success'), t('scheduling.appointment.successfullyDeleted'))
- })
+ } else {
+ Toast('error', t('states.error'), 'Could not delete appointment')
+ }
+
setShowDeleteConfirmation(false)
}
@@ -99,11 +147,19 @@ const ViewAppointment = () => {
}
}, [getButtons, setButtonToolBar])
+ if (isLoading || appointment === undefined) {
+ return
+ }
+
return (
<>
- {patient && appointment ? (
+ {patientName && appointment ? (
-
+
void
@@ -28,8 +28,9 @@ const DateTimePickerWithLabelFormGroup = (props: Props) => {
isInvalid={isInvalid}
feedback={feedback}
onChange={(inputDate) => {
+ console.log('date time picker', inputDate)
if (onChange) {
- onChange(inputDate)
+ onChange(new Date(inputDate))
}
}}
showTimeSelect
@@ -39,6 +40,8 @@ const DateTimePickerWithLabelFormGroup = (props: Props) => {
withPortal={false}
minDate={moment().toDate()}
maxDate={moment().add(3, 'months').toDate()}
+ minTime={moment().toDate()}
+ maxTime={new Date(new Date().setHours(19, 0, 0, 0))}
/>
)
diff --git a/src/shared/db/AppointmentRepository.ts b/src/shared/db/AppointmentRepository.ts
index 36adcea156..bfd161a80d 100644
--- a/src/shared/db/AppointmentRepository.ts
+++ b/src/shared/db/AppointmentRepository.ts
@@ -1,10 +1,10 @@
import escapeStringRegexp from 'escape-string-regexp'
import { relationalDb } from '../config/pouchdb'
-import Appointment from '../model/Appointment'
+import AppointmentDB from '../model/Appointment'
import Repository from './Repository'
-class AppointmentRepository extends Repository {
+class AppointmentRepository extends Repository {
constructor() {
super('appointment', relationalDb)
this.db.createIndex({
@@ -15,7 +15,7 @@ class AppointmentRepository extends Repository {
}
// Fuzzy search for patient appointments. Used for patient appointment search bar
- async searchPatientAppointments(patientId: string, text: string): Promise {
+ async searchPatientAppointments(patientId: string, text: string): Promise {
const escapedString = escapeStringRegexp(text)
return super.search({
selector: {
diff --git a/src/shared/model/Appointment.ts b/src/shared/model/Appointment.ts
index bdd6791c77..d5e3552541 100644
--- a/src/shared/model/Appointment.ts
+++ b/src/shared/model/Appointment.ts
@@ -1,6 +1,6 @@
import AbstractDBModel from './AbstractDBModel'
-export default interface Appointment extends AbstractDBModel {
+export default interface AppointmentDB extends AbstractDBModel {
startDateTime: string
endDateTime: string
patient: string
@@ -8,3 +8,21 @@ export default interface Appointment extends AbstractDBModel {
reason: string
type: string
}
+
+export default interface Appointment {
+ appointmentType: {
+ text: string
+ }
+ participant: {
+ actor: {
+ reference: string
+ }
+ }[]
+ id: string
+ start: string
+ end:string
+ minutesDuration: number
+ status: string
+ patientId:string
+}
+