@bluedot-innovation/models 5.1.20 → 5.1.22

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,9 @@
1
+ ## 5.1.22
2
+ - Validate zones' actions and conditions for missing type
3
+
4
+ ## 5.1.21
5
+ - Remote config update
6
+
1
7
  ## 5.1.20
2
8
  - Validate projectId when posting a zone
3
9
 
@@ -121,6 +121,29 @@ describe('Zone', () => {
121
121
  })
122
122
  })
123
123
 
124
+ it('should throw an error when action type is missing', (done) => {
125
+ delete zone.actions[0].type
126
+ delete zone.actions[1].type
127
+ let Zone = new ZoneModel(zone)
128
+ Zone.validate((err, res) => {
129
+ assert.exists(err, 'err object should exist')
130
+ assert.isObject(err)
131
+ assert.match(err.message, /(Action requires a type of `url` or `message`.)/i)
132
+ done()
133
+ })
134
+ })
135
+
136
+ it('should throw an error when action object is empty', (done) => {
137
+ zone.actions = [{}]
138
+ let Zone = new ZoneModel(zone)
139
+ Zone.validate((err, res) => {
140
+ assert.exists(err, 'err object should exist')
141
+ assert.isObject(err)
142
+ assert.match(err.message, /(Action requires a type of `url` or `message`.)/i)
143
+ done()
144
+ })
145
+ })
146
+
124
147
  it('should throw an error when a condition is invalid', (done) => {
125
148
  zone.conditions[0].startDate = 'abc'
126
149
  let Zone = new ZoneModel(zone)
@@ -132,6 +155,28 @@ describe('Zone', () => {
132
155
  })
133
156
  })
134
157
 
158
+ it('should throw an error when a condition type is missing', (done) => {
159
+ delete zone.conditions[0].type
160
+ let Zone = new ZoneModel(zone)
161
+ Zone.validate((err, res) => {
162
+ assert.exists(err, 'err object should exist')
163
+ assert.isObject(err)
164
+ assert.match(err.message, /(Condition requires a type of `dayRange`, `speed`, `timeActive`, `travelDirection` or `travelPath`.)/i)
165
+ done()
166
+ })
167
+ })
168
+
169
+ it('should throw an error when a condition object is empty', (done) => {
170
+ zone.conditions = [{}]
171
+ let Zone = new ZoneModel(zone)
172
+ Zone.validate((err, res) => {
173
+ assert.exists(err, 'err object should exist')
174
+ assert.isObject(err)
175
+ assert.match(err.message, /(Condition requires a type of `dayRange`, `speed`, `timeActive`, `travelDirection` or `travelPath`.)/i)
176
+ done()
177
+ })
178
+ })
179
+
135
180
  it('should throw an error when custom data is null', (done) => {
136
181
  zone.customData = null
137
182
  let Zone = new ZoneModel(zone)
@@ -168,7 +213,7 @@ describe('Zone', () => {
168
213
  zone.customData["name#3"] = "A Custom action"
169
214
  let Zone = new ZoneModel(zone)
170
215
  Zone.validate((err, res) => {
171
- assert.match(err.message, /(CustomData key should have alpha numeric and _ characters only)/i);
216
+ assert.match(err.message, /(CustomData key should have alpha numeric, - and _ characters only)/i);
172
217
  done()
173
218
  })
174
219
  })
package/helpers.js CHANGED
@@ -19,13 +19,12 @@ helpers.threeOrMoreVertices = function(vertices) {
19
19
  }
20
20
 
21
21
  helpers.validateCustomData = function (data) {
22
-
23
22
  if(!isValidObject(data)){
24
23
  return false
25
24
  }
26
25
 
27
26
  const customDataKeys = Object.keys(data)
28
- const alphaNumericValidation = new RegExp(/^[\w\s]+$/)
27
+ const alphaNumericValidation = new RegExp(/^[\w\s-]+$/)
29
28
  return customDataKeys.every(key => {
30
29
  return alphaNumericValidation.test(key)
31
30
  });
package/models/Zone.js CHANGED
@@ -31,7 +31,7 @@ let helpers = require('../helpers')
31
31
  // ////// //
32
32
 
33
33
  let customDataValidators = [
34
- {validator: helpers.validateCustomData, msg: 'CustomData key should have alpha numeric and _ characters only'},
34
+ {validator: helpers.validateCustomData, msg: 'CustomData key should have alpha numeric, - and _ characters only'},
35
35
  {validator: helpers.checkDuplicateKeys, msg: 'CustomData key should be unique'},
36
36
  {validator: helpers.isCustomDataValuesNotEmpty, msg: 'CustomData value should not be empty'},
37
37
  {validator: helpers.isValidCustomDataCount, msg: 'Only 5 custom data fields are allowed to be added per zone'}
@@ -44,9 +44,23 @@ const tagsFormation = (tags) => {
44
44
  let actionSchema = new Schema({},
45
45
  { discriminatorKey: 'type', _id: false })
46
46
 
47
+ actionSchema.pre('validate', function(next){
48
+ if(this.type === undefined){
49
+ return next(new Error('Action requires a type of `url` or `message`.'))
50
+ }
51
+ next()
52
+ })
53
+
47
54
  let conditionSchema = new Schema({},
48
55
  { discriminatorKey: 'type', _id: false })
49
56
 
57
+ conditionSchema.pre('validate', function(next){
58
+ if(this.type === undefined){
59
+ return next(new Error('Condition requires a type of `dayRange`, `speed`, `timeActive`, `travelDirection` or `travelPath`.'))
60
+ }
61
+ next()
62
+ })
63
+
50
64
  let geofeatureSchema = new Schema({},
51
65
  { discriminatorKey: 'type', _id: false })
52
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.20",
3
+ "version": "5.1.22",
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",
@@ -7,11 +7,6 @@ let useCaseTypeEnum = {
7
7
  message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
8
8
  }
9
9
 
10
- let triggerEngineEnum = {
11
- values: ['BD', 'Hybrid', 'Native'],
12
- message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
13
- }
14
-
15
10
  let AndroidSchema = new mongoose.Schema({
16
11
  triggeringOnWifiAccuracyThreshold: {type: Number, required: true},
17
12
  triggeringOnGpsAccuracyThreshold: {type: Number, required: true},
@@ -26,8 +21,7 @@ let AndroidSchema = new mongoose.Schema({
26
21
  applicationLoggingEnabled: {type: Boolean, required: true}, //Local logging on the device
27
22
  telemetry : telemetrySchema,
28
23
  useCaseType : {type: String, enum: useCaseTypeEnum, required: true},
29
- maximumPageSize: {type: Number, required: true, max: 256},
30
- triggerEngine: {type: String, enum: triggerEngineEnum, required: true}
24
+ maximumPageSize: {type: Number, required: true}
31
25
  }, {
32
26
  _id: false
33
27
  })
@@ -9,6 +9,11 @@ let useCaseTypeEnum = {
9
9
  message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
10
10
  }
11
11
 
12
+ let triggerEngineEnum = {
13
+ values: ['BD', 'Hybrid', 'Native'],
14
+ message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
15
+ }
16
+
12
17
  let IosSchema = new mongoose.Schema({
13
18
  triggeringOnWifiAccuracyThreshold : {type: Number, required: true},
14
19
  triggeringOnGpsAccuracyThreshold : {type: Number, required: true},
@@ -23,14 +28,14 @@ let IosSchema = new mongoose.Schema({
23
28
  triggeringOnPollingLocationFurtherThreshold : {type: Number, required: true},
24
29
  triggeringOnPollingLocationFarthestThreshold : {type: Number, required: true},
25
30
  focalDistanceAccuracyCoefficient : {type: Number, required: true},
26
- accelerometerEnablementThreshold : {type: Number, required: true},
27
31
  endpointUrl : {type: String, required: true, validate: {validator: validators.validateUrl, msg: 'endpoint Url is required'}},
28
32
  filteringEnabled : {type: Boolean, required: true},
29
33
  applicationLoggingEnabled : {type: Boolean, required: true},
30
34
  useCaseType : {type: String, enum: useCaseTypeEnum, required: true},
31
35
  telemetry: telemetrySchema,
32
36
  accuracySuppression: accuracySuppressionSchema,
33
- starvationDetector: starvationDetectionSchema
37
+ starvationDetector: starvationDetectionSchema,
38
+ triggerEngine: {type: String, enum: triggerEngineEnum, required: true}
34
39
  }, {
35
40
  _id: false
36
41
  })
@@ -20,7 +20,8 @@ let telemetryBlacklistEnum = {
20
20
  "heading",
21
21
  "fenceCrossed",
22
22
  "rulesDownloadBegin",
23
- "rulesDownloadEnd" ],
23
+ "rulesDownloadEnd",
24
+ "debug"],
24
25
  message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
25
26
  }
26
27
 
package/utils/index.js CHANGED
@@ -17,21 +17,26 @@ const notSameThreeInARow = function(list) {
17
17
  const cleanList = clean(list)
18
18
  let hasSame = false;
19
19
 
20
- for (var i = 0; i < cleanList.length; i++) {
21
- const itemA = cleanList[i]
22
- const itemB = cleanList[i - 1]
23
- const itemC = cleanList[i - 2]
24
-
25
- if (equal(itemA, itemB)) {
26
- hasSame = true;
27
- }
28
- if (equal(itemA, itemC)) {
20
+ for (var i = 2; i < cleanList.length; i++) {
21
+ const itemA = trimLatLon(cleanList[i])
22
+ const itemB = trimLatLon(cleanList[i - 1])
23
+ const itemC = trimLatLon(cleanList[i - 2])
24
+
25
+ if (equal(itemA, itemB) || equal(itemA, itemC) || equal(itemB, itemC)) {
29
26
  hasSame = true;
27
+ break;
30
28
  }
31
29
  }
32
30
  return !hasSame;
33
31
  }
34
32
 
33
+ const trimLatLon = function(item) {
34
+ let result = {}
35
+ result.latitude = trimDecimals(item.latitude, 6)
36
+ result.longitude = trimDecimals(item.longitude, 6)
37
+ return result;
38
+ }
39
+
35
40
  module.exports = {
36
41
  clean,
37
42
  trimDecimals,