@bluedot-innovation/models 5.1.32 → 5.1.33

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.33
2
+ - Checked for duplicate geofeatures _ids
3
+
1
4
  ## 5.1.32
2
5
  - Using Double type for Circle fence radius
3
6
 
@@ -4,7 +4,7 @@ let chai = require('chai')
4
4
  let assert = chai.assert
5
5
  let rules = require('./zone.rules.json')
6
6
  let helpers = require('../helpers')
7
- let { FALSY_VALUES } = require('../../constants/tests')
7
+ let { FALSY_VALUES, GEOFEATURE_ID } = require('../../constants/tests')
8
8
 
9
9
  let ZoneModel = require('../../Models').Zone(mongoose)
10
10
  let ZoneModelBuiltInMongoose = require('../../Models').Zone()
@@ -379,5 +379,16 @@ describe('Zone', () => {
379
379
  })
380
380
  })
381
381
  })
382
+
383
+ it('should throw an error when geofeatures have the same _id', (done) => {
384
+ zone.geofeatures[0]._id = GEOFEATURE_ID
385
+ zone.geofeatures[1]._id = GEOFEATURE_ID
386
+ let Zone = new ZoneModel(zone)
387
+ Zone.validate((err, res) => {
388
+ assert.exists(err, 'err object should exist')
389
+ assert.match(err.message, /(Duplicate values are not allowed for geofeatures _id.)/i)
390
+ done()
391
+ })
392
+ })
382
393
  })
383
394
  })
@@ -1 +1,2 @@
1
- exports.FALSY_VALUES = [null, false, NaN, "", undefined]
1
+ exports.FALSY_VALUES = [null, false, NaN, "", undefined]
2
+ exports.GEOFEATURE_ID = 'd4dc6266-3380-4f61-b534-3ebe1258a545'
package/helpers.js CHANGED
@@ -192,6 +192,24 @@ helpers.checkValidLowAccuracyFence = function(zone, next) {
192
192
  next()
193
193
  }
194
194
 
195
+ helpers.checkDuplicateGeofeaturesId = function(zone, next) {
196
+ if (!zone.geofeatures || !zone.geofeatures.length) return next()
197
+ zone.geofeatures.forEach(geofeature => {
198
+ if(!geofeature) return next(new Error("Geofeature must have a type"))
199
+ })
200
+
201
+ if (zone.geofeatures && zone.geofeatures.length > 0) {
202
+ const geofeatureIds = new Set()
203
+ const hasDuplicates = zone.geofeatures.some(geofeature => {
204
+ return geofeatureIds.size === geofeatureIds.add(geofeature._id).size
205
+ })
206
+ if (hasDuplicates) {
207
+ return next(new Error("Duplicate values are not allowed for geofeatures _id."))
208
+ }
209
+ }
210
+ next()
211
+ }
212
+
195
213
  helpers.validateNotNull = function(value) {
196
214
  return value !== null
197
215
  }
package/models/Zone.js CHANGED
@@ -142,7 +142,8 @@ zoneSchema.pre('validate', function(next) {
142
142
  new Promise((resolve, reject) => helpers.checkConditionsSuitableForBeacon(this, resolve)),
143
143
  new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
144
144
  new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve)),
145
- new Promise((resolve, reject) => helpers.checkValidLowAccuracyFence(this, resolve))
145
+ new Promise((resolve, reject) => helpers.checkValidLowAccuracyFence(this, resolve)),
146
+ new Promise((resolve, reject) => helpers.checkDuplicateGeofeaturesId(this, resolve))
146
147
  ]).then(results => {
147
148
  for( let result of results ) {
148
149
  if( result )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.32",
3
+ "version": "5.1.33",
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",