-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceEventCriteria.ts
More file actions
90 lines (79 loc) · 3.49 KB
/
Copy pathDeviceEventCriteria.ts
File metadata and controls
90 lines (79 loc) · 3.49 KB
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
import { EventClassification, DeviceEvent, EventTriggerSource } from "./DeviceEvent";
export interface DeviceEventCriteria {
deviceId?: string;
beforeGlobalId?: number;
eventTriggerSource?: EventTriggerSource | EventTriggerSource[];
eventClassification?: EventClassification | EventClassification[];
eventManualClassification?: EventClassification | EventClassification[] | null;
rfidCode?: string | string[];
isStarred?: boolean;
isContraband?: boolean;
sampling?: number;
}
export type UserDeviceEventCriteria = Omit<DeviceEventCriteria, "deviceId" | "eventManualClassification" | "sampling">;
export class DeviceEventCriteriaHelper {
/**
* Check if an event matches the given criteria
*/
static eventMatches(event: DeviceEvent, criteria?: DeviceEventCriteria): boolean {
if (!criteria) return true;
// Check eventClassification if specified
if (criteria.eventClassification !== undefined) {
const classifications = Array.isArray(criteria.eventClassification)
? criteria.eventClassification
: [criteria.eventClassification];
if (!classifications.includes(event.eventClassification as EventClassification)) {
return false;
}
}
if (criteria.eventManualClassification === null) {
if (event.eventManualClassification !== null) {
return false;
}
} else if (criteria.eventManualClassification !== undefined) {
const manualClassifications = Array.isArray(criteria.eventManualClassification)
? criteria.eventManualClassification
: [criteria.eventManualClassification];
if (!manualClassifications.includes(event.eventManualClassification as EventClassification)) {
return false;
}
}
if (criteria.eventTriggerSource !== undefined) {
const triggerSources = Array.isArray(criteria.eventTriggerSource)
? criteria.eventTriggerSource
: [criteria.eventTriggerSource];
if (!triggerSources.includes(event.eventTriggerSource as EventTriggerSource)) {
return false;
}
}
if (criteria.sampling !== undefined && criteria.sampling > 1) {
if (event.globalId === undefined || event.globalId % criteria.sampling !== 0) {
return false;
}
}
if (criteria.rfidCode !== undefined) {
const requestedCodes = Array.isArray(criteria.rfidCode) ? criteria.rfidCode : [criteria.rfidCode];
const eventRfidCodes = event.rfidCodes ?? [];
if (!requestedCodes.some((rfidCode) => eventRfidCodes.includes(rfidCode))) {
return false;
}
}
// Add checks for other criteria properties as needed
// This will automatically handle any future criteria properties
for (const [key, value] of Object.entries(criteria)) {
if (
key === "beforeGlobalId" ||
key === "eventClassification" ||
key === "eventManualClassification" ||
key === "eventTriggerSource" ||
key === "rfidCode" ||
key === "sampling"
)
continue; // Skip pagination/filter helpers handled above
if (value !== undefined && event[key] !== value) {
return false;
}
}
return true;
}
}