-
diff --git a/src/patients/patient-slice.ts b/src/patients/patient-slice.ts
index 2e98e75b89..1f4f9d7f1d 100644
--- a/src/patients/patient-slice.ts
+++ b/src/patients/patient-slice.ts
@@ -12,6 +12,7 @@ import Patient from '../model/Patient'
import RelatedPerson from '../model/RelatedPerson'
import { AppThunk } from '../store'
import { uuid } from '../util/uuid'
+import { cleanupPatient } from './util/set-patient-helper'
interface PatientState {
status: 'loading' | 'error' | 'completed'
@@ -35,8 +36,8 @@ interface Error {
prefix?: string
familyName?: string
preferredLanguage?: string
- email?: string
- phoneNumber?: string
+ emails?: (string | undefined)[]
+ phoneNumbers?: (string | undefined)[]
}
interface AddRelatedPersonError {
@@ -204,15 +205,33 @@ function validatePatient(patient: Patient) {
}
}
- if (patient.email) {
- if (!validator.isEmail(patient.email)) {
- error.email = 'patient.errors.invalidEmail'
+ if (patient.emails) {
+ const errors: (string | undefined)[] = []
+ patient.emails.forEach((email) => {
+ if (!validator.isEmail(email.value)) {
+ errors.push('patient.errors.invalidEmail')
+ } else {
+ errors.push(undefined)
+ }
+ })
+ // Only add to error obj if there's an error
+ if (errors.some((value) => value !== undefined)) {
+ error.emails = errors
}
}
- if (patient.phoneNumber) {
- if (!validator.isMobilePhone(patient.phoneNumber)) {
- error.phoneNumber = 'patient.errors.invalidPhoneNumber'
+ if (patient.phoneNumbers) {
+ const errors: (string | undefined)[] = []
+ patient.phoneNumbers.forEach((phoneNumber) => {
+ if (!validator.isMobilePhone(phoneNumber.value)) {
+ errors.push('patient.errors.invalidPhoneNumber')
+ } else {
+ errors.push(undefined)
+ }
+ })
+ // Only add to error obj if there's an error
+ if (errors.some((value) => value !== undefined)) {
+ error.phoneNumbers = errors
}
}
@@ -225,10 +244,11 @@ export const createPatient = (
): AppThunk => async (dispatch) => {
dispatch(createPatientStart())
- const newPatientError = validatePatient(patient)
+ const cleanPatient = cleanupPatient(patient)
+ const newPatientError = validatePatient(cleanPatient)
if (isEmpty(newPatientError)) {
- const newPatient = await PatientRepository.save(patient)
+ const newPatient = await PatientRepository.save(cleanPatient)
dispatch(createPatientSuccess())
if (onSuccess) {
@@ -245,9 +265,12 @@ export const updatePatient = (
onSuccess?: (patient: Patient) => void,
): AppThunk => async (dispatch) => {
dispatch(updatePatientStart())
- const updateError = validatePatient(patient)
+
+ const cleanPatient = cleanupPatient(patient)
+ const updateError = validatePatient(cleanPatient)
+
if (isEmpty(updateError)) {
- const updatedPatient = await PatientRepository.saveOrUpdate(patient)
+ const updatedPatient = await PatientRepository.saveOrUpdate(cleanPatient)
dispatch(updatePatientSuccess(updatedPatient))
if (onSuccess) {
diff --git a/src/patients/util/set-patient-helper.ts b/src/patients/util/set-patient-helper.ts
new file mode 100644
index 0000000000..641ef8c013
--- /dev/null
+++ b/src/patients/util/set-patient-helper.ts
@@ -0,0 +1,39 @@
+import Patient from '../../model/Patient'
+import { getPatientName } from './patient-name-util'
+
+/**
+ * Add full name. Get rid of empty phone numbers, emails, and addresses.
+ * @param patient
+ */
+const cleanupPatient = (patient: Patient) => {
+ const newPatient = { ...patient }
+
+ const { givenName, familyName, suffix } = patient
+ newPatient.fullName = getPatientName(givenName, familyName, suffix)
+
+ type cik = 'phoneNumbers' | 'emails' | 'addresses'
+ const contactInformationKeys: cik[] = ['phoneNumbers', 'emails', 'addresses']
+ contactInformationKeys.forEach((key) => {
+ if (key in newPatient) {
+ const nonEmpty = newPatient[key]
+ .filter(({ value }) => value.trim() !== '')
+ .map((entry) => {
+ const newValue = entry.value.trim()
+ if ('type' in entry) {
+ return { id: entry.id, value: newValue, type: entry.type }
+ }
+ return { id: entry.id, value: newValue }
+ })
+
+ if (nonEmpty.length > 0) {
+ newPatient[key] = nonEmpty
+ } else {
+ delete newPatient[key]
+ }
+ }
+ })
+
+ return newPatient
+}
+
+export { cleanupPatient }