fleetmap-reports 1.0.642 → 1.0.644

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.642",
3
+ "version": "1.0.644",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -11,24 +11,16 @@ function calculateFirstStopTime (stopTime, from, time) {
11
11
  }
12
12
 
13
13
  async function createActivityReport (from, to, userData, traccarInstance) {
14
- const notAuthGroup = userData.groups.find(g => g.id === Number(process.env.AFRIQUIA_NOT_AUTH_GROUP_ID))
15
- const authGroup = userData.groups.find(g => g.id === Number(process.env.AFRIQUIA_AUTH_GROUP_ID))
16
-
17
- console.log(notAuthGroup, authGroup)
18
-
19
14
  const unauthorizedGeofences = []
20
15
  const authorizedGeofences = []
21
- if (notAuthGroup) {
22
- const geofenceIds = (await traccarInstance.geofences.geofencesGet(undefined, undefined, undefined, notAuthGroup.id, undefined, undefined).then(r => r.data)).map(g => g.id)
23
- unauthorizedGeofences.push(...(userData.geofences.filter(g => geofenceIds.includes(g.id))))
24
- console.log('unauthorizedGeofences', unauthorizedGeofences)
25
- }
26
-
27
- if (authGroup) {
28
- const geofenceIds = (await traccarInstance.geofences.geofencesGet(undefined, undefined, undefined, authGroup.id, undefined, undefined).then(r => r.data)).map(g => g.id)
29
- authorizedGeofences.push(...(userData.geofences.filter(g => geofenceIds.includes(g.id))))
30
- console.log('authorizedGeofences', unauthorizedGeofences)
31
- }
16
+
17
+ const notAuthGeofenceIds = (await traccarInstance.geofences.geofencesGet(undefined, undefined, undefined, Number(process.env.AFRIQUIA_NOT_AUTH_GROUP_ID), undefined, undefined).then(r => r.data)).map(g => g.id)
18
+ unauthorizedGeofences.push(...(userData.geofences.filter(g => notAuthGeofenceIds.includes(g.id))))
19
+ console.log('unauthorizedGeofences', unauthorizedGeofences)
20
+
21
+ const authGeofenceIds = (await traccarInstance.geofences.geofencesGet(undefined, undefined, undefined, Number(process.env.AFRIQUIA_AUTH_GROUP_ID), undefined, undefined).then(r => r.data)).map(g => g.id)
22
+ authorizedGeofences.push(...(userData.geofences.filter(g => authGeofenceIds.includes(g.id))))
23
+ console.log('authorizedGeofences', authorizedGeofences)
32
24
 
33
25
  const allInOne = await traccar.getAllInOne(traccarInstance, from, to, userData.devices, true, true, true, false)
34
26
 
@@ -39,12 +31,27 @@ async function createActivityReport (from, to, userData, traccarInstance) {
39
31
  const stops = allInOne.stops.filter(s => s.deviceId === d.id)
40
32
  const group = userData.groups.find(g => g.id === d.groupId)
41
33
 
42
- const unauthorizedStops = stops.filter(s =>
43
- unauthorizedGeofences.find(g => insideGeofence({ latitude: s.latitude, longitude: s.longitude }, g))
44
- )
45
- const authorizedStops = stops.filter(s =>
46
- authorizedGeofences.find(g => insideGeofence({ latitude: s.latitude, longitude: s.longitude }, g))
47
- )
34
+ let unauthorizedStops = 0
35
+ let authorizedStops = 0
36
+ let otherStops = 0
37
+ let authorizedStopsTime = 0
38
+
39
+ stops.forEach(s => {
40
+ const unauthorized = unauthorizedGeofences.find(g => insideGeofence({ latitude: s.latitude, longitude: s.longitude }, g))
41
+
42
+ if (unauthorized) {
43
+ unauthorizedStops = unauthorizedStops + 1
44
+ } else {
45
+ const authorized = authorizedGeofences.find(g => insideGeofence({ latitude: s.latitude, longitude: s.longitude }, g))
46
+
47
+ if (authorized) {
48
+ authorizedStops = authorizedStops + 1
49
+ authorizedStopsTime = authorizedStopsTime + (s.duration > s.engineHours ? (s.duration - s.engineHours) : 0)
50
+ } else {
51
+ otherStops = otherStops + 1
52
+ }
53
+ }
54
+ })
48
55
 
49
56
  const idleEvents = getIdleEvents(route)
50
57
 
@@ -94,10 +101,10 @@ async function createActivityReport (from, to, userData, traccarInstance) {
94
101
  totalIdleEvents: idleEvents.length,
95
102
  minIdleEvent: idleTimeArray.length > 0 ? Math.min(...idleTimeArray) : 0,
96
103
  maxIdleEvent: idleTimeArray.length > 0 ? Math.max(...idleTimeArray) : 0,
97
- otherStops: stops.length - unauthorizedStops.length - authorizedStops.length,
98
- authorizedStops: authorizedStops.length,
99
- unauthorizedStops: unauthorizedStops.length,
100
- authorizedStopsTime: unauthorizedStops.reduce((a, b) => a + (b.duration > b.engineHours ? (b.duration - b.engineHours) : 0), 0),
104
+ otherStops,
105
+ authorizedStops,
106
+ unauthorizedStops,
107
+ authorizedStopsTime,
101
108
  averageSpeed: totalDistance > 0 ? ((trips.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / totalDistance) : 0,
102
109
  maxSpeed: trips.reduce((a, b) => {
103
110
  return a > b.maxSpeed ? a : b.maxSpeed