@bluedot-innovation/models 5.1.28 → 5.1.30

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.30
2
+ - Added DayRange validation
3
+
4
+ ## 5.1.29
5
+ - Validated BoundingBox and Polygon's area should be at least 70 square metres
6
+
1
7
  ## 5.1.28
2
8
  - Fixed PATCH tags with lowercase and unique strings
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
  })
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
@@ -1,7 +1,7 @@
1
1
  let mongoose = require('mongoose')
2
2
 
3
3
  let schema = {
4
- startDate: {type: Date},
4
+ startDate: {type: Date, validate: validateDateRange},
5
5
  endDate: {type: Date}
6
6
  }
7
7
 
@@ -11,7 +11,7 @@ let DayRangeConditionSchema = new mongoose.Schema(schema, {
11
11
  })
12
12
 
13
13
  DayRangeConditionSchema.add({
14
- type: {type: String, required: true, validate: {isAsync: true, validator: validateDateRange}}
14
+ type: {type: String, required: true, validate: validateDateRange}
15
15
  })
16
16
 
17
17
  function validateDateRange (type, callback) {
@@ -20,7 +20,7 @@ function validateDateRange (type, callback) {
20
20
 
21
21
  if (!startDate && !endDate) return callback(false, `Must have a startDate, endDate, or both.`)
22
22
 
23
- if (!startDate) startDate = new Date(this.startDate)
23
+ if (!startDate) startDate = new Date(startDate)
24
24
 
25
25
  if (!endDate) endDate = new Date(endDate)
26
26
 
@@ -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.28",
3
+ "version": "5.1.30",
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",