@bluedot-innovation/models 5.1.27 → 5.1.29

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.29
2
+ - Validated BoundingBox and Polygon's area should be at least 70 square metres
3
+
4
+ ## 5.1.28
5
+ - Fixed PATCH tags with lowercase and unique strings
6
+
1
7
  ## 5.1.27
2
8
  - Fixed validations for `macAddress` in Beacon model
3
9
 
@@ -93,5 +93,28 @@ describe('BoundingBox', () => {
93
93
  done()
94
94
  })
95
95
  })
96
+
97
+ it('should fail when area is less than 70 square metres', (done) => {
98
+ boundingBox.geometry ={
99
+ type: "Polygon",
100
+ coordinates: [
101
+ [
102
+ [ 145.1688126217282, -37.95960975490519 ],
103
+ [ 145.1688126217282, -37.95960975490519 ],
104
+ [ 145.1688112806237, -37.95960975490519 ],
105
+ [ 145.1688112806237, -37.95960975490519 ],
106
+ [ 145.1688126217282, -37.95960975490519 ]
107
+ ]
108
+ ]
109
+ }
110
+ const BoundingBox = new BoundingBoxModel(boundingBox)
111
+
112
+ BoundingBox.validate((err, res) => {
113
+ assert.exists(err, 'err object should exist')
114
+ assert.isObject(err)
115
+ assert.match(err.message, /(Bounding box area should be at least 70 square metres)/i)
116
+ done()
117
+ })
118
+ })
96
119
  })
97
120
  })
@@ -138,5 +138,28 @@ describe('Polygon', () => {
138
138
  })
139
139
  })
140
140
  })
141
+
142
+ it('should fail when area is less than 70 square metres', (done) => {
143
+ polygon.geometry ={
144
+ type: "Polygon",
145
+ coordinates: [
146
+ [
147
+ [ 143.95608938213672, -37.851879047856684 ],
148
+ [ 143.95611083980884, -37.85191293379294 ],
149
+ [ 143.95612693306293, -37.851879047856684 ],
150
+ [ 143.95608938213672, -37.851879047856684 ],
151
+ [ 143.95608938213672, -37.851879047856684 ]
152
+ ]
153
+ ]
154
+ }
155
+ let Polygon = new PolygonModel(polygon)
156
+
157
+ Polygon.validate((err, res) => {
158
+ assert.exists(err, 'err object should exist')
159
+ assert.isObject(err)
160
+ assert.match(err.message, /(Polygon area should be at least 70 square metres)/i)
161
+ done()
162
+ })
163
+ })
141
164
  })
142
165
  })
@@ -23,4 +23,10 @@ helpers.testNullValue = (docObject, model, callback) => {
23
23
  })
24
24
  }
25
25
 
26
+ helpers.removeDuplicatedTags = zone => {
27
+ if (zone.tags && zone.tags.length) {
28
+ zone.tags = [...new Set(zone.tags.map(tag => typeof tag === 'string' ? tag.toLowerCase() : String(tag)))]
29
+ }
30
+ }
31
+
26
32
  module.exports = helpers
@@ -86,16 +86,12 @@ describe('Zone', () => {
86
86
  })
87
87
  })
88
88
 
89
- it('should set tags to lowercase and remove the duplicated tag', (done) => {
89
+ it('should set tags to lowercase and remove the duplicated tag', () => {
90
90
  zone.tags = ["TAG", 1, "TaG", 1]
91
-
92
- let Zone = new ZoneModel(zone)
93
- assert.exists(Zone._doc)
94
- assert.exists(Zone._doc.tags)
95
- assert.strictEqual(Zone._doc.tags.length, 2)
96
- assert.strictEqual(Zone._doc.tags[0], "tag")
97
- assert.strictEqual(Zone._doc.tags[1], "1")
98
- done()
91
+ helpers.removeDuplicatedTags(zone)
92
+ assert.strictEqual(zone.tags.length, 2)
93
+ assert.strictEqual(zone.tags[0], "tag")
94
+ assert.strictEqual(zone.tags[1], "1")
99
95
  })
100
96
  })
101
97
 
package/helpers.js CHANGED
@@ -1,5 +1,7 @@
1
1
  var equal = require('deep-equal');
2
2
  const { clean } = require('./utils');
3
+ const area = require('@turf/area').default;
4
+ const {polygon} = require('@turf/helpers');
3
5
 
4
6
  let helpers = {}
5
7
 
@@ -176,4 +178,16 @@ const isValidObject = data => {
176
178
  return !(data === null || typeof data === 'undefined'|| data.constructor !== Object)
177
179
  }
178
180
 
181
+ helpers.calculateArea = geometry => {
182
+ if (geometry && geometry.coordinates && geometry.coordinates.length > 0) {
183
+ const featurePolygon = polygon(geometry.coordinates)
184
+ const calculatedArea = area(featurePolygon)
185
+ if (calculatedArea < 70) {
186
+ return false
187
+ }
188
+ return true
189
+ }
190
+ return false
191
+ }
192
+
179
193
  module.exports = helpers
package/models/Zone.js CHANGED
@@ -37,8 +37,10 @@ let customDataValidators = [
37
37
  {validator: helpers.isValidCustomDataCount, msg: 'Only 5 custom data fields are allowed to be added per zone'}
38
38
  ];
39
39
 
40
- const tagsFormation = (tags) => {
41
- return [...new Set(tags.map(tag => typeof tag === 'string' ? tag.toLowerCase(): tag))]
40
+ const tagsFormation = (zone) => {
41
+ if (zone.tags && zone.tags.length) {
42
+ zone.tags = [...new Set(zone.tags.map(tag => typeof tag === 'string' ? tag.toLowerCase() : String(tag)))]
43
+ }
42
44
  }
43
45
 
44
46
  let actionSchema = new Schema({},
@@ -84,7 +86,7 @@ let zoneSchema = new Schema({
84
86
  message: '{VALUE} is not a number'
85
87
  }
86
88
  },
87
- tags: {type: [String], set : tagsFormation, validate: helpers.validateNotNull},
89
+ tags: {type: [String], validate: helpers.validateNotNull},
88
90
  conditions: {
89
91
  type: [conditionSchema],
90
92
  validate: {
@@ -145,6 +147,7 @@ zoneSchema.pre('validate', function(next) {
145
147
  if( result )
146
148
  return next(result)
147
149
  }
150
+ tagsFormation(this)
148
151
  next()
149
152
  }).catch((error) => console.log('Validation error ', error));
150
153
  })
@@ -6,7 +6,10 @@ let helpers = require('../../helpers')
6
6
 
7
7
  const schema = {
8
8
  _id: {type: String, default: uuidv4},
9
- geometry: {type: {}},
9
+ geometry: {type: {}, validate: {
10
+ validator: helpers.calculateArea,
11
+ message: 'Bounding box area should be at least 70 square metres'
12
+ }},
10
13
  name: {type: String, required: [true, 'name is required.']},
11
14
  color: String,
12
15
  accuracy: {type: String, enum: Object.values(ZoneEnum.fenceAccuracy)},
@@ -8,7 +8,10 @@ const helpers = require('../../helpers')
8
8
 
9
9
  const schema = {
10
10
  _id: {type: String, default: uuidv4},
11
- geometry: {type: {}},
11
+ geometry: {type: {}, validate: {
12
+ validator: helpers.calculateArea,
13
+ message: 'Polygon area should be at least 70 square metres'
14
+ }},
12
15
  name: {type: String, required: [true, 'name is required.']},
13
16
  color: String,
14
17
  accuracy: {type: String, enum: Object.values(ZoneEnum.fenceAccuracy)},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "5.1.27",
3
+ "version": "5.1.29",
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",
@@ -31,6 +31,7 @@
31
31
  "@bluedot-innovation/enums": "^2.1.5",
32
32
  "@bluedot-innovation/mongoose-json-patch": "0.0.6",
33
33
  "@mongoosejs/double": "^0.1.2",
34
+ "@turf/area": "^6.0.1",
34
35
  "deep-equal": "^1.0.1",
35
36
  "fast-deep-equal": "^2.0.1",
36
37
  "lodash.clonedeep": "4.5.0",