@bluedot-innovation/models 7.15.0 → 7.16.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 CHANGED
@@ -1,3 +1,9 @@
1
+ ## 7.16.1
2
+ - Remove attentive webhook and add Attentive project integration
3
+
4
+ ## 7.16.0
5
+ - Remove support for beacons and bump json patch dependency for vulnerability
6
+
1
7
  ## 7.15.0
2
8
  - Add nowReadyConfiguration feature toggle to account
3
9
 
package/Models.js CHANGED
@@ -6,7 +6,6 @@ const mongoose = require('mongoose')
6
6
  let Models = {}
7
7
 
8
8
  Models.Account = require('./models/Account').Model
9
- Models.Beacon = require('./models/Beacon').Model
10
9
  Models.Destination = require('./models/Destination').Model
11
10
  Models.MessageAction = require('./models/MessageAction').Model
12
11
  Models.Project = require('./models/Project').Model
@@ -19,7 +18,6 @@ Models.BoundingBox = require('./models/geofeatures/BoundingBox').Model
19
18
  Models.Circle = require('./models/geofeatures/Circle').Model
20
19
  Models.Polygon = require('./models/geofeatures/Polygon').Model
21
20
  Models.Geoline = require('./models/geofeatures/Geoline').Model
22
- Models.ZoneBeacon = require('./models/geofeatures/ZoneBeacon').Model
23
21
 
24
22
  // Conditions
25
23
  Models.DayRange = require('./models/conditions/DayRange').Model
@@ -49,7 +47,6 @@ Models.DailyStats = require('./models/DailyStats').Model
49
47
  let Schemas = {}
50
48
 
51
49
  Schemas.Account = require('./models/Account').Schema
52
- Schemas.Beacon = require('./models/Beacon').Schema
53
50
  Schemas.Destination = require('./models/Destination').Schema
54
51
  Schemas.MessageAction = require('./models/MessageAction').Schema
55
52
  Schemas.Application = require('./models/Application').Schema
@@ -68,7 +65,6 @@ Schemas.BoundingBox = require('./models/geofeatures/BoundingBox').Schema
68
65
  Schemas.Circle = require('./models/geofeatures/Circle').Schema
69
66
  Schemas.Polygon = require('./models/geofeatures/Polygon').Schema
70
67
  Schemas.Geoline = require('./models/geofeatures/Geoline').Schema
71
- Schemas.ZoneBeacon = require('./models/geofeatures/ZoneBeacon').Schema
72
68
 
73
69
  // Conditions
74
70
  Schemas.DayRange = require('./models/conditions/DayRange').Schema
@@ -72,6 +72,10 @@
72
72
  },
73
73
  "flipdish": {
74
74
  "nowReadyEnabled": true
75
+ },
76
+ "attentive": {
77
+ "helloRegister": true,
78
+ "trigger": true
75
79
  }
76
80
  },
77
81
  "tempo": {
@@ -485,27 +485,17 @@ describe('Project', () => {
485
485
  let Project = new ProjectModel(project)
486
486
  Project.validate((err, res) => {
487
487
  assert.exists(err)
488
- assert.match(err.message, /type "register" can only have structure "mobivity" or "attentive"/)
488
+ assert.match(err.message, /type "register" can only have structure "mobivity"/)
489
489
  done()
490
490
  })
491
491
  })
492
- it(`should not validate if the type is register and the structure is not mobivity or attentive`, (done) => {
492
+ it(`should not validate if the type is register and the structure is not mobivity`, (done) => {
493
493
  project.webhooks[0].type = "register"
494
494
  project.webhooks[0].structure = "standard"
495
495
  let Project = new ProjectModel(project)
496
496
  Project.validate((err, res) => {
497
497
  assert.exists(err)
498
- assert.match(err.message, /type "register" can only have structure "mobivity" or "attentive"/)
499
- done()
500
- })
501
- })
502
- it(`should not validate if structure is attentive and the type is not register`, (done) => {
503
- project.webhooks[0].type = "trigger"
504
- project.webhooks[0].structure = "attentive"
505
- let Project = new ProjectModel(project)
506
- Project.validate((err, res) => {
507
- assert.exists(err)
508
- assert.match(err.message, /type "register" can only have structure "mobivity" or "attentive"/)
498
+ assert.match(err.message, /type "register" can only have structure "mobivity"/)
509
499
  done()
510
500
  })
511
501
  })
@@ -609,6 +599,27 @@ describe('Project', () => {
609
599
  })
610
600
  })
611
601
  })
602
+ describe("Attentive integration validation", () => {
603
+ it('should throw an error when trigger is non boolean', (done) => {
604
+ project.integrations.attentive.trigger = "invalid"
605
+ let Project = new ProjectModel(project)
606
+ Project.validate((err, res) => {
607
+ assert.exists(err)
608
+ assert.match(err.message, /Cast to Boolean failed for value "invalid" .* at path "trigger"/)
609
+ done()
610
+ })
611
+ })
612
+
613
+ it('should throw an error when helloRegister is non boolean', (done) => {
614
+ project.integrations.attentive.helloRegister = "invalid"
615
+ let Project = new ProjectModel(project)
616
+ Project.validate((err, res) => {
617
+ assert.exists(err)
618
+ assert.match(err.message, /Cast to Boolean failed for value "invalid" .* at path "helloRegister"/)
619
+ done()
620
+ })
621
+ })
622
+ })
612
623
  })
613
624
 
614
625
  describe('should throw an error when any of the values of the array contains a falsy value', () => {
@@ -21,7 +21,6 @@
21
21
  "url": "https://www.google.com"
22
22
  }
23
23
  ],
24
- "beacons": [],
25
24
  "conditions": [
26
25
  {
27
26
  "type" : "dayRange",
@@ -30,21 +29,6 @@
30
29
  }
31
30
  ],
32
31
  "geofeatures": [{
33
- "type": "beacon",
34
- "name": "create beacon",
35
- "proximityUUID": "23a01af0-232a-4518-9c0e-323fb773f5ef",
36
- "location": {
37
- "latitude": -37.8159544565362,
38
- "longitude": 144.9723565578461
39
- },
40
- "beaconType": "Both",
41
- "major": 10000,
42
- "phone": 123123123123,
43
- "minor": 56466,
44
- "txPower": -77,
45
- "macAddress": "01:17:c5:31:84:19",
46
- "accountId": "90a2ed18-86be-48e7-948b-393b8b679ef6"
47
- },{
48
32
  "type": "circle",
49
33
  "name": "Test Circle",
50
34
  "accuracy": "high",
@@ -18,7 +18,7 @@ beforeEach(() => {
18
18
  delete zone.conditions
19
19
 
20
20
  // geofeatures are tested individually so we only need to try one in Zones' test
21
- zone.geofeatures = zone.geofeatures.filter(x => x.type.match(/beacon/i))
21
+ zone.geofeatures = zone.geofeatures.filter(x => x.type.match(/circle/i))
22
22
 
23
23
  // remove customData
24
24
  delete zone.customData
@@ -50,12 +50,13 @@ testCase('Zone - Action', () => {
50
50
  })
51
51
  })
52
52
  assertions('should throw an error when an invalid geofeature (complex validation) is instantiated within a zone', (done) => {
53
- delete zone.geofeatures[0].minor
53
+ delete zone.geofeatures[0].radius
54
54
  let Zone = new ZoneModel(zone)
55
55
  Zone.validate((err) => {
56
56
  assert.exists(err, 'err object should exist')
57
57
  assert.isObject(err.errors)
58
- assert('geofeatures.0.minor' in err.errors)
58
+ console.log(err.errors)
59
+ assert('geofeatures.0.radius' in err.errors)
59
60
  done()
60
61
  })
61
62
  })
@@ -75,7 +76,7 @@ testCase('Zone - Action', () => {
75
76
  Zone.validate((err) => {
76
77
  assert.exists(err, 'err object should exist')
77
78
  assert.isObject(err.errors)
78
- assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
79
+ assert.match(err.message, /(Geofeature requires a type of `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
79
80
  done()
80
81
  })
81
82
  })
@@ -86,7 +87,7 @@ testCase('Zone - Action', () => {
86
87
  Zone.validate((err) => {
87
88
  assert.exists(err, 'err object should exist')
88
89
  assert.isObject(err.errors)
89
- assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
90
+ assert.match(err.message, /(Geofeature requires a type of `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
90
91
  done()
91
92
  })
92
93
  })
@@ -97,7 +98,7 @@ testCase('Zone - Action', () => {
97
98
  Zone.validate((err) => {
98
99
  assert.exists(err, 'err object should exist')
99
100
  assert.isObject(err.errors)
100
- assert.match(err.message, /(Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
101
+ assert.match(err.message, /(Geofeature requires a type of `circle`, `polygon`, `geoline` or `boundingBox`.)/i)
101
102
  done()
102
103
  })
103
104
  })
package/helpers.js CHANGED
@@ -94,34 +94,6 @@ helpers.checkIsDifferent = function(propertyName) {
94
94
  }
95
95
  }
96
96
 
97
- helpers.checkConditionsSuitableForBeacon = function(zone, next) {
98
- let hasBeacons = false;
99
- if( zone.geofeatures && zone.geofeatures.length ) {
100
- for( let geofeature of zone.geofeatures ) {
101
- if(!geofeature) return next(new Error("Geofeature must have a type"))
102
-
103
- if( geofeature.type === "beacon" ) {
104
- hasBeacons = true;
105
- break;
106
- }
107
- }
108
- }
109
- if( !hasBeacons )
110
- return next(); // No beacons.
111
- if( !zone.conditions || !zone.conditions.length )
112
- return next(); // No conditions.
113
- for( let condition of zone.conditions ) {
114
- if( !condition ) return next(new Error("Condition must have a valid value"))
115
- if( condition.type === "travelDirection" ) {
116
- return next(new Error("Zone cannot both contain a beacon and have a Travel Direction condition."))
117
- }
118
- if( condition.type === "speed" ) {
119
- return next(new Error("Zone cannot both contain a beacon and have a Speed condition."))
120
- }
121
- }
122
- next();
123
- }
124
-
125
97
  helpers.checkMinimumGeofeaturesValidForZone = function(zone, next) {
126
98
  if( !zone.geofeatures )
127
99
  return next();
@@ -162,7 +134,7 @@ helpers.checkArrayContainsValidValues = function(document, properties, next) {
162
134
  }
163
135
  }
164
136
  })
165
-
137
+
166
138
  return next()
167
139
  }
168
140
 
package/models/Zone.js CHANGED
@@ -13,7 +13,6 @@ let circleSchema = require('./geofeatures/Circle').circleSchema
13
13
  let boundingBoxSchema = require('./geofeatures/BoundingBox').boundingBoxSchema
14
14
  let geolineSchema = require('./geofeatures/Geoline').geolineSchema
15
15
  let polygonSchema = require('./geofeatures/Polygon').polygonSchema
16
- let beaconSchema = require('./geofeatures/ZoneBeacon').beaconSchema
17
16
 
18
17
  // Conditions
19
18
  let speedRawSchema = require('./conditions/Speed').rawSchema
@@ -68,9 +67,9 @@ let geofeatureSchema = new Schema({},
68
67
 
69
68
  geofeatureSchema.pre('validate', function(next){
70
69
 
71
- if([`beacon`,`circle`,`polygon`,`geoline`,`boundingBox`].indexOf(this.type) < 0){
70
+ if([`circle`,`polygon`,`geoline`,`boundingBox`].indexOf(this.type) < 0){
72
71
 
73
- return next(new Error('Geofeature requires a type of `beacon`, `circle`, `polygon`, `geoline` or `boundingBox`.'))
72
+ return next(new Error('Geofeature requires a type of `circle`, `polygon`, `geoline` or `boundingBox`.'))
74
73
  }
75
74
 
76
75
  next()
@@ -135,7 +134,6 @@ zoneSchema.path('conditions').discriminator('timeActive', new Schema(timeActiveR
135
134
  zoneSchema.path('conditions').discriminator('travelDirection', new Schema(travelDirectionRawSchema, {_id: false}))
136
135
  zoneSchema.path('conditions').discriminator('travelPath', new Schema(travelPathRawSchema, {_id: false}))
137
136
 
138
- zoneSchema.path('geofeatures').discriminator('beacon', new Schema(beaconSchema))
139
137
  zoneSchema.path('geofeatures').discriminator('circle', new Schema(circleSchema))
140
138
  zoneSchema.path('geofeatures').discriminator('polygon', new Schema(polygonSchema))
141
139
  zoneSchema.path('geofeatures').discriminator('geoline', new Schema(geolineSchema))
@@ -144,7 +142,6 @@ zoneSchema.path('geofeatures').discriminator('boundingBox', new Schema(boundingB
144
142
  zoneSchema.pre('validate', function(next) {
145
143
  Promise.all([
146
144
  new Promise((resolve, reject) => helpers.checkArrayContainsValidValues(this, ['geofeatures', 'actions', 'conditions', 'tags'], resolve)),
147
- new Promise((resolve, reject) => helpers.checkConditionsSuitableForBeacon(this, resolve)),
148
145
  new Promise((resolve, reject) => helpers.checkMinimumGeofeaturesValidForZone(this, resolve)),
149
146
  new Promise((resolve, reject) => helpers.validateTravelPathConditionCount(this, resolve)),
150
147
  new Promise((resolve, reject) => helpers.checkValidLowAccuracyFence(this, resolve)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "7.15.0",
3
+ "version": "7.16.1",
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",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@bluedot-innovation/enums": "^2.1.5",
33
- "@bluedot-innovation/mongoose-json-patch": "0.0.8",
33
+ "@bluedot-innovation/mongoose-json-patch": "^0.0.9",
34
34
  "@mongoosejs/double": "^0.2.0",
35
35
  "@turf/area": "^6.0.1",
36
36
  "@turf/helpers": "^6.1.4",
@@ -4,6 +4,7 @@ const segmentIntegrationSchema = require('./SegmentIntegration')
4
4
  const oneSignalIntegrationSchema = require('./OneSignalIntegration')
5
5
  const bbotIntegrationSchema = require('./BbotIntegration')
6
6
  const flipdishIntegrationSchema = require('./FlipdishIntegration')
7
+ const projectAttentiveIntegrationSchema = require('./ProjectAttentiveIntegration')
7
8
 
8
9
 
9
10
  const MParticleSchema = new mongoose.Schema({
@@ -56,7 +57,8 @@ const IntegrationSchema = new mongoose.Schema({
56
57
  segment: { type: segmentIntegrationSchema },
57
58
  oneSignal: { type: oneSignalIntegrationSchema },
58
59
  bbot: { type: bbotIntegrationSchema },
59
- flipdish: { type: flipdishIntegrationSchema }
60
+ flipdish: { type: flipdishIntegrationSchema },
61
+ attentive: { type: projectAttentiveIntegrationSchema }
60
62
  },{ _id : false });
61
63
  function apiKeyValidator(value){
62
64
  // check if the value is a non-empty string
@@ -0,0 +1,10 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ let ProjectAttentiveIntegrationSchema = new mongoose.Schema({
4
+ helloRegister: {type: Boolean, required: true},
5
+ trigger: {type: Boolean, required: true}
6
+ }, {
7
+ _id: false
8
+ })
9
+
10
+ module.exports = ProjectAttentiveIntegrationSchema
@@ -28,9 +28,9 @@ function validateMobivityRequiredFields(structure){
28
28
  }
29
29
 
30
30
  function validateTypeAndStructure(type) {
31
- if ((type === "register" && !['mobivity', 'attentive'].includes(this.structure))
32
- || (type !== "register" && ['mobivity', 'attentive'].includes(this.structure))) {
33
- throw new Error('type "register" can only have structure "mobivity" or "attentive"')
31
+ if ((type === "register" && !['mobivity'].includes(this.structure))
32
+ || (type !== "register" && ['mobivity'].includes(this.structure))) {
33
+ throw new Error('type "register" can only have structure "mobivity"')
34
34
  } else if ((type === "helloOrder" && this.structure !== 'default')
35
35
  || (type !== "helloOrder" && this.structure === 'default')) {
36
36
  throw new Error('type "helloOrder" can only have structure "default"')
@@ -44,7 +44,7 @@ let WebhookSchema = new mongoose.Schema({
44
44
  body: {type: mongoose.Schema.Types.Mixed, validate: [{ validator: validators.validateNonEmptyStringKeyValuePairsOnly, msg: 'webhook body keys and values must be strings' }, { validator: validators.validateNoSpacesInObjectKeys, msg: 'webhook body keys cannot contain spaces' }]},
45
45
  structure: {
46
46
  type: String,
47
- enum: ['standard', 'tealium', 'mobivity', 'attentive', 'default'],
47
+ enum: ['standard', 'tealium', 'mobivity', 'default'],
48
48
  default: 'standard',
49
49
  validate: [
50
50
  {validator: validateTealiumRequiredFields, msg: 'tealium webhooks require "tealium_account" and "tealium_profile" body fields to be present'},
@@ -1,15 +0,0 @@
1
- {
2
- "type": "beacon",
3
- "name": "create beacon",
4
- "proximityUUID": "23a01af0-232a-4518-9c0e-323fb773f5ef",
5
- "location": {
6
- "latitude": -37.8159544565362,
7
- "longitude": 144.9723565578461
8
- },
9
- "beaconType": "Both",
10
- "major": 10000,
11
- "minor": 56466,
12
- "txPower": -77,
13
- "macAddress": "01:17:c5:31:84:19",
14
- "accountId": "90a2ed18-86be-48e7-948b-393b8b679ef6"
15
- }
@@ -1,13 +0,0 @@
1
- {
2
- "requiredFields":["name", "location", "beaconType"],
3
- "dependentRequiredFields": [
4
- {
5
- "property": "beaconType",
6
- "pairings": [
7
- {"value": "IOS", "requiredFields": ["proximityUUID", "major", "minor"]},
8
- {"value": "Android", "requiredFields": ["macAddress"]},
9
- {"value": "Both", "requiredFields": ["proximityUUID", "major", "minor", "macAddress"]}
10
- ]
11
- }
12
- ]
13
- }
@@ -1,173 +0,0 @@
1
- let mongoose = require('mongoose')
2
-
3
- mongoose.Promise = global.Promise
4
-
5
- let BeaconModel = require('../../Models').Beacon(mongoose)
6
- let describe = require('mocha').describe
7
- let it = 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('./beacon.rules.json')
15
-
16
- let beacon
17
-
18
- beforeEach(() => {
19
- beacon = JSON.parse(JSON.stringify(require('./beacon.data.json')))
20
- })
21
-
22
- describe('Beacon', () => {
23
- describe('Valid Beacon', () => {
24
- it('should validate a valid beacon', (done) => {
25
- const Beacon = new BeaconModel(beacon)
26
-
27
- Beacon.validate((err, res) => {
28
- assert.notExists(res)
29
- assert.notExists(err)
30
- done()
31
- })
32
- })
33
- })
34
-
35
- describe('Invalid Beacon', () => {
36
- it('should not validate a beacon with invalid Beacon', (done) => {
37
- delete beacon[rules.requiredFields[0]]
38
- let Beacon = new BeaconModel(beacon)
39
- Beacon.validate((err, res) => {
40
- assert.exists(err, 'err object should exist')
41
- assert.isObject(err.errors)
42
- done()
43
- })
44
-
45
- })
46
-
47
- describe('check each dependent required property of the model and validate that it is present and in a valid format', () => {
48
- let dependentRequiredFields = rules.dependentRequiredFields
49
- dependentRequiredFields.forEach((parent) => {
50
- parent.pairings.forEach((i) => {
51
- i.requiredFields.forEach((field) => {
52
- it(`should throw an error with property '${field}' if '${i.value}' is selected and '${field}' is not present`, (done) => {
53
- // delete beacon[field] i.e. txPower from beacon
54
- delete beacon[field]
55
- // set required dependent field property
56
- beacon[parent.property] = i.value
57
- let Beacon = new BeaconModel(beacon)
58
- Beacon.validate((err, res) => {
59
- assert.notExists(res)
60
- assert.exists(err, 'err object should exist')
61
- assert.isObject(err.errors)
62
- assert.nestedProperty(err, `errors.${field}`, `error should have a ${field} property`)
63
- done()
64
- })
65
- })
66
- })
67
-
68
- i.requiredFields.forEach((field) => {
69
- it(`should throw an error with property '${field}' if '${i.value}' is selected and '${field}' is not in a valid format`, (done) => {
70
- beacon[field] = "dumpt text"
71
- // set required dependent field property
72
- beacon[parent.property] = i.value
73
- let Beacon = new BeaconModel(beacon)
74
- Beacon.validate((err, res) => {
75
- assert.notExists(res)
76
- assert.exists(err, 'err object should exist')
77
- assert.isObject(err.errors)
78
- assert.nestedProperty(err, `errors.${field}`, `error should have a ${field} property`)
79
- done()
80
- })
81
- })
82
- })
83
- })
84
- })
85
- })
86
-
87
- describe('check each required property of the model and validate that it is present', () => {
88
- let requiredFields = rules.requiredFields
89
- requiredFields.forEach((requiredField) => {
90
- it(`should throw an error with property '${requiredField}' if '${requiredField}' is not present`, (done) => {
91
- // delete i from geofeatures' beacon child
92
- delete beacon[requiredField]
93
- let Beacon = new BeaconModel(beacon)
94
- Beacon.validate((err, res) => {
95
- assert.notExists(res)
96
- assert.exists(err, 'err object should exist')
97
- assert.isObject(err.errors)
98
- assert.nestedProperty(err, `errors.${requiredField}`, `error should have a ${requiredField} property`)
99
- done()
100
- })
101
- })
102
- })
103
- })
104
-
105
- describe('should fail validation if the beaconType does not match the enum', () => {
106
- const failingBeaconTypes = ['ios', 'iOS', 'android', 'ANDROID', 'both', 'BOTH'];
107
-
108
- failingBeaconTypes.forEach(beaconType => {
109
- it(beaconType, (done) => {
110
- let Beacon = new BeaconModel(beacon);
111
- Beacon.beaconType = beaconType;
112
-
113
- Beacon.validate((err, res) => {
114
- assert.exists(err)
115
- assert.match(err, /(is not a valid enum value)/i)
116
- done()
117
- })
118
- })
119
- })
120
- })
121
-
122
- describe('should fail validation if the txPower is not a number in the range from -100 to 0', () => {
123
- const beaconTypes = ['Android', 'Both'];
124
- const invalidTxPowerValues = ['dump_text', 10, -77.77]
125
-
126
- beaconTypes.forEach(beaconType => {
127
- invalidTxPowerValues.forEach(value => {
128
- it(`should throw an error with txPower with value: ${value} and beaconType: ${beaconType}`, (done) => {
129
- let Beacon = new BeaconModel(beacon);
130
- Beacon.beaconType = beaconType;
131
- Beacon.txPower = value
132
- Beacon.validate((err, res) => {
133
- assert.notExists(res)
134
- assert.exists(err, 'err object should exist')
135
- assert.isObject(err.errors)
136
- assert.nestedProperty(err, 'errors.txPower' ,`error should have a txPower property`)
137
- done()
138
- })
139
- })
140
- })
141
- })
142
- })
143
-
144
- describe('should fail validation when passed an invalid Mac Address', () => {
145
- const beaconTypes = ['Android', 'Both'];
146
- const invalidMacAddresses = [
147
- 'invalid mac address',
148
- 1324,
149
- false,
150
- undefined,
151
- 'F:9:0:4:F:2',
152
- 'F:90:0B:4E:F0:22',
153
- ]
154
-
155
- beaconTypes.forEach(beaconType => {
156
- invalidMacAddresses.forEach(value => {
157
- it(`should throw an error with a Mac Address with value: ${value} and beaconType: ${beaconType}`, (done) => {
158
- let Beacon = new BeaconModel(beacon);
159
- Beacon.beaconType = beaconType;
160
- Beacon.macAddress = value
161
- Beacon.validate((err, res) => {
162
- assert.notExists(res)
163
- assert.exists(err, 'err object should exist')
164
- assert.isObject(err.errors)
165
- assert.nestedProperty(err, 'errors.macAddress' ,`error should have a macAddress property`)
166
- done()
167
- })
168
- })
169
- })
170
- })
171
- })
172
- })
173
- })
@@ -1,16 +0,0 @@
1
- {
2
- "type": "beacon",
3
- "name": "create beacon",
4
- "proximityUUID": "23a01af0-232a-4518-9c0e-323fb773f5ef",
5
- "location": {
6
- "latitude": -37.8159544565362,
7
- "longitude": 144.9723565578461
8
- },
9
- "beaconType": "Both",
10
- "major": 10000,
11
- "minor": 56466,
12
- "txPower": -77,
13
- "proximity": "Near",
14
- "macAddress": "01:17:c5:31:84:19",
15
- "accountId": "90a2ed18-86be-48e7-948b-393b8b679ef6"
16
- }
@@ -1,13 +0,0 @@
1
- {
2
- "requiredFields":["name", "location", "beaconType"],
3
- "dependentRequiredFields": [
4
- {
5
- "property": "beaconType",
6
- "pairings": [
7
- {"value": "ios", "requiredFields": ["major", "minor"]},
8
- {"value": "android", "requiredFields": ["txPower", "macAddress"]},
9
- {"value": "both", "requiredFields": ["major", "minor", "txPower", "macAddress"]}
10
- ]
11
- }
12
- ]
13
- }
@@ -1,79 +0,0 @@
1
- let mongoose = require('mongoose')
2
-
3
- mongoose.Promise = global.Promise
4
-
5
- let ZoneBeaconModel = require('../../../Models').ZoneBeacon(mongoose)
6
- let ZoneBeaconModelBuiltInMongoose = require('../../../Models').ZoneBeacon()
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
-
15
- let rules = require('./zoneBeacon.rules.json')
16
-
17
- let zoneBeacon
18
-
19
- beforeEach(() => {
20
- zoneBeacon = JSON.parse(JSON.stringify(require('./zoneBeacon.data.json')))
21
- })
22
-
23
- describe('ZoneBeacon', () => {
24
- describe('Valid ZoneBeacon', () => {
25
- it('should validate a valid zoneBeacon ', (done) => {
26
- const ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
27
- ZoneBeacon.validate((err, res) => {
28
- assert.notExists(res)
29
- assert(err === null, 'err should equals null')
30
- done()
31
- })
32
- })
33
- it('should validate a valid zoneBeacon ', (done) => {
34
- const ZoneBeacon = new ZoneBeaconModelBuiltInMongoose(zoneBeacon)
35
- ZoneBeacon.validate((err, res) => {
36
- assert.notExists(res)
37
- assert(err === null, 'err should equals null')
38
- done()
39
- })
40
- })
41
-
42
- it('should return Double type for longitude and latitude', (done) => {
43
- zoneBeacon.location = {"latitude": -37, "longitude": 144}
44
- const ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
45
- assert.isTrue(ZoneBeacon.location.latitude instanceof mongoose.Types.Double)
46
- assert.isTrue(ZoneBeacon.location.longitude instanceof mongoose.Types.Double)
47
- done()
48
- })
49
- })
50
-
51
- describe('Invalid ZoneBeacon', () => {
52
- it('should not validate a zoneBeacon with invalid ZoneBeacon', (done) => {
53
- delete zoneBeacon[rules.requiredFields[0]]
54
- let ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
55
- ZoneBeacon.validate((err, res) => {
56
- assert.exists(err, 'err object should exist')
57
- assert.isObject(err.errors)
58
- done()
59
- })
60
- })
61
- describe('check each required property of the model and validate that it is present', () => {
62
- let requiredFields = rules.requiredFields
63
- requiredFields.map((i) => {
64
- it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
65
- // delete i from geofeatures' zoneBeacon child
66
- delete zoneBeacon[i]
67
- let ZoneBeacon = new ZoneBeaconModel(zoneBeacon)
68
- ZoneBeacon.validate((err, res) => {
69
- assert.notExists(res)
70
- assert.exists(err, 'err object should exist')
71
- assert.isObject(err.errors)
72
- assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
73
- done()
74
- })
75
- })
76
- })
77
- })
78
- })
79
- })
package/models/Beacon.js DELETED
@@ -1,104 +0,0 @@
1
- const mongoose = require('mongoose')
2
- const BeaconEnum = require('@bluedot-innovation/enums').BeaconEnum
3
- const PointGeometry = require('../schemas/PointGeometry')
4
- const patcher = require('@bluedot-innovation/mongoose-json-patch')
5
- const Point = require('../schemas/Point')
6
- const uuidv4 = require('uuid/v4')
7
- const helpers = require('../helpers')
8
- const StateEnum = require('@bluedot-innovation/enums').StateEnum
9
-
10
- function validateMacAddress(beaconType, macAddress) {
11
- if(isRequiredForAndroid(beaconType)){
12
- return helpers.isValidMacAddress(macAddress)
13
- }
14
- return true
15
- }
16
-
17
-
18
- function isRequiredForAndroid(beaconType){
19
- return beaconType.toLowerCase() === BeaconEnum.ANDROID.toLowerCase() || beaconType.toLowerCase() === BeaconEnum.BOTH.toLowerCase()
20
- }
21
-
22
- function validateProximityUUID(beaconType, proximityUUID) {
23
- if(isRequiredForIOS(beaconType)){
24
- return helpers.validateUUIDv4(proximityUUID)
25
- }
26
- return true
27
- }
28
-
29
- function isRequiredForIOS(beaconType){
30
- return beaconType.toLowerCase() === BeaconEnum.IOS.toLowerCase() || beaconType.toLowerCase() === BeaconEnum.BOTH.toLowerCase()
31
- }
32
-
33
- function validateTxPower(beaconType, txPower) {
34
- if(isRequiredForAndroid(beaconType)){
35
- if(txPower % 1 != 0) {
36
- return false;
37
- }
38
-
39
- return txPower >= -100 && txPower <= 0
40
- }
41
- return true
42
- }
43
-
44
-
45
- let schema = {
46
- _id: {type: String, default: uuidv4},
47
- name: {type: String, required: [true, 'name is required.']},
48
- description: {type: String, default: '', require: true},
49
- creationTime: {type: Date, default: Date.now},
50
- lastUpdateTime: {type: Date, default: Date.now},
51
- state: {type: String, default: StateEnum.ACTIVE},
52
- beaconType: {
53
- type: String,
54
- required: true,
55
- enum: [BeaconEnum.IOS, BeaconEnum.ANDROID, BeaconEnum.BOTH]
56
- },
57
- location: {type: Point, required: true},
58
- geometry: {type: PointGeometry},
59
- accountId: {type: String, required: true},
60
- // These must be unique
61
- major: {type: Number, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a major'], validate: [function() {return validateMajorMinor(this.beaconType, this.major)}, 'Major needs to be within range(0-65535)']},
62
- minor: {type: Number, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a minor'], validate: [function() {return validateMajorMinor(this.beaconType, this.minor)}, 'Minor needs to be within range(0-65535)']},
63
- txPower: {type: Number, validate: [function() {return validateTxPower(this.beaconType, this.txPower)}, 'TxPower needs to be within range(-100 to 0) without decimals']},
64
- macAddress: {type: String, required: [function() {return isRequiredForAndroid(this.beaconType)}, 'Please enter a Mac Address'], validate: [function () {return validateMacAddress(this.beaconType, this.macAddress)}, 'Please enter a valid Mac Address']},
65
- proximityUUID: {type: String, required: [function() {return isRequiredForIOS(this.beaconType)}, 'Please enter a Proximity UUID'], validate: [function () {return validateProximityUUID(this.beaconType, this.proximityUUID)}, 'Please enter a valid Proximity UUID']}
66
- }
67
-
68
- let BeaconSchema = new mongoose.Schema(schema, {collection: 'beacon', autoIndex: false})
69
-
70
- BeaconSchema.index({ macAddress: 1 })
71
- BeaconSchema.index({ proximityUUID: 1, major: 1, minor: 1 })
72
- BeaconSchema.index({ accountId: 1, geometry: '2dsphere' })
73
-
74
- BeaconSchema.plugin(patcher)
75
-
76
- // mongoose validator helper plugin
77
- BeaconSchema.post('validate', function (doc) {
78
- // Beacon Schema post validate hook
79
- this.lastUpdateTime = Date.now()
80
- Object.assign(this.errors || {}, this.errors || {}, this.customErrors)
81
- })
82
-
83
- function validateMajorMinor (beaconType, value) {
84
- 'use strict'
85
-
86
- if(isRequiredForIOS(beaconType)){
87
- return value >= 0 && value <= 65535
88
- }
89
- return true
90
- }
91
-
92
- // optional mongoose
93
- let model = (mongooseInstance) => {
94
- const instance = mongooseInstance || mongoose
95
- let Beacon = instance.model('Beacon', BeaconSchema)
96
-
97
- Beacon.on('index', function (error) {
98
- console.log(error.message)
99
- })
100
-
101
- return Beacon
102
- }
103
- // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
104
- module.exports = { Model: model, Schema: BeaconSchema, beaconSchema: schema }
@@ -1,28 +0,0 @@
1
- const cloneDeep = require('lodash.clonedeep')
2
- const mongoose = require('mongoose')
3
- const beaconSchema = require('../Beacon').beaconSchema
4
- const BeaconProximityEnum = require('@bluedot-innovation/enums').BeaconProximityEnum
5
-
6
- const schema = cloneDeep(beaconSchema)
7
-
8
- delete schema.state
9
- delete schema.creationTime
10
- delete schema.lastUpdateTime
11
- delete schema.__v
12
-
13
- schema.proximity = {
14
- type: String,
15
- default: BeaconProximityEnum.FAR,
16
- enum: [BeaconProximityEnum.NEAR, BeaconProximityEnum.IMMEDIATE, BeaconProximityEnum.FAR]
17
- }
18
-
19
- const ZoneBeaconSchema = new mongoose.Schema(schema)
20
-
21
- // optional mongoose
22
- let model = (mongooseInstance) => {
23
- const instance = mongooseInstance || mongoose
24
- return instance.model('ZoneBeacon', ZoneBeaconSchema)
25
- }
26
-
27
- // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
28
- module.exports = { Model: model, Schema: ZoneBeaconSchema, beaconSchema: schema }