@bluedot-innovation/models 6.1.1 → 6.1.2-alpha.2

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,7 @@
1
+ ## 6.1.2
2
+ - Updated zone destinationId to accept null values
3
+ - Added zone destinationId validation on zone geofeatures
4
+
1
5
  ## 6.1.1
2
6
  - Add destinationId to Zone model
3
7
  - Edit features.eta.enabled to Account model
@@ -93,6 +93,57 @@ describe('Zone', () => {
93
93
  assert.strictEqual(zone.tags[0], "tag")
94
94
  assert.strictEqual(zone.tags[1], "1")
95
95
  })
96
+
97
+ it('should save without errors when eta.destinationId is null', (done) => {
98
+ zone.eta = {
99
+ "destinationId": null
100
+ }
101
+ let Zone = new ZoneModel(zone)
102
+ Zone.validate((err, res) => {
103
+ assert.notExists(res)
104
+ assert.notExists(err)
105
+ done()
106
+ })
107
+ })
108
+
109
+ it('should save without errors when eta.destinationId is set and there is a single polygon geofeature', (done) => {
110
+ zone.eta = {
111
+ "destinationId": "test-destination-id"
112
+ }
113
+ zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^polygon$/))
114
+ let Zone = new ZoneModel(zone)
115
+ Zone.validate((err, res) => {
116
+ assert.notExists(res)
117
+ assert.notExists(err)
118
+ done()
119
+ })
120
+ })
121
+
122
+ it('should save without errors when eta.destinationId is set and there is a single boundingBox geofeature', (done) => {
123
+ zone.eta = {
124
+ "destinationId": "test-destination-id"
125
+ }
126
+ zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^boundingBox$/))
127
+ let Zone = new ZoneModel(zone)
128
+ Zone.validate((err, res) => {
129
+ assert.notExists(res)
130
+ assert.notExists(err)
131
+ done()
132
+ })
133
+ })
134
+ it('should save without errors when eta.destinationId is set and there is a single circle geofeature', (done) => {
135
+
136
+ zone.eta = {
137
+ "destinationId": "test-destination-id"
138
+ }
139
+ zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^circle$/))
140
+ let Zone = new ZoneModel(zone)
141
+ Zone.validate((err, res) => {
142
+ assert.notExists(res)
143
+ assert.notExists(err)
144
+ done()
145
+ })
146
+ })
96
147
  })
97
148
 
98
149
  describe('Invalid Zone ', () => {
@@ -401,5 +452,43 @@ describe('Zone', () => {
401
452
  done()
402
453
  })
403
454
  })
455
+
456
+ it('should not throw an error when eta.destinationId is set and there is a single geoline geofeature', (done) => {
457
+ zone.eta = {
458
+ "destinationId": "test-destination-id"
459
+ }
460
+ zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^geoline$/))
461
+ let Zone = new ZoneModel(zone)
462
+ Zone.validate((err, res) => {
463
+ assert.exists(err, 'err object should exist')
464
+ assert.match(err.message, /(destinationId cannot be set on zones with a geoline or beacon)/i)
465
+ done()
466
+ })
467
+ })
468
+
469
+ it('should not throw an error when eta.destinationId is set and there is a single beacon geofeature', (done) => {
470
+ zone.eta = {
471
+ "destinationId": "test-destination-id"
472
+ }
473
+ zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^beacon$/))
474
+ let Zone = new ZoneModel(zone)
475
+ Zone.validate((err, res) => {
476
+ assert.exists(err, 'err object should exist')
477
+ assert.match(err.message, /(destinationId cannot be set on zones with a geoline or beacon)/i)
478
+ done()
479
+ })
480
+ })
481
+
482
+ it('should not throw an error when eta.destinationId is set and there is more than one geofeatue', (done) => {
483
+ zone.eta = {
484
+ "destinationId": "test-destination-id"
485
+ }
486
+ let Zone = new ZoneModel(zone)
487
+ Zone.validate((err, res) => {
488
+ assert.exists(err, 'err object should exist')
489
+ assert.match(err.message, /(destinationId can only be set on zones with a single geofeature)/i)
490
+ done()
491
+ })
492
+ })
404
493
  })
405
494
  })
package/helpers.js CHANGED
@@ -240,8 +240,30 @@ helpers.ensureNonEmptyGeofeatures = function(zone, next){
240
240
  next()
241
241
  }
242
242
 
243
- helpers.validateEtaDestinationId = function(etaDestinationId) {
244
- return !(etaDestinationId.length > 128)
243
+ helpers.validateEtaDestinationIdLength = function(etaDestinationId) {
244
+ if(etaDestinationId){
245
+ return !(etaDestinationId.length > 128)
246
+ }
247
+ return true
248
+ }
249
+
250
+ helpers.validateEtaDestinationSingleGeofeature = function(etaDestinationId) {
251
+ if(etaDestinationId){
252
+ const geofeatures = this.ownerDocument().geofeatures
253
+ if(!geofeatures.length){
254
+ return false
255
+ }
256
+ return geofeatures.length === 1
257
+ }
258
+ return true
259
+ }
260
+
261
+ helpers.validateEtaDestinationIdGeofeatureType = function(etaDestinationId) {
262
+ const geofeatures = this.ownerDocument().geofeatures
263
+ if(etaDestinationId && geofeatures.length){
264
+ return !(geofeatures[0].type === 'geoline' || geofeatures[0].type === 'beacon')
265
+ }
266
+ return true
245
267
  }
246
268
 
247
269
  helpers.validateTriggerWebhooks = function(webhooks) {
package/models/Zone.js CHANGED
@@ -121,7 +121,19 @@ let zoneSchema = new Schema({
121
121
  state: {type: String, default: StateEnum.ACTIVE, select: false},
122
122
  accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}},
123
123
  eta: zoneEtaSchema
124
- }, {collection: 'zone'})
124
+ }, {collection: 'zone', autoIndex: false})
125
+
126
+ zoneSchema.index(
127
+ {
128
+ projectId: 1,
129
+ 'eta.destinationId': 1
130
+ },
131
+ {
132
+ unique: true,
133
+ partialFilterExpression: { 'eta.destinationId': { $exists:true, $type:"string" } },
134
+ name: "destinationUniqueWithinProject"
135
+ })
136
+
125
137
 
126
138
  // Mongoose-json-patch
127
139
  // https://www.npmjs.com/package/mongoose-json-patch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "6.1.1",
3
+ "version": "6.1.2-alpha.2",
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",
@@ -2,9 +2,13 @@ let mongoose = require('mongoose')
2
2
  let helpers = require('../helpers')
3
3
 
4
4
  let ZoneEtaSchema = new mongoose.Schema({
5
- destinationId: {type: String, validate: { validator: helpers.validateEtaDestinationId, msg: 'destinationId must be no more than 128 characters long' }}
5
+ destinationId: {type: String, validate: [
6
+ { validator: helpers.validateEtaDestinationIdLength, msg: 'destinationId must be no more than 128 characters long' },
7
+ { validator: helpers.validateEtaDestinationSingleGeofeature, msg: 'destinationId can only be set on zones with a single geofeature' },
8
+ { validator: helpers.validateEtaDestinationIdGeofeatureType, msg: 'destinationId cannot be set on zones with a geoline or beacon' }
9
+ ]}
6
10
  }, {
7
11
  _id: false
8
12
  })
9
13
 
10
- module.exports = ZoneEtaSchema
14
+ module.exports = ZoneEtaSchema