@bluedot-innovation/models 3.0.11 → 3.0.13

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
+ ## 3.0.13
2
+ - `Polygon` and `Polyline` "same vertices" check measures accurate to 6 decimal places only.
3
+
4
+ ## 3.0.12
5
+ - Enforce `beaconType` capitalization on `Beacon` model
6
+
1
7
  ## 3.0.11
2
8
  - Added `lastSyncTime` to the `project` model
3
9
 
@@ -52,10 +52,9 @@ describe('Beacon', () => {
52
52
  })
53
53
  })
54
54
 
55
- describe('check each required property of the model and validate that it is present', () => {
55
+ describe('check each required property of the model and validate that it is present', (done) => {
56
56
  let dependentRequiredFields = rules.dependentRequiredFields
57
57
  dependentRequiredFields.forEach((parent) => {
58
- console.log("parent", parent)
59
58
  parent.pairings.forEach((i) => {
60
59
  i.requiredFields.forEach((field) => {
61
60
  it(`should throw an error with property '${parent.property}' if '${i.value}' is selected and '${field}' is invalid`, (done) => {
@@ -79,16 +78,33 @@ describe('Beacon', () => {
79
78
 
80
79
  describe('check each required property of the model and validate that it is present', () => {
81
80
  let requiredFields = rules.requiredFields
82
- requiredFields.forEach((i) => {
83
- it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
81
+ requiredFields.forEach((requiredField) => {
82
+ it(`should throw an error with property '${requiredField}' if '${requiredField}' is not present`, (done) => {
84
83
  // delete i from geofeatures' beacon child
85
- delete beacon[i]
84
+ delete beacon[requiredField]
86
85
  let Beacon = new BeaconModel(beacon)
87
86
  Beacon.validate((err, res) => {
88
87
  assert.notExists(res)
89
88
  assert.exists(err, 'err object should exist')
90
89
  assert.isObject(err)
91
- assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
90
+ assert.nestedProperty(err, `errors.${requiredField}`, `error should have a ${requiredField} property`)
91
+ done()
92
+ })
93
+ })
94
+ })
95
+ })
96
+
97
+ describe('should fail validation if the beaconType does not match the enum', () => {
98
+ const failingBeaconTypes = ['ios', 'iOS', 'android', 'ANDROID', 'both', 'BOTH'];
99
+
100
+ failingBeaconTypes.forEach(beaconType => {
101
+ it(beaconType, (done) => {
102
+ let Beacon = new BeaconModel(beacon);
103
+ Beacon.beaconType = beaconType;
104
+
105
+ Beacon.validate((err, res) => {
106
+ assert.exists(err)
107
+ assert.match(err, /(is not a valid enum value)/i)
92
108
  done()
93
109
  })
94
110
  })
@@ -1,17 +1,6 @@
1
- let mongoose = require('mongoose')
2
-
3
- mongoose.Promise = global.Promise
4
-
5
1
  let PolygonModel = require('../../../Models').Polygon(mongoose)
6
2
  let PolygonModelBuiltInMongoose = require('../../../Models').Polygon()
7
- let describe = require('mocha').describe
8
- let it = require('mocha').it
9
- let chai = require('chai')
10
-
11
- let assert = chai.assert
12
- let mocha = require('mocha')
13
- let beforeEach = mocha.beforeEach
14
-
3
+ const { VERTICES_TOO_CLOSE, CANT_HAVE_THREE_OR_MORE_VERTICES } = require('../../../constants/errorMessages')
15
4
  let rules = require('./polygon.rules.json')
16
5
 
17
6
  let polygon
@@ -69,7 +58,7 @@ describe('Polygon', () => {
69
58
  })
70
59
  })
71
60
 
72
- it('when it only has less than 3 vertices, it should error', (done) => {
61
+ it('when it has less than 3 vertices, it should error', (done) => {
73
62
  polygon.vertices = [{
74
63
  "latitude": -37.81902680201739,
75
64
  "longitude": 144.9821037054062
@@ -81,18 +70,19 @@ describe('Polygon', () => {
81
70
 
82
71
  let Polygon = new PolygonModel(polygon)
83
72
  Polygon.validate((err, res) => {
84
- assert.match(err, /A polygon must have 3 or more vertices/i)
73
+ const regex = new RegExp(CANT_HAVE_THREE_OR_MORE_VERTICES)
74
+ assert.match(err, regex)
85
75
  done()
86
76
  })
87
77
  })
88
78
 
89
- describe('For any given vertex, if any of the next 2 vertices are the same, it should error', (done) => {
90
- it('next vertice is the same', () => {
79
+ describe('For any given vertex, if any of the next 2 vertices (accurate to 6 decimal points) are the same, it should error', (done) => {
80
+ it('next vertice is the same', (done) => {
91
81
  polygon.vertices = [{
92
- "latitude": -37.81902680201739,
93
- "longitude": 144.9821037054062
82
+ "latitude": -37.81902680201733,
83
+ "longitude": 144.9821037054044
94
84
  }, {
95
- "latitude": -37.81902680201739,
85
+ "latitude": -37.81902680201731,
96
86
  "longitude": 144.9821037054062
97
87
  }, {
98
88
  "latitude": -36.81902680201745,
@@ -104,12 +94,13 @@ describe('Polygon', () => {
104
94
 
105
95
  let Polygon = new PolygonModel(polygon)
106
96
  Polygon.validate((err, res) => {
107
- assert.match(err, /For any given vertex, the next 2 vertices cannot match that vertex./i)
97
+ const regex = new RegExp(VERTICES_TOO_CLOSE)
98
+ assert.match(err, regex)
108
99
  done()
109
100
  })
110
101
  })
111
102
 
112
- it('the one after next is the same', () => {
103
+ it('the one after next is the same', (done) => {
113
104
  polygon.vertices = [{
114
105
  "latitude": -37.81902680201739,
115
106
  "longitude": 144.9821037054062
@@ -126,7 +117,8 @@ describe('Polygon', () => {
126
117
 
127
118
  let Polygon = new PolygonModel(polygon)
128
119
  Polygon.validate((err, res) => {
129
- assert.match(err, /For any given vertex, the next 2 vertices cannot match that vertex./i)
120
+ const regex = new RegExp(VERTICES_TOO_CLOSE)
121
+ assert.match(err, regex)
130
122
  done()
131
123
  })
132
124
  })
@@ -3,8 +3,8 @@ let mongoose = require('mongoose')
3
3
  mongoose.Promise = global.Promise
4
4
 
5
5
  let ZoneBeaconModel = require('../../../Models').ZoneBeacon(mongoose)
6
- let testCase = require('mocha').describe
7
- let assertions = require('mocha').it
6
+ let describe = require('mocha').describe
7
+ let it = require('mocha').it
8
8
  let chai = require('chai')
9
9
 
10
10
  let assert = chai.assert
@@ -19,9 +19,9 @@ beforeEach(() => {
19
19
  zoneBeacon = JSON.parse(JSON.stringify(require('./zoneBeacon.data.json')))
20
20
  })
21
21
 
22
- testCase('ZoneBeacon', () => {
23
- testCase('Valid ZoneBeacon', () => {
24
- assertions('should validate a valid zoneBeacon ', (done) => {
22
+ describe('ZoneBeacon', () => {
23
+ describe('Valid ZoneBeacon', () => {
24
+ it('should validate a valid zoneBeacon ', (done) => {
25
25
  const ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
26
26
  ZoneBeacon.validate((err, res) => {
27
27
  assert.notExists(res)
@@ -31,8 +31,8 @@ testCase('ZoneBeacon', () => {
31
31
  })
32
32
  })
33
33
 
34
- testCase('Invalid ZoneBeacon', () => {
35
- assertions('should not validate a zoneBeacon with invalid ZoneBeacon', (done) => {
34
+ describe('Invalid ZoneBeacon', () => {
35
+ it('should not validate a zoneBeacon with invalid ZoneBeacon', (done) => {
36
36
  delete zoneBeacon[rules.requiredFields[0]]
37
37
  let ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
38
38
  ZoneBeacon.validate((err, res) => {
@@ -41,10 +41,10 @@ testCase('ZoneBeacon', () => {
41
41
  done()
42
42
  })
43
43
  })
44
- testCase('check each required property of the model and validate that it is present', () => {
44
+ describe('check each required property of the model and validate that it is present', () => {
45
45
  let requiredFields = rules.requiredFields
46
46
  requiredFields.map((i) => {
47
- assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
47
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
48
48
  // delete i from geofeatures' zoneBeacon child
49
49
  delete zoneBeacon[i]
50
50
  let ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
@@ -0,0 +1,2 @@
1
+ exports.VERTICES_TOO_CLOSE = 'For any given vertex, the next 2 vertices cannot be too close to it.'
2
+ exports.CANT_HAVE_THREE_OR_MORE_VERTICES = 'A polygon must have 3 or more vertices.'
package/helpers.js CHANGED
@@ -1,11 +1,8 @@
1
1
  var equal = require('deep-equal');
2
+ const { clean } = require('./utils');
2
3
 
3
4
  let helpers = {}
4
5
 
5
- const clean = (item) => {
6
- return item && JSON.parse(JSON.stringify(item))
7
- }
8
-
9
6
  helpers.validateUUIDv4 = function (id) {
10
7
  const expression = '^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'
11
8
  const regex = new RegExp(expression, 'i')
@@ -21,26 +18,6 @@ helpers.threeOrMoreVertices = function(vertices) {
21
18
  return vertices.length >= 3;
22
19
  }
23
20
 
24
- helpers.notSameThreeInARow = function(list) {
25
- const cleanList = clean(list)
26
- let hasSame = false;
27
-
28
- for (var i = 0; i < cleanList.length; i++) {
29
- const itemA = cleanList[i]
30
- const itemB = cleanList[i - 1]
31
- const itemC = cleanList[i - 2]
32
- const isSame = equal(itemA, itemB);
33
- if (equal(itemA, itemB)) {
34
- hasSame = true;
35
- }
36
- if (equal(itemA, itemC)) {
37
- hasSame = true;
38
- }
39
- }
40
-
41
- return !hasSame;
42
- }
43
-
44
21
  helpers.validateCustomData = function (data) {
45
22
  const customDataKeys = Object.keys(data)
46
23
  let hasValidationErrors = false
package/models/Beacon.js CHANGED
@@ -1,9 +1,9 @@
1
- let mongoose = require('mongoose')
2
- let StateEnum = require('@bluedot-innovation/enums').StateEnum
3
- let Point = require('../schemas/Point')
4
- let PointGeometry = require('../schemas/PointGeometry')
5
- let uuidv4 = require('uuid/v4')
6
- let patcher = require('mongoose-json-patch')
1
+ const { BeaconEnum, StateEnum } = require('@bluedot-innovation/enums')
2
+ const PointGeometry = require('../schemas/PointGeometry')
3
+ const patcher = require('mongoose-json-patch')
4
+ const Point = require('../schemas/Point')
5
+ const mongoose = require('mongoose')
6
+ const uuidv4 = require('uuid/v4')
7
7
 
8
8
  let schema = {
9
9
  _id: {type: String, default: uuidv4},
@@ -15,6 +15,7 @@ let schema = {
15
15
  beaconType: {
16
16
  type: String,
17
17
  required: true,
18
+ enum: [BeaconEnum.IOS, BeaconEnum.ANDROID, BeaconEnum.BOTH],
18
19
  validate: {
19
20
  isAsync: true,
20
21
  validator: validateBeaconType,
@@ -1,8 +1,10 @@
1
- let mongoose = require('mongoose')
2
- let StateEnum = require('@bluedot-innovation/enums').StateEnum
3
- let Point = require('../../schemas/Point')
4
- let uuidv4 = require('uuid/v4')
5
- let helpers = require('../../helpers')
1
+ const mongoose = require('mongoose')
2
+ const StateEnum = require('@bluedot-innovation/enums').StateEnum
3
+ const Point = require('../../schemas/Point')
4
+ const uuidv4 = require('uuid/v4')
5
+ const checkDistanceBetweenVertices = require('../../validators/checkDistanceBetweenVertices')
6
+ const { VERTICES_TOO_CLOSE, CANT_HAVE_THREE_OR_MORE_VERTICES } = require('../../constants/errorMessages')
7
+ const helpers = require('../../helpers')
6
8
 
7
9
  const schema = {
8
10
  _id: {type: String, default: uuidv4},
@@ -13,10 +15,10 @@ const schema = {
13
15
  type: [Point], required: [true, 'vertices is required.'],
14
16
  validate: [{
15
17
  validator: helpers.threeOrMoreVertices,
16
- msg: 'A polygon must have 3 or more vertices.'
18
+ msg: CANT_HAVE_THREE_OR_MORE_VERTICES
17
19
  }, {
18
- validator: helpers.notSameThreeInARow,
19
- msg: 'For any given vertex, the next 2 vertices cannot match that vertex.'
20
+ validator: checkDistanceBetweenVertices,
21
+ msg: VERTICES_TOO_CLOSE
20
22
  }]
21
23
  }
22
24
  }
@@ -1,7 +1,7 @@
1
1
  const cloneDeep = require('lodash.clonedeep')
2
2
  const mongoose = require('mongoose')
3
3
  const beaconSchema = require('../Beacon').beaconSchema
4
- const BeaconEnum = require('@bluedot-innovation/enums').BeaconEnum
4
+ const BeaconProximityEnum = require('@bluedot-innovation/enums').BeaconProximityEnum
5
5
 
6
6
  const schema = cloneDeep(beaconSchema)
7
7
 
@@ -12,8 +12,8 @@ delete schema.__v
12
12
 
13
13
  schema.proximity = {
14
14
  type: String,
15
- default: BeaconEnum.FAR,
16
- enum: [BeaconEnum.NEAR, BeaconEnum.IMMEDIATE, BeaconEnum.FAR]
15
+ default: BeaconProximityEnum.FAR,
16
+ enum: [BeaconProximityEnum.NEAR, BeaconProximityEnum.IMMEDIATE, BeaconProximityEnum.FAR]
17
17
  }
18
18
 
19
19
  const ZoneBeaconSchema = new mongoose.Schema(schema)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "3.0.11",
3
+ "version": "3.0.13",
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",
package/utils/index.js ADDED
@@ -0,0 +1,36 @@
1
+ var equal = require('deep-equal');
2
+
3
+ // Used for converting a mongoose object to a javascript object
4
+ const clean = (item) => {
5
+ return item && JSON.parse(JSON.stringify(item))
6
+ }
7
+
8
+ // Reduces decimals to `n`
9
+ // e.g. reduceDecimals(1.2345, 2)
10
+ // returns 1.23
11
+ const trimDecimals = (num, n) => parseFloat(parseFloat(num).toFixed(n))
12
+
13
+ const notSameThreeInARow = function(list) {
14
+ const cleanList = clean(list)
15
+ let hasSame = false;
16
+
17
+ for (var i = 0; i < cleanList.length; i++) {
18
+ const itemA = cleanList[i]
19
+ const itemB = cleanList[i - 1]
20
+ const itemC = cleanList[i - 2]
21
+
22
+ if (equal(itemA, itemB)) {
23
+ hasSame = true;
24
+ }
25
+ if (equal(itemA, itemC)) {
26
+ hasSame = true;
27
+ }
28
+ }
29
+ return !hasSame;
30
+ }
31
+
32
+ module.exports = {
33
+ clean,
34
+ trimDecimals,
35
+ notSameThreeInARow,
36
+ }
@@ -0,0 +1,21 @@
1
+ var equal = require('deep-equal');
2
+ const { notSameThreeInARow, trimDecimals } = require('../utils');
3
+
4
+ /**
5
+ * checkDistanceBetweenVertices -
6
+ * Returns boolean signalling whether or not enough space between points in
7
+ * polygons and polylines. Checks closeness by checking if the same when trimmed
8
+ * to 6 decimal places
9
+ */
10
+ const checkDistanceBetweenVertices = function(vertices) {
11
+ const trimmedVertices = vertices.map(({latitude, longitude}) => {
12
+ return {
13
+ latitude: trimDecimals(latitude, 6),
14
+ longitude: trimDecimals(longitude, 6)
15
+ }
16
+ })
17
+ return notSameThreeInARow(trimmedVertices)
18
+ }
19
+
20
+
21
+ module.exports = checkDistanceBetweenVertices;