@bluedot-innovation/models 2.0.11-alpha.1 → 2.0.12-alpha.1
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 +3 -3
- package/__tests__/filter/bearing/bearingFilter.spec.js +51 -21
- package/models/BearingFilter.js +23 -11
- package/models/User.js +0 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
## 2.0.11
|
|
2
|
-
-
|
|
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
|
|
7
|
-
- Added `customerApiKey` to account model (for backwards
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
})
|
package/models/BearingFilter.js
CHANGED
|
@@ -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,7 +19,7 @@ BearingFilterSchema.add({
|
|
|
18
19
|
function validateAngles (toAngle, magicCallback) {
|
|
19
20
|
let fromAngle = this.fromAngle
|
|
20
21
|
|
|
21
|
-
if (!toAngle
|
|
22
|
+
if (!toAngle === undefined) {
|
|
22
23
|
return magicCallback(false, 'toAngle is required')
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -26,19 +27,25 @@ function validateAngles (toAngle, magicCallback) {
|
|
|
26
27
|
return magicCallback(false, 'fromAngle and toAngle cannot be the same')
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
if (fromAngle
|
|
30
|
-
|
|
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
|
-
}
|
|
30
|
+
if (!isNinetyApart(fromAngle, toAngle)) {
|
|
31
|
+
return magicCallback(false, 'From Angle and To Angle must be separated by 90 degrees in either direction')
|
|
38
32
|
}
|
|
33
|
+
|
|
39
34
|
magicCallback(true)
|
|
40
35
|
}
|
|
41
36
|
|
|
37
|
+
function isNinetyApart (fromAngle, toAngle) {
|
|
38
|
+
const fromAngleWrapped = parseInt(fromAngle) + 360
|
|
39
|
+
const toAngleWrapped = parseInt(toAngle) + 360
|
|
40
|
+
if ((fromAngle - toAngle) === 90 || (toAngle - fromAngle) === 90) {
|
|
41
|
+
return true
|
|
42
|
+
} else if (fromAngle > toAngle) {
|
|
43
|
+
return (toAngleWrapped - fromAngle) === 90
|
|
44
|
+
} else if (fromAngle < toAngle) {
|
|
45
|
+
return (fromAngleWrapped - toAngle) === 90
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
// optional mongoose
|
|
43
50
|
let model = (mongooseInstance) => {
|
|
44
51
|
let BearingFilter
|
|
@@ -51,4 +58,9 @@ let model = (mongooseInstance) => {
|
|
|
51
58
|
return BearingFilter
|
|
52
59
|
}
|
|
53
60
|
// Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
|
|
54
|
-
module.exports = {
|
|
61
|
+
module.exports = {
|
|
62
|
+
Model: model,
|
|
63
|
+
Schema: BearingFilterSchema,
|
|
64
|
+
bearingFilterSchema: schema,
|
|
65
|
+
isNinetyApart
|
|
66
|
+
}
|
package/models/User.js
CHANGED
|
@@ -8,10 +8,8 @@ const mongoose = require('mongoose')
|
|
|
8
8
|
const patcher = require('mongoose-json-patch')
|
|
9
9
|
const helpers = require('../helpers')
|
|
10
10
|
const ProjectPermission = require('../schemas/ProjectPermission')
|
|
11
|
-
const uuidv4 = require('uuid/v4')
|
|
12
11
|
|
|
13
12
|
let UserSchema = new mongoose.Schema({
|
|
14
|
-
_id: {type: String, default: uuidv4},
|
|
15
13
|
username: {
|
|
16
14
|
type: String,
|
|
17
15
|
required: true,
|