-
Notifications
You must be signed in to change notification settings - Fork 125
/
newAppointmentFlow.js
319 lines (284 loc) · 8.3 KB
/
newAppointmentFlow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import * as Sentry from '@sentry/browser';
import {
getFormData,
getNewAppointment,
getEligibilityStatus,
vaosCommunityCare,
getTypeOfCare,
getClinicsForChosenFacility,
} from './utils/selectors';
import { FACILITY_TYPES, FLOW_TYPES, TYPES_OF_CARE } from './utils/constants';
import {
getCommunityCare,
getSystemIdentifiers,
getLongTermAppointmentHistory,
getSitesSupportingVAR,
} from './api';
import {
showTypeOfCareUnavailableModal,
startDirectScheduleFlow,
startRequestAppointmentFlow,
updateFacilityType,
updateCCEnabledSystems,
updateCCEligibility,
} from './actions/newAppointment';
const AUDIOLOGY = '203';
const SLEEP_CARE = 'SLEEP';
const PODIATRY = 'tbd-podiatry';
function isCCAudiology(state) {
return (
getFormData(state).facilityType === FACILITY_TYPES.COMMUNITY_CARE &&
getFormData(state).typeOfCareId === AUDIOLOGY
);
}
function isCommunityCare(state) {
return TYPES_OF_CARE.find(
typeOfCare =>
typeOfCare.id === getFormData(state).typeOfCareId && typeOfCare.ccId,
);
}
function isCCFacility(state) {
return getFormData(state).facilityType === FACILITY_TYPES.COMMUNITY_CARE;
}
function isCCEligible(state) {
return getNewAppointment(state).isCCEligible;
}
function isSleepCare(state) {
return getFormData(state).typeOfCareId === SLEEP_CARE;
}
function isPodiatry(state) {
return getFormData(state).typeOfCareId === PODIATRY;
}
export default {
home: {
url: '/',
},
typeOfAppointment: {
url: '/new-appointment',
// Temporary stub for typeOfAppointment which will eventually be first step
// Next will direct to type of care or provider once both flows are complete
next: 'typeOfFacility',
previous: 'home',
},
typeOfCare: {
url: '/new-appointment',
async next(state, dispatch) {
let nextState = 'vaFacility';
const communityCareEnabled = vaosCommunityCare(state);
if (isSleepCare(state)) {
nextState = 'typeOfSleepCare';
} else if (isCommunityCare(state)) {
try {
if (communityCareEnabled) {
// Check if user registered systems support community care...
const userSystemIds = await getSystemIdentifiers();
const ccSites = await getSitesSupportingVAR();
const ccEnabledSystems = userSystemIds.filter(id =>
ccSites.some(site => site._id === id),
);
dispatch(updateCCEnabledSystems(ccEnabledSystems));
// Reroute to VA facility page if none of the user's registered systems support community care.
if (ccEnabledSystems.length) {
const response = await getCommunityCare(
getTypeOfCare(getNewAppointment(state).data).cceType,
);
dispatch(updateCCEligibility(response.eligible));
if (response.eligible) {
// If CC enabled systems and toc is podiatry, skip typeOfFacility
if (isPodiatry(state)) {
dispatch(updateFacilityType(FACILITY_TYPES.COMMUNITY_CARE));
return 'requestDateTime';
}
return 'typeOfFacility';
}
}
}
// If no CC enabled systems and toc is podiatry, show modal
if (isPodiatry(state)) {
dispatch(showTypeOfCareUnavailableModal());
return 'typeOfCare';
}
dispatch(updateFacilityType(FACILITY_TYPES.VAMC));
return 'vaFacility';
} catch (e) {
Sentry.captureException(e);
Sentry.captureMessage(
'Community Care eligibility check failed with errors',
);
dispatch(updateFacilityType(FACILITY_TYPES.VAMC));
return 'vaFacility';
}
}
dispatch(updateFacilityType(FACILITY_TYPES.VAMC));
return nextState;
},
previous: 'home',
},
typeOfFacility: {
url: '/new-appointment/choose-facility-type',
next(state) {
if (isCCAudiology(state)) {
return 'audiologyCareType';
}
if (isCCFacility(state)) {
return 'requestDateTime';
}
return 'vaFacility';
},
previous: 'typeOfCare',
},
typeOfSleepCare: {
url: '/new-appointment/choose-sleep-care',
next: 'vaFacility',
previous: 'typeOfCare',
},
audiologyCareType: {
url: '/new-appointment/audiology',
next: 'requestDateTime',
previous: 'typeOfFacility',
},
ccPreferences: {
url: '/new-appointment/community-care-preferences',
next: 'reasonForAppointment',
previous(state) {
if (isCCAudiology(state)) {
return 'audiologyCareType';
}
return 'typeOfFacility';
},
},
vaFacility: {
url: '/new-appointment/va-facility',
async next(state, dispatch) {
const eligibilityStatus = getEligibilityStatus(state);
if (eligibilityStatus.direct) {
let appointments = null;
// If we can't get the history, then continue anyway
// and we'll show the full clinic list
try {
appointments = await getLongTermAppointmentHistory();
} catch (error) {
Sentry.captureException(error);
}
if (appointments) {
const clinics = getClinicsForChosenFacility(state);
const hasMatchingClinics = clinics.some(
clinic =>
!!appointments.find(
appt =>
clinic.siteCode === appt.facilityId &&
clinic.clinicId === appt.clinicId,
),
);
if (hasMatchingClinics) {
dispatch(startDirectScheduleFlow(appointments));
return 'clinicChoice';
}
} else {
dispatch(startDirectScheduleFlow(appointments));
return 'clinicChoice';
}
}
if (eligibilityStatus.request) {
dispatch(startRequestAppointmentFlow());
return 'requestDateTime';
}
throw new Error('Veteran not eligible for direct scheduling or requests');
},
previous(state) {
let nextState = 'typeOfCare';
const communityCareEnabled = vaosCommunityCare(state);
if (communityCareEnabled && isCCEligible(state)) {
nextState = 'typeOfFacility';
}
return nextState;
},
},
clinicChoice: {
url: '/new-appointment/clinics',
previous: 'vaFacility',
next(state, dispatch) {
if (getFormData(state).clinicId === 'NONE') {
dispatch(startRequestAppointmentFlow());
return 'requestDateTime';
}
// fetch appointment slots
return 'preferredDate';
},
},
preferredDate: {
url: '/new-appointment/preferred-date',
next: 'selectDateTime',
previous: 'clinicChoice',
},
selectDateTime: {
url: '/new-appointment/select-date',
next: 'reasonForAppointment',
previous: 'preferredDate',
},
requestDateTime: {
url: '/new-appointment/request-date',
next(state) {
if (isCCFacility(state)) {
return 'ccPreferences';
}
return 'reasonForAppointment';
},
previous(state) {
if (isPodiatry(state)) {
return 'typeOfCare';
}
if (isCCFacility(state)) {
return 'typeOfFacility';
}
return 'vaFacility';
},
},
reasonForAppointment: {
url: '/new-appointment/reason-appointment',
next(state) {
if (
isCCFacility(state) ||
getNewAppointment(state).flowType === FLOW_TYPES.DIRECT
) {
return 'contactInfo';
}
return 'visitType';
},
previous(state) {
if (isCCFacility(state)) {
return 'ccPreferences';
}
if (getNewAppointment(state).flowType === FLOW_TYPES.DIRECT) {
return 'selectDateTime';
}
return 'requestDateTime';
},
},
visitType: {
url: '/new-appointment/choose-visit-type',
previous: 'reasonForAppointment',
next: 'contactInfo',
},
appointmentTime: {
url: '/new-appointment/appointment-time',
next: 'contactInfo',
previous: 'vaFacility',
},
contactInfo: {
url: '/new-appointment/contact-info',
next: 'review',
previous(state) {
if (isCCFacility(state)) {
return 'ccPreferences';
}
if (getNewAppointment(state).flowType === FLOW_TYPES.DIRECT) {
return 'reasonForAppointment';
}
return 'visitType';
},
},
review: {
url: '/new-appointment/review',
},
};