forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatient.ts
270 lines (241 loc) · 9.49 KB
/
patient.ts
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
import { Type } from 'class-transformer';
import { IsString, IsUUID, MaxLength, ValidateNested } from 'class-validator';
import { Patient } from '../../models/patient';
import {
isOnMap,
MapPosition,
PatientStatus,
patientStatusAllowedValues,
MapCoordinates,
isNotInSimulatedRegion,
currentSimulatedRegionIdOf,
currentCoordinatesOf,
isInSimulatedRegion,
currentSimulatedRegionOf,
} from '../../models/utils';
import {
changePosition,
changePositionWithId,
} from '../../models/utils/position/position-helpers-mutable';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import {
cloneDeepMutable,
StrictObject,
UUID,
uuidValidationOptions,
} from '../../utils';
import { IsLiteralUnion, IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
import { PatientRemovedEvent } from '../../simulation/events';
import { sendSimulationEvent } from '../../simulation/events/utils';
import { updateTreatments } from './utils/calculate-treatments';
import { getElement } from './utils/get-element';
import { removeElementPosition } from './utils/spatial-elements';
import { logPatientAdded, logPatientRemoved } from './utils/log';
/**
* Performs all necessary actions to remove a patient from the state.
* This includes deleting all treatments, removing it from the spatial tree and sending a {@link PatientRemovedEvent} if the patient is in a simulated region.
* @param patientId The ID of the patient to be deleted
*/
export function deletePatient(
draftState: Mutable<ExerciseState>,
patientId: UUID
) {
const patient = getElement(draftState, 'patient', patientId);
if (isInSimulatedRegion(patient)) {
const simulatedRegion = currentSimulatedRegionOf(draftState, patient);
sendSimulationEvent(
simulatedRegion,
PatientRemovedEvent.create(patientId)
);
}
removeElementPosition(draftState, 'patient', patientId);
delete draftState.patients[patientId];
}
export class AddPatientAction implements Action {
@IsValue('[Patient] Add patient' as const)
public readonly type = '[Patient] Add patient';
@ValidateNested()
@Type(() => Patient)
public readonly patient!: Patient;
}
export class MovePatientAction implements Action {
@IsValue('[Patient] Move patient' as const)
public readonly type = '[Patient] Move patient';
@IsUUID(4, uuidValidationOptions)
public readonly patientId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
}
export class RemovePatientFromSimulatedRegionAction implements Action {
@IsValue('[Patient] Remove patient from simulated region' as const)
public readonly type = '[Patient] Remove patient from simulated region';
@IsUUID(4, uuidValidationOptions)
public readonly patientId!: UUID;
}
export class RemovePatientAction implements Action {
@IsValue('[Patient] Remove patient' as const)
public readonly type = '[Patient] Remove patient';
@IsUUID(4, uuidValidationOptions)
public readonly patientId!: UUID;
}
export class SetVisibleStatusAction implements Action {
@IsValue('[Patient] Set Visible Status' as const)
public readonly type = '[Patient] Set Visible Status';
@IsUUID(4, uuidValidationOptions)
public readonly patientId!: UUID;
@IsLiteralUnion(patientStatusAllowedValues)
public readonly patientStatus!: PatientStatus;
}
export class SetUserTextAction implements Action {
@IsValue('[Patient] Set Remarks' as const)
public readonly type = '[Patient] Set Remarks';
@IsUUID(4, uuidValidationOptions)
public readonly patientId!: UUID;
@IsString()
@MaxLength(65535)
public readonly remarks!: string;
}
export namespace PatientActionReducers {
export const addPatient: ActionReducer<AddPatientAction> = {
action: AddPatientAction,
reducer: (draftState, { patient }) => {
if (
StrictObject.entries(patient.healthStates).some(
([id, healthState]) => healthState.id !== id
)
) {
throw new ReducerError(
"Not all health state's ids match their key id"
);
}
StrictObject.values(patient.healthStates).forEach((healthState) => {
healthState.nextStateConditions.forEach(
(nextStateCondition) => {
if (
patient.healthStates[
nextStateCondition.matchingHealthStateId
] === undefined
) {
throw new ReducerError(
`HealthState with id ${nextStateCondition.matchingHealthStateId} does not exist`
);
}
}
);
});
if (
patient.healthStates[patient.currentHealthStateId] === undefined
) {
throw new ReducerError(
`HealthState with id ${patient.currentHealthStateId} does not exist`
);
}
const mutablePatient = cloneDeepMutable(patient);
draftState.patientCounter++;
const paddedCounter = String(draftState.patientCounter).padStart(
4,
'0'
);
mutablePatient.identifier = `${draftState.configuration.patientIdentifierPrefix}${paddedCounter}`;
draftState.patients[mutablePatient.id] = mutablePatient;
changePosition(mutablePatient, patient.position, draftState);
logPatientAdded(draftState, patient.id);
return draftState;
},
rights: 'trainer',
};
export const movePatient: ActionReducer<MovePatientAction> = {
action: MovePatientAction,
reducer: (draftState, { patientId, targetPosition }) => {
changePositionWithId(
patientId,
MapPosition.create(targetPosition),
'patient',
draftState
);
return draftState;
},
rights: 'participant',
};
export const removePatientFromSimulatedRegion: ActionReducer<RemovePatientFromSimulatedRegionAction> =
{
action: RemovePatientFromSimulatedRegionAction,
reducer: (draftState, { patientId }) => {
const patient = getElement(draftState, 'patient', patientId);
if (isNotInSimulatedRegion(patient)) {
throw new ReducerError(
`Patient with Id: ${patientId} was expected to be in simulated region but position was of type: ${patient.position.type}`
);
}
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
currentSimulatedRegionIdOf(patient)
);
sendSimulationEvent(
simulatedRegion,
PatientRemovedEvent.create(patientId)
);
const coordinates = cloneDeepMutable(
currentCoordinatesOf(simulatedRegion)
);
// place the patient on the right hand side of the simulated region
coordinates.y -= 0.5 * simulatedRegion.size.height;
coordinates.x += 5 + Math.max(simulatedRegion.size.width, 0);
changePositionWithId(
patientId,
MapPosition.create(coordinates),
'patient',
draftState
);
return draftState;
},
rights: 'trainer',
};
export const removePatient: ActionReducer<RemovePatientAction> = {
action: RemovePatientAction,
reducer: (draftState, { patientId }) => {
const patient = getElement(draftState, 'patient', patientId);
if (isInSimulatedRegion(patient)) {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
currentSimulatedRegionIdOf(patient)
);
sendSimulationEvent(
simulatedRegion,
PatientRemovedEvent.create(patientId)
);
}
logPatientRemoved(draftState, patientId);
deletePatient(draftState, patientId);
return draftState;
},
rights: 'trainer',
};
export const setVisibleStatus: ActionReducer<SetVisibleStatusAction> = {
action: SetVisibleStatusAction,
reducer: (draftState, { patientId, patientStatus }) => {
const patient = getElement(draftState, 'patient', patientId);
patient.pretriageStatus = patientStatus;
if (isOnMap(patient)) {
updateTreatments(draftState, patient);
}
return draftState;
},
rights: 'participant',
};
export const setUserTextAction: ActionReducer<SetUserTextAction> = {
action: SetUserTextAction,
reducer: (draftState, { patientId, remarks }) => {
const patient = getElement(draftState, 'patient', patientId);
patient.remarks = remarks;
return draftState;
},
rights: 'participant',
};
}