fleetmap-reports 1.0.982 → 1.0.983

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.982",
3
+ "version": "1.0.983",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/kms-report.js CHANGED
@@ -10,9 +10,8 @@ const traccar = require('./util/traccar')
10
10
  const { getDates, getTranslations, isClientSide, executeServerSide } = require('./util/utils')
11
11
  const drivers = require('./util/driver')
12
12
  const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getDataByDay, checkTripsKms, getKms } = require('./util/trips')
13
- const traccarHelper = require('./util/traccar')
14
13
  const { devicesToProcess } = require('./util/device')
15
- const { getDriverData } = require('./util/driver')
14
+ const { getDriverData, reportByDriver } = require('./util/driver')
16
15
  const automaticReports = require('./automaticReports')
17
16
  const { getSummaryByDay } = require('./util/traccar')
18
17
 
@@ -100,16 +99,30 @@ async function createKmsReportByDevice (from, to, userData, traccarInstance) {
100
99
 
101
100
  async function createKmsReportByDriver (from, to, userData, traccarInstance) {
102
101
  const devices = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
103
- console.log(devices.length)
104
102
 
105
- if (!devices.length) {
106
- // empty report
107
- return { drivers: [] }
108
- }
103
+ const results = await reportByDriver(devices, traccarInstance, from, to, userData, processDrivers)
104
+
105
+ const report = { drivers: [] }
109
106
 
110
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
107
+ results.drivers.forEach(result => {
108
+ const driver = report.drivers.find(d => d.driver.id === result.driver.id)
109
+ if (driver) {
110
+ if (userData.groupByDay) {
111
+ driver.days.forEach(day => {
112
+ const resultDay = result.days.find(d => d.date === day.date)
113
+ if (resultDay) {
114
+ day.kms = day.kms + resultDay.kms
115
+ }
116
+ })
117
+ } else {
118
+ driver.summary.distance = driver.summary.distance + result.summary.distance
119
+ }
120
+ } else {
121
+ report.drivers.push(result)
122
+ }
123
+ })
111
124
 
112
- return { drivers: processDrivers(from, to, userData, allInOne) }
125
+ return report
113
126
  }
114
127
 
115
128
  async function createKmsReportByGroup (from, to, userData, traccarInstance) {
@@ -1,5 +1,8 @@
1
1
  const jsPDF = require('jspdf')
2
- const { convertMS, convertToLocaleString, convertToFeature, convertPositionToFeature, getTranslations, getLanguage } = require('./util/utils')
2
+ const {
3
+ convertMS, convertToLocaleString, convertToFeature, convertPositionToFeature, getTranslations, getLanguage,
4
+ getCountry
5
+ } = require('./util/utils')
3
6
  const { headerFromUser, AmiriRegular } = require('./util/pdfDocument')
4
7
  require('jspdf-autotable')
5
8
  const { getStyle } = require('./reportStyle')
@@ -15,7 +18,6 @@ const booleanPointInPolygon = require('@turf/boolean-point-in-polygon')
15
18
  const automaticReports = require('./automaticReports')
16
19
  const { default: axios } = require('axios')
17
20
  const { getDriverName, getDriverData } = require('./util/driver')
18
- const crg = require('country-reverse-geocoding').country_reverse_geocoding()
19
21
 
20
22
  const fileName = 'SpeedingReport'
21
23
  const eventTypes = ['deviceOverspeed']
@@ -299,11 +301,6 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
299
301
  return events
300
302
  }
301
303
 
302
- function getCountry (position) {
303
- const country = crg.get_country(position.latitude, position.longitude)
304
- return country && country.code
305
- }
306
-
307
304
  async function getRoadSpeedLimits (devices, routes, threshold, minimumMinutes = 0) {
308
305
  const position = routes.find(p => getCountry(p))
309
306
  const country = position && getCountry(position)
@@ -52,4 +52,13 @@ describe('Kms_Reports', function () {
52
52
  assert.equal(device.days.length, 30) // Total Kms
53
53
  assert.equal(device.days[29].kms, 136174.93281451002) // Total Kms
54
54
  }, 30000)
55
+ it('KmsReport by driver', async () => {
56
+ const reports = await getReports()
57
+ const userData = await reports.getUserData()
58
+ userData.byDriver = true
59
+ console.log(userData.drivers)
60
+ const data = await reports.kmsReport(new Date(2024, 0, 24, 0, 0, 0), new Date(2024, 0, 24, 23, 59, 59),
61
+ userData)
62
+ assert.equal(data.length, 1)
63
+ }, 3000000)
55
64
  })
@@ -1,5 +1,6 @@
1
1
  const { getReports } = require('./index')
2
2
  const assert = require('assert')
3
+ const { getCountry } = require('../util/utils')
3
4
 
4
5
  async function getSpeedingReport (report, userData) {
5
6
  const data = await report.speedingReport(
@@ -61,4 +62,49 @@ describe('speeding tests', function () {
61
62
  assert.equal(totalDistance, 46.64576895855739) // Total Kms
62
63
  assert.equal(totalEventTime, 1402000) // Total Duration
63
64
  }, 999990000)
65
+
66
+ // eslint-disable-next-line no-undef
67
+ it('detects brazil', () => {
68
+ const p = {
69
+ id: 24088423790,
70
+ attributes: {
71
+ door: false,
72
+ hours: 2520745000,
73
+ motion: false,
74
+ distance: 0,
75
+ odometer: 5973617,
76
+ sat: 17,
77
+ battery: 4.03,
78
+ hdop: 0.7,
79
+ idleTime: 0,
80
+ output: 0,
81
+ input: 0,
82
+ driverUniqueId: '',
83
+ power: 12.61,
84
+ event: 0,
85
+ totalDistance: 31419705.27,
86
+ ignition: false,
87
+ adc3: 0,
88
+ adc2: 0,
89
+ status: 61,
90
+ tripTime: 0
91
+ },
92
+ deviceId: 28097,
93
+ type: null,
94
+ protocol: 'startek',
95
+ serverTime: '2024-01-20T03:01:50.000+00:00',
96
+ deviceTime: '2024-01-20T03:01:50.000+00:00',
97
+ fixTime: '2024-01-20T03:01:50.000+00:00',
98
+ outdated: false,
99
+ valid: true,
100
+ latitude: -22.295906,
101
+ longitude: -48.576135,
102
+ altitude: 579,
103
+ speed: 0,
104
+ course: 80,
105
+ address: 'Avenida Deputado Zien Nassif, 938, Vila Industrial, Jaú - SP, 17204-212, Brazil',
106
+ accuracy: 0
107
+ }
108
+ assert.equal('BRA', getCountry(p))
109
+ })
64
110
  })
package/src/util/utils.js CHANGED
@@ -298,3 +298,14 @@ exports.getLogoUrl = getLogoUrl
298
298
  exports.convertFromUTCDate = (value, user) => {
299
299
  return convertFromUTC(value, user.attributes.timezone || getUserPartner(user).timezone)
300
300
  }
301
+
302
+ const crg = require('country-reverse-geocoding').country_reverse_geocoding()
303
+
304
+ exports.getCountry = (position) => {
305
+ const country = crg.get_country(position.latitude, position.longitude)
306
+ if (!country) {
307
+ const country = position && position.attributes.address && position.attributes.address.split(',').slice(-1)[0].trim()
308
+ return { Brazil: 'BRA' }[country]
309
+ }
310
+ return country && country.code
311
+ }