forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm-group.ts
186 lines (165 loc) · 6.13 KB
/
alarm-group.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
import { Type } from 'class-transformer';
import {
IsNumber,
IsString,
IsUUID,
Min,
ValidateNested,
} from 'class-validator';
import { AlarmGroup } from '../../models/alarm-group';
import { AlarmGroupVehicle } from '../../models/utils/alarm-group-vehicle';
import type { Mutable } from '../../utils';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { ReducerError } from '../reducer-error';
import { getElement } from './utils/get-element';
export class AddAlarmGroupAction implements Action {
@IsValue('[AlarmGroup] Add AlarmGroup' as const)
public readonly type = '[AlarmGroup] Add AlarmGroup';
@ValidateNested()
@Type(() => AlarmGroup)
public readonly alarmGroup!: AlarmGroup;
}
export class RenameAlarmGroupAction implements Action {
@IsValue('[AlarmGroup] Rename AlarmGroup' as const)
public readonly type = '[AlarmGroup] Rename AlarmGroup';
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupId!: UUID;
@IsString()
public readonly name!: string;
}
export class RemoveAlarmGroupAction implements Action {
@IsValue('[AlarmGroup] Remove AlarmGroup' as const)
public readonly type = '[AlarmGroup] Remove AlarmGroup';
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupId!: UUID;
}
export class AddAlarmGroupVehicleAction implements Action {
@IsValue('[AlarmGroup] Add AlarmGroupVehicle' as const)
public readonly type = '[AlarmGroup] Add AlarmGroupVehicle';
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupId!: UUID;
@ValidateNested()
@Type(() => AlarmGroupVehicle)
public readonly alarmGroupVehicle!: AlarmGroupVehicle;
}
export class EditAlarmGroupVehicleAction implements Action {
@IsValue('[AlarmGroup] Edit AlarmGroupVehicle' as const)
public readonly type = '[AlarmGroup] Edit AlarmGroupVehicle';
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupVehicleId!: UUID;
@IsNumber()
@Min(0)
public readonly time!: number;
@IsString()
public readonly name!: string;
}
export class RemoveAlarmGroupVehicleAction implements Action {
@IsValue('[AlarmGroup] Remove AlarmGroupVehicle' as const)
public readonly type = '[AlarmGroup] Remove AlarmGroupVehicle';
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupId!: UUID;
@IsUUID(4, uuidValidationOptions)
public readonly alarmGroupVehicleId!: UUID;
}
export namespace AlarmGroupActionReducers {
export const addAlarmGroup: ActionReducer<AddAlarmGroupAction> = {
action: AddAlarmGroupAction,
reducer: (draftState, { alarmGroup }) => {
draftState.alarmGroups[alarmGroup.id] =
cloneDeepMutable(alarmGroup);
return draftState;
},
rights: 'trainer',
};
export const renameAlarmGroup: ActionReducer<RenameAlarmGroupAction> = {
action: RenameAlarmGroupAction,
reducer: (draftState, { alarmGroupId, name }) => {
const alarmGroup = getElement(
draftState,
'alarmGroup',
alarmGroupId
);
alarmGroup.name = name;
return draftState;
},
rights: 'trainer',
};
export const removeAlarmGroup: ActionReducer<RemoveAlarmGroupAction> = {
action: RemoveAlarmGroupAction,
reducer: (draftState, { alarmGroupId }) => {
getElement(draftState, 'alarmGroup', alarmGroupId);
delete draftState.alarmGroups[alarmGroupId];
return draftState;
},
rights: 'trainer',
};
export const addAlarmGroupVehicle: ActionReducer<AddAlarmGroupVehicleAction> =
{
action: AddAlarmGroupVehicleAction,
reducer: (draftState, { alarmGroupId, alarmGroupVehicle }) => {
const alarmGroup = getElement(
draftState,
'alarmGroup',
alarmGroupId
);
alarmGroup.alarmGroupVehicles[alarmGroupVehicle.id] =
cloneDeepMutable(alarmGroupVehicle);
return draftState;
},
rights: 'trainer',
};
export const editAlarmGroupVehicle: ActionReducer<EditAlarmGroupVehicleAction> =
{
action: EditAlarmGroupVehicleAction,
reducer: (
draftState,
{ alarmGroupId, alarmGroupVehicleId, time, name }
) => {
const alarmGroup = getElement(
draftState,
'alarmGroup',
alarmGroupId
);
const alarmGroupVehicle = getAlarmGroupVehicle(
alarmGroup,
alarmGroupVehicleId
);
alarmGroupVehicle.time = time;
alarmGroupVehicle.name = name;
return draftState;
},
rights: 'trainer',
};
export const removeAlarmGroupVehicle: ActionReducer<RemoveAlarmGroupVehicleAction> =
{
action: RemoveAlarmGroupVehicleAction,
reducer: (draftState, { alarmGroupId, alarmGroupVehicleId }) => {
const alarmGroup = getElement(
draftState,
'alarmGroup',
alarmGroupId
);
getAlarmGroupVehicle(alarmGroup, alarmGroupVehicleId);
delete alarmGroup.alarmGroupVehicles[alarmGroupVehicleId];
return draftState;
},
rights: 'trainer',
};
}
function getAlarmGroupVehicle(
alarmGroup: Mutable<AlarmGroup>,
alarmGroupVehicleId: UUID
) {
const alarmGroupVehicle =
alarmGroup.alarmGroupVehicles[alarmGroupVehicleId];
if (!alarmGroupVehicle) {
throw new ReducerError(
`AlarmGroupVehicle with id ${alarmGroupVehicleId} does not exist in AlarmGroup with id ${alarmGroup.id}`
);
}
return alarmGroupVehicle;
}