@bluedot-innovation/models 2.0.11-alpha.2 → 2.0.12-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,10 +1,10 @@
1
- ## 2.0.11
2
- - Added uuid as `_id` for user
1
+ ## 2.0.12
2
+ - Update bearingFilter to allow wrap around angles (e.g. 315deg & 45deg)
3
3
 
4
4
  ## 2.0.10
5
5
  - Added indexes on beacons model
6
- - Added `apiKey` to project model (for backwards compatability)
7
- - Added `customerApiKey` to account model (for backwards compatability)
6
+ - Added `apiKey` to project model (for backwards compatibility)
7
+ - Added `customerApiKey` to account model (for backwards compatibility)
8
8
 
9
9
  ## 2.0.9
10
10
  - Validation on `projectPermissions` in user model
@@ -1,17 +1,15 @@
1
1
  let mongoose = require('mongoose')
2
-
3
2
  mongoose.Promise = global.Promise
4
3
 
5
- let BearingFilterModel = require('../../../Models').BearingFilter(mongoose)
6
- let testCase = require('mocha').describe
7
- let assertions = require('mocha').it
8
- let chai = require('chai')
9
-
10
- let assert = chai.assert
11
- let mocha = require('mocha')
12
- let beforeEach = mocha.beforeEach
13
-
14
- let rules = require('./bearingFilter.rules.json')
4
+ const BearingFilterModel = require('../../../Models').BearingFilter(mongoose)
5
+ const { isNinetyApart } = require.main.require('models/BearingFilter')
6
+ const describe = require('mocha').describe
7
+ const it = require('mocha').it
8
+ const chai = require('chai')
9
+ const assert = chai.assert
10
+ const mocha = require('mocha')
11
+ const beforeEach = mocha.beforeEach
12
+ const rules = require('./bearingFilter.rules.json')
15
13
 
16
14
  let bearingFilter
17
15
 
@@ -19,9 +17,9 @@ beforeEach(() => {
19
17
  bearingFilter = JSON.parse(JSON.stringify(require('./bearingFilter.data.json')))
20
18
  })
21
19
 
22
- testCase('BearingFilter', () => {
23
- testCase('Valid BearingFilter', () => {
24
- assertions('should validate a valid bearingFilter ', (done) => {
20
+ describe('BearingFilter', () => {
21
+ describe('Valid BearingFilter', () => {
22
+ it('should validate a valid bearingFilter ', (done) => {
25
23
  let BearingFilter = new BearingFilterModel(bearingFilter)
26
24
  BearingFilter.validate((err, res) => {
27
25
  assert.notExists(res)
@@ -31,8 +29,8 @@ testCase('BearingFilter', () => {
31
29
  })
32
30
  })
33
31
 
34
- testCase('Invalid BearingFilter', () => {
35
- assertions('should not validate a bearingFilter with invalid BearingFilter', (done) => {
32
+ describe('Invalid BearingFilter', () => {
33
+ it('should not validate a bearingFilter with invalid BearingFilter', (done) => {
36
34
  delete bearingFilter[rules.requiredFields[0]]
37
35
  let BearingFilter = new BearingFilterModel(bearingFilter)
38
36
  BearingFilter.validate((err, res) => {
@@ -41,7 +39,7 @@ testCase('BearingFilter', () => {
41
39
  done()
42
40
  })
43
41
  })
44
- assertions('should throw an error when toAngle === fromAngle', (done) => {
42
+ it('should throw an error when toAngle === fromAngle', (done) => {
45
43
  bearingFilter.toAngle = 10
46
44
  bearingFilter.fromAngle = 10
47
45
  let BearingFilter = new BearingFilterModel(bearingFilter)
@@ -52,7 +50,7 @@ testCase('BearingFilter', () => {
52
50
  done()
53
51
  })
54
52
  })
55
- assertions('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle < fromAngle)', (done) => {
53
+ it('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle < fromAngle)', (done) => {
56
54
  bearingFilter.toAngle = 5
57
55
  bearingFilter.fromAngle = 10
58
56
  let BearingFilter = new BearingFilterModel(bearingFilter)
@@ -63,7 +61,7 @@ testCase('BearingFilter', () => {
63
61
  done()
64
62
  })
65
63
  })
66
- assertions('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle > fromAngle)', (done) => {
64
+ it('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle > fromAngle)', (done) => {
67
65
  bearingFilter.toAngle = 10
68
66
  bearingFilter.fromAngle = 5
69
67
  let BearingFilter = new BearingFilterModel(bearingFilter)
@@ -74,10 +72,10 @@ testCase('BearingFilter', () => {
74
72
  done()
75
73
  })
76
74
  })
77
- testCase('check each required property of the model and validate that it is present', () => {
75
+ describe('check each required property of the model and validate that it is present', () => {
78
76
  let requiredFields = rules.requiredFields
79
77
  requiredFields.map((i) => {
80
- assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
78
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
81
79
  // delete i from geofeatures' bearingFilter child
82
80
  delete bearingFilter[i]
83
81
  let BearingFilter = new BearingFilterModel(bearingFilter)
@@ -93,3 +91,35 @@ testCase('BearingFilter', () => {
93
91
  })
94
92
  })
95
93
  })
94
+
95
+ describe('isNinetyApart(fromAngle, toAngle)', () => {
96
+ it('should return true if the angles are 90 degrees apart', () => {
97
+ const angles = [
98
+ [90, 180],
99
+ [50, 140],
100
+ [315, 45],
101
+ [300, 30],
102
+ [211, 301]
103
+ ]
104
+
105
+ angles.forEach(([fromAngle, toAngle]) => {
106
+ const result = isNinetyApart(fromAngle, toAngle)
107
+ assert.isTrue(result)
108
+ })
109
+ })
110
+
111
+ it('should return false if the angles are NOT 90 degrees apart', () => {
112
+ const angles = [
113
+ [90, 181],
114
+ [50, 90],
115
+ [315, 360],
116
+ [180, 181],
117
+ [25, 79]
118
+ ]
119
+
120
+ angles.forEach(([fromAngle, toAngle]) => {
121
+ const result = isNinetyApart(fromAngle, toAngle)
122
+ assert.isFalse(result)
123
+ })
124
+ })
125
+ })
@@ -11,6 +11,7 @@ const BearingFilterSchema = new mongoose.Schema(schema, {
11
11
  versionKey: false
12
12
  })
13
13
 
14
+ // For discriminator key
14
15
  BearingFilterSchema.add({
15
16
  type: {type: String, required: true}
16
17
  })
@@ -18,27 +19,37 @@ BearingFilterSchema.add({
18
19
  function validateAngles (toAngle, magicCallback) {
19
20
  let fromAngle = this.fromAngle
20
21
 
21
- if (!toAngle && toAngle !== 0) {
22
+ if (toAngle === undefined) {
22
23
  return magicCallback(false, 'toAngle is required')
23
24
  }
24
25
 
26
+ if (fromAngle === undefined) {
27
+ return magicCallback(false, 'fromAngle is required')
28
+ }
29
+
25
30
  if (toAngle === fromAngle) {
26
31
  return magicCallback(false, 'fromAngle and toAngle cannot be the same')
27
32
  }
28
33
 
29
- if (fromAngle > toAngle) {
30
- if ((fromAngle - toAngle) <= 270) {
31
- return magicCallback(false, 'From Angle and To Angle must best separated by at least 90 degrees in both directions')
32
- }
33
- }
34
- if (toAngle > fromAngle) {
35
- if (((toAngle - fromAngle) < 90) || (((toAngle - fromAngle) > 270))) {
36
- return magicCallback(false, 'From Angle and To Angle must best separated by at least 90 degrees in both directions')
37
- }
34
+ if (!isNinetyApart(fromAngle, toAngle)) {
35
+ return magicCallback(false, 'From Angle and To Angle must be separated by 90 degrees in either direction')
38
36
  }
37
+
39
38
  magicCallback(true)
40
39
  }
41
40
 
41
+ function isNinetyApart (fromAngle, toAngle) {
42
+ const fromAngleWrapped = parseInt(fromAngle) + 360
43
+ const toAngleWrapped = parseInt(toAngle) + 360
44
+ if ((fromAngle - toAngle) === 90 || (toAngle - fromAngle) === 90) {
45
+ return true
46
+ } else if (fromAngle > toAngle) {
47
+ return (toAngleWrapped - fromAngle) === 90
48
+ } else if (fromAngle < toAngle) {
49
+ return (fromAngleWrapped - toAngle) === 90
50
+ }
51
+ }
52
+
42
53
  // optional mongoose
43
54
  let model = (mongooseInstance) => {
44
55
  let BearingFilter
@@ -51,4 +62,9 @@ let model = (mongooseInstance) => {
51
62
  return BearingFilter
52
63
  }
53
64
  // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
54
- module.exports = { Model: model, Schema: BearingFilterSchema, bearingFilterSchema: schema }
65
+ module.exports = {
66
+ Model: model,
67
+ Schema: BearingFilterSchema,
68
+ bearingFilterSchema: schema,
69
+ isNinetyApart
70
+ }
package/models/User.js CHANGED
@@ -10,7 +10,6 @@ const helpers = require('../helpers')
10
10
  const ProjectPermission = require('../schemas/ProjectPermission')
11
11
 
12
12
  let UserSchema = new mongoose.Schema({
13
- _id: {type: String, required: true},
14
13
  username: {
15
14
  type: String,
16
15
  required: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "2.0.11-alpha.2",
3
+ "version": "2.0.12-alpha.2",
4
4
  "description": "",
5
5
  "main": "Models.js",
6
6
  "scripts": {