@bluedot-innovation/models 5.1.30 → 5.1.31

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/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 5.1.31
2
+ - Validated Travel Direction/Speed conditions cannot be added to a Zone that contains large(low accuracy) Geofences.
3
+
1
4
  ## 5.1.30
2
5
  - Added DayRange validation
3
6
 
@@ -339,6 +339,35 @@ describe('Zone', () => {
339
339
  })
340
340
  })
341
341
 
342
+ it('should throw an error when a zone has a low accuracy fence with Speed/travelDirection condition', (done) => {
343
+ zone.conditions.push({
344
+ "type": "speed",
345
+ "minimumSpeed": 0,
346
+ "maximumSpeed": 68.33142000000001
347
+ })
348
+ zone.geofeatures = [
349
+ {
350
+ "type": "boundingBox",
351
+ "name": "Test BoundingBox",
352
+ "accuracy": "low",
353
+ "northEast": {
354
+ "latitude": -37.81794619852986,
355
+ "longitude": 144.97999548912048
356
+ },
357
+ "southWest": {
358
+ "latitude": -37.81846319511329,
359
+ "longitude": 144.97921228408813
360
+ }
361
+ }
362
+ ]
363
+ let Zone = new ZoneModel(zone)
364
+ Zone.validate((err, res) => {
365
+ assert.exists(err, 'err object should exist')
366
+ assert.match(err.message, /(This Zone contains one or more large Geofences. Travel Direction\/Speed conditions cannot be added to a Zone that contains large Geofences.)/i)
367
+ done()
368
+ })
369
+ })
370
+
342
371
  describe('should throw an error when any of the values of the array contains a falsy value', () => {
343
372
  let arrayTypeFields = rules.arrayTypeFields
344
373
 
@@ -0,0 +1,4 @@
1
+ exports.BEACON = 'beacon'
2
+ exports.LOW_ACCURACY = 'low'
3
+ exports.TRAVEL_DIRECTION = 'travelDirection'
4
+ exports.SPEED = 'speed'
package/helpers.js CHANGED
@@ -2,6 +2,7 @@ var equal = require('deep-equal');
2
2
  const { clean } = require('./utils');
3
3
  const area = require('@turf/area').default;
4
4
  const {polygon} = require('@turf/helpers');
5
+ const {BEACON, LOW_ACCURACY, TRAVEL_DIRECTION, SPEED} = require('./constants/commonConstants')
5
6
 
6
7
  let helpers = {}
7
8
 
@@ -165,6 +166,32 @@ helpers.checkArrayContainsValidValues = function(document, properties, next) {
165
166
  return next()
166
167
  }
167
168
 
169
+ helpers.checkValidLowAccuracyFence = function(zone, next) {
170
+ let hasLowAccuracyFence = false;
171
+
172
+ if (!zone.geofeatures || !zone.geofeatures.length) return next()
173
+ zone.geofeatures.forEach(geofeature => {
174
+ if(!geofeature) return next(new Error("Geofeature must have a type"))
175
+ if(geofeature.type !== BEACON && geofeature.accuracy === LOW_ACCURACY){
176
+ hasLowAccuracyFence = true
177
+ }
178
+ });
179
+
180
+ if(hasLowAccuracyFence) {
181
+ if (!zone.conditions || !zone.conditions.length) return next()
182
+
183
+ const hasSpeedOrDirection = zone.conditions.find(condition => {
184
+ if(!condition) return next(new Error("condition must have a type"))
185
+ return (condition.type === TRAVEL_DIRECTION) || (condition.type === SPEED)
186
+ })
187
+
188
+ if(hasSpeedOrDirection) {
189
+ return next(new Error("This Zone contains one or more large Geofences. Travel Direction/Speed conditions cannot be added to a Zone that contains large Geofences."))
190
+ }
191
+ }
192
+ next()
193
+ }
194
+
168
195
  helpers.validateNotNull = function(value) {
169
196
  return value !== null
170
197
  }
package/models/Zone.js CHANGED
@@ -141,7 +141,8 @@ zoneSchema.pre('validate', function(next) {
141
141
  new Promise((resolve, reject) => helpers.checkArrayContainsValidValues(this, ['geofeatures', 'actions', 'conditions', 'tags'], resolve)),
142
142
  new Promise((resolve, reject) => helpers.checkConditionsSuitableForBeacon(this, resolve)),
143
143
  new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
144
- new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve))
144
+ new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve)),
145
+ new Promise((resolve, reject) => helpers.checkValidLowAccuracyFence(this, resolve))
145
146
  ]).then(results => {
146
147
  for( let result of results ) {
147
148
  if( result )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.30",
3
+ "version": "5.1.31",
4
4
  "main": "Models.js",
5
5
  "scripts": {
6
6
  "lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",