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

Fix deletion of simulated region now cascades #740

Merged
merged 9 commits into from
Mar 10, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project does **not** adhere to [Semantic Versioning](https://semver.org
### Changed

- Images are now stored in git and not git lfs anymore
- Patients, vehicles, personnel and material inside a simulated region are now deleted, when the simulated region is deleted. For vehicles, personnel, and material, they will only be deleted if all that belong together are in the same simulated region.

### Fixed

Expand Down
112 changes: 92 additions & 20 deletions shared/src/store/action-reducers/simulated-region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { SimulatedRegion, TransferPoint } from '../../models';
import {
isInSpecificSimulatedRegion,
isInSpecificVehicle,
MapCoordinates,
MapPosition,
SimulatedRegionPosition,
Expand All @@ -21,14 +22,103 @@ import {
MaterialAvailableEvent,
} from '../../simulation';
import { sendSimulationEvent } from '../../simulation/events/utils';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsLiteralUnion, IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ExpectedReducerError, ReducerError } from '../reducer-error';
import { TransferPointActionReducers } from './transfer-point';
import {
deleteTransferPoint,
TransferPointActionReducers,
} from './transfer-point';
import { isCompletelyLoaded } from './utils/completely-load-vehicle';
import { getElement, getElementByPredicate } from './utils/get-element';

/**
* This function assumes that every SimulatedRegion holds **ONLY** material,vehicles,patients and personnel
* (in any amount) and **ONE** TransferPoint.
* It only deletes material,vehicles and personnel if they are **ALL** in the SimulatedRegion to be deleted
* If one were to add more things to a SimulatedRegion one would have to change this function accordingly.
* @param draftState: The Draft State from which the SimulatedRegion should be deleted.
* @param simulatedRegionId: The Id of the SimulatedRegion that should be deleted.
*/

export function deleteSimulatedRegion(
draftState: Mutable<ExerciseState>,
simulatedRegionId: UUID
) {
// Delete the TransferPoint

const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);

const transferPointId = getElementByPredicate(
draftState,
'transferPoint',
(element) => isInSpecificSimulatedRegion(element, simulatedRegion.id)
).id;

deleteTransferPoint(draftState, transferPointId);

// Find related vehicles

const relatedVehicles = Object.values(draftState.vehicles).filter(
(vehicle) =>
isInSpecificSimulatedRegion(vehicle, simulatedRegionId) &&
Object.keys(vehicle.materialIds).every((materialId) => {
const material = getElement(draftState, 'material', materialId);
return (
isInSpecificSimulatedRegion(material, simulatedRegionId) ||
isInSpecificVehicle(material, vehicle.id)
);
}) &&
Object.keys(vehicle.personnelIds).every((personnelId) => {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
return (
isInSpecificSimulatedRegion(personnel, simulatedRegionId) ||
isInSpecificVehicle(personnel, vehicle.id)
);
})
);

// Find and delete related materials and personnel

relatedVehicles.forEach((vehicle) => {
Object.keys(vehicle.materialIds).forEach(
(vehicleId) => delete draftState.materials[vehicleId]
);
Object.keys(vehicle.personnelIds).forEach(
(personnelId) => delete draftState.personnel[personnelId]
);
});

// Delete related vehicles

relatedVehicles.forEach(
(vehicle) => delete draftState.vehicles[vehicle.id]
);

// Find and delete related patients

Object.values(draftState.patients)
.filter((patients) =>
isInSpecificSimulatedRegion(patients, simulatedRegionId)
)
.forEach((patients) => delete draftState.patients[patients.id]);

// Delete the SimulatedRegion

delete draftState.simulatedRegions[simulatedRegionId];
}

export class AddSimulatedRegionAction implements Action {
@IsValue('[SimulatedRegion] Add simulated region' as const)
readonly type = '[SimulatedRegion] Add simulated region';
Expand Down Expand Up @@ -146,25 +236,7 @@ export namespace SimulatedRegionActionReducers {
{
action: RemoveSimulatedRegionAction,
reducer: (draftState, { simulatedRegionId }) => {
const simulatedRegion = getElement(
draftState,
'simulatedRegion',
simulatedRegionId
);
const transferPoint = getElementByPredicate(
draftState,
'transferPoint',
(element) =>
isInSpecificSimulatedRegion(element, simulatedRegion.id)
);
TransferPointActionReducers.removeTransferPoint.reducer(
draftState,
{
type: '[TransferPoint] Remove TransferPoint',
transferPointId: transferPoint.id,
}
);
lukasrad02 marked this conversation as resolved.
Show resolved Hide resolved
delete draftState.simulatedRegions[simulatedRegionId];
deleteSimulatedRegion(draftState, simulatedRegionId);
return draftState;
},
rights: 'trainer',
Expand Down
100 changes: 46 additions & 54 deletions shared/src/store/action-reducers/transfer-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
nestedCoordinatesOf,
} from '../../models/utils';
import { changePositionWithId } from '../../models/utils/position/position-helpers-mutable';
import type { ExerciseState } from '../../state';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
Expand All @@ -23,6 +25,49 @@ import { letElementArrive } from './transfer';
import { calculateDistance } from './utils/calculate-distance';
import { getElement } from './utils/get-element';

export function deleteTransferPoint(
draftState: Mutable<ExerciseState>,
transferPointId: UUID
) {
// check if transferPoint exists
getElement(draftState, 'transferPoint', transferPointId);
// TODO: make it dynamic (if at any time something else is able to transfer this part needs to be changed accordingly)
// Let all vehicles and personnel arrive that are on transfer to this transferPoint before deleting it
for (const vehicleId of Object.keys(draftState.vehicles)) {
const vehicle = getElement(draftState, 'vehicle', vehicleId);
if (
isInTransfer(vehicle) &&
currentTransferOf(vehicle).targetTransferPointId === transferPointId
) {
letElementArrive(draftState, vehicle.type, vehicleId);
}
}
for (const personnelId of Object.keys(draftState.personnel)) {
const personnel = getElement(draftState, 'personnel', personnelId);
if (
isInTransfer(personnel) &&
currentTransferOf(personnel).targetTransferPointId ===
transferPointId
) {
letElementArrive(draftState, personnel.type, personnelId);
}
}
// TODO: If we can assume that the transfer points are always connected to each other,
// we could just iterate over draftState.transferPoints[transferPointId].reachableTransferPoints
for (const transferPoint of Object.values(draftState.transferPoints)) {
for (const connectedTransferPointId of Object.keys(
transferPoint.reachableTransferPoints
)) {
const connectedTransferPoint =
draftState.transferPoints[connectedTransferPointId]!;
delete connectedTransferPoint.reachableTransferPoints[
transferPointId
];
}
}
delete draftState.transferPoints[transferPointId];
}

// TODO check: type "TransferPoint" the T is big, in other files, the second word starts with a small letter

export class AddTransferPointAction implements Action {
Expand Down Expand Up @@ -236,60 +281,7 @@ export namespace TransferPointActionReducers {
{
action: RemoveTransferPointAction,
reducer: (draftState, { transferPointId }) => {
// check if transferPoint exists
getElement(draftState, 'transferPoint', transferPointId);
// TODO: make it dynamic (if at any time something else is able to transfer this part needs to be changed accordingly)
// Let all vehicles and personnel arrive that are on transfer to this transferPoint before deleting it
for (const vehicleId of Object.keys(draftState.vehicles)) {
const vehicle = getElement(
draftState,
'vehicle',
vehicleId
);
if (
isInTransfer(vehicle) &&
currentTransferOf(vehicle).targetTransferPointId ===
transferPointId
) {
letElementArrive(draftState, vehicle.type, vehicleId);
}
}
for (const personnelId of Object.keys(draftState.personnel)) {
const personnel = getElement(
draftState,
'personnel',
personnelId
);
if (
isInTransfer(personnel) &&
currentTransferOf(personnel).targetTransferPointId ===
transferPointId
) {
letElementArrive(
draftState,
personnel.type,
personnelId
);
}
}
// TODO: If we can assume that the transfer points are always connected to each other,
// we could just iterate over draftState.transferPoints[transferPointId].reachableTransferPoints
for (const transferPoint of Object.values(
draftState.transferPoints
)) {
for (const connectedTransferPointId of Object.keys(
transferPoint.reachableTransferPoints
)) {
const connectedTransferPoint =
draftState.transferPoints[
connectedTransferPointId
]!;
delete connectedTransferPoint.reachableTransferPoints[
transferPointId
];
}
}
delete draftState.transferPoints[transferPointId];
deleteTransferPoint(draftState, transferPointId);
return draftState;
},
rights: 'trainer',
Expand Down