@bluedot-innovation/models 4.0.0 → 5.0.0

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.
Files changed (160) hide show
  1. package/.eslintrc.js +42 -0
  2. package/.gitlab-ci.yml +43 -0
  3. package/CHANGELOG.md +156 -0
  4. package/Models.js +55 -10
  5. package/README.MD +36 -0
  6. package/__tests__/account/account.data.json +12 -0
  7. package/__tests__/account/account.rules.js +8 -0
  8. package/__tests__/account/account.spec.js +83 -0
  9. package/__tests__/action/message/messageAction.data.json +5 -0
  10. package/__tests__/action/message/messageAction.rules.json +3 -0
  11. package/__tests__/action/message/messageAction.spec.js +61 -0
  12. package/__tests__/action/url/urlAction.data.json +4 -0
  13. package/__tests__/action/url/urlAction.rules.json +3 -0
  14. package/__tests__/action/url/urlAction.spec.js +74 -0
  15. package/__tests__/beacon/beacon.data.json +15 -0
  16. package/__tests__/beacon/beacon.rules.json +13 -0
  17. package/__tests__/beacon/beacon.spec.js +104 -0
  18. package/__tests__/condition/beacon/beaconFilter.data.json +1 -0
  19. package/__tests__/condition/beacon/beaconFilter.rules.json +3 -0
  20. package/__tests__/condition/beacon/beaconFilter.spec.js +1 -0
  21. package/__tests__/condition/dayRange/dayRange.data.json +5 -0
  22. package/__tests__/condition/dayRange/dayRange.rules.json +3 -0
  23. package/__tests__/condition/dayRange/dayRange.spec.js +74 -0
  24. package/__tests__/condition/percentageCrossed/travelPath.data.json +5 -0
  25. package/__tests__/condition/percentageCrossed/travelPath.rules.json +3 -0
  26. package/__tests__/condition/percentageCrossed/travelPath.spec.js +62 -0
  27. package/__tests__/condition/speed/speed.data.json +5 -0
  28. package/__tests__/condition/speed/speed.rules.json +3 -0
  29. package/__tests__/condition/speed/speed.spec.js +73 -0
  30. package/__tests__/condition/timeActive/timeActive.data.json +5 -0
  31. package/__tests__/condition/timeActive/timeActive.rules.json +7 -0
  32. package/__tests__/condition/timeActive/timeActive.spec.js +94 -0
  33. package/__tests__/condition/travelDirection/travelDirection.data.json +5 -0
  34. package/__tests__/condition/travelDirection/travelDirection.rules.json +3 -0
  35. package/__tests__/condition/travelDirection/travelDirection.spec.js +132 -0
  36. package/__tests__/dailyStats/checkDailyStats.js +31 -0
  37. package/__tests__/geofeature/boundingBox/boundingBox.data.json +12 -0
  38. package/__tests__/geofeature/boundingBox/boundingBox.rules.json +3 -0
  39. package/__tests__/geofeature/boundingBox/boundingBox.spec.js +78 -0
  40. package/__tests__/geofeature/circle/circle.data.json +9 -0
  41. package/__tests__/geofeature/circle/circle.rules.json +3 -0
  42. package/__tests__/geofeature/circle/circle.spec.js +71 -0
  43. package/__tests__/geofeature/geoline/geoline.data.json +46 -0
  44. package/__tests__/geofeature/geoline/geoline.rules.json +3 -0
  45. package/__tests__/geofeature/geoline/geoline.spec.js +70 -0
  46. package/__tests__/geofeature/polygon/polygon.data.json +46 -0
  47. package/__tests__/geofeature/polygon/polygon.rules.json +3 -0
  48. package/__tests__/geofeature/polygon/polygon.spec.js +127 -0
  49. package/__tests__/geofeature/zoneBeacon/zoneBeacon.data.json +16 -0
  50. package/__tests__/geofeature/zoneBeacon/zoneBeacon.rules.json +13 -0
  51. package/__tests__/geofeature/zoneBeacon/zoneBeacon.spec.js +62 -0
  52. package/__tests__/monthlyStats/monthlyStats.data.json +29 -0
  53. package/__tests__/monthlyStats/monthlyStats.spec.js +66 -0
  54. package/__tests__/project/project.data.json +18 -0
  55. package/__tests__/project/project.rules.json +9 -0
  56. package/__tests__/project/project.spec.js +92 -0
  57. package/__tests__/ruleRequestAggregation/ruleRequestAggregation.data.json +8 -0
  58. package/__tests__/ruleRequestAggregation/ruleRequestAggregation.spec.js +69 -0
  59. package/__tests__/setup.js +7 -0
  60. package/__tests__/user/user.data.js +19 -0
  61. package/__tests__/user/user.rules.js +8 -0
  62. package/__tests__/user/user.spec.js +130 -0
  63. package/__tests__/utils/utils.spec.js +24 -0
  64. package/__tests__/zone/zone.action.spec.js +50 -0
  65. package/__tests__/zone/zone.condition.spec.js +94 -0
  66. package/__tests__/zone/zone.data.json +157 -0
  67. package/__tests__/zone/zone.geofeatures.spec.js +75 -0
  68. package/__tests__/zone/zone.spec.js +134 -0
  69. package/constants/errorMessages.js +2 -0
  70. package/helpers.js +117 -0
  71. package/models/Account.js +50 -0
  72. package/models/AdminPortalUser.js +10 -11
  73. package/models/Beacon.js +84 -32
  74. package/models/Contract.js +37 -38
  75. package/models/CustomerLogin.js +9 -10
  76. package/models/DBEvent.js +13 -14
  77. package/models/DailyStats.js +7 -7
  78. package/models/DeviceRulesRequest.js +27 -28
  79. package/models/MessageAction.js +28 -26
  80. package/models/MonthlyStats.js +35 -19
  81. package/models/NotificationLogEntry.js +7 -8
  82. package/models/POILocation.js +6 -7
  83. package/models/POILookup.js +8 -9
  84. package/models/POIResult.js +7 -8
  85. package/models/PartnerCustomer.js +12 -13
  86. package/models/PartnerPasswordReset.js +8 -9
  87. package/models/PartnerUser.js +10 -11
  88. package/models/Partners.js +11 -12
  89. package/models/Project.js +55 -0
  90. package/models/RuleRequestAggregation.js +33 -17
  91. package/models/SingleUseToken.js +7 -8
  92. package/models/TestDevice.js +11 -12
  93. package/models/TestDeviceCluster.js +9 -10
  94. package/models/TestInstance.js +9 -10
  95. package/models/TestScenario.js +9 -10
  96. package/models/TimeOfDayRange.js +16 -16
  97. package/models/UrlAction.js +46 -0
  98. package/models/User.js +61 -0
  99. package/models/Zone.js +126 -0
  100. package/models/ZoneUIState.js +18 -18
  101. package/models/conditions/DayRange.js +52 -0
  102. package/models/conditions/Speed.js +42 -0
  103. package/models/conditions/TimeActive.js +50 -0
  104. package/models/conditions/TravelDirection.js +67 -0
  105. package/models/conditions/TravelPath.js +36 -0
  106. package/models/geofeatures/BoundingBox.js +44 -0
  107. package/models/geofeatures/Circle.js +39 -0
  108. package/models/geofeatures/Geoline.js +34 -0
  109. package/models/geofeatures/Polygon.js +45 -0
  110. package/models/geofeatures/ZoneBeacon.js +28 -0
  111. package/package.json +30 -6
  112. package/schemas/MultiLineStringGeometry.js +14 -0
  113. package/schemas/Point.js +13 -0
  114. package/schemas/PointGeometry.js +14 -0
  115. package/schemas/PolygonGeometry.js +14 -0
  116. package/schemas/ProjectPermission.js +21 -0
  117. package/schemas/Webhook.js +27 -0
  118. package/utils/index.js +39 -0
  119. package/validators/checkDistanceBetweenVertices.js +21 -0
  120. package/yarn.lock +2796 -0
  121. package/models/AdminUser.js +0 -10
  122. package/models/Application.js +0 -38
  123. package/models/ApplicationNotificationAction.js +0 -30
  124. package/models/ApplicationUsage.js +0 -11
  125. package/models/BeaconFilter.js +0 -23
  126. package/models/BearingFilter.js +0 -19
  127. package/models/BoundingBox.js +0 -17
  128. package/models/Circle.js +0 -21
  129. package/models/ConfigurationParameters.js +0 -20
  130. package/models/Customer.js +0 -38
  131. package/models/DateRangeFilter.js +0 -20
  132. package/models/Fence.js +0 -21
  133. package/models/FormattedBeacon.js +0 -16
  134. package/models/FormattedCircle.js +0 -12
  135. package/models/FormattedConditions.js +0 -11
  136. package/models/FormattedDateRange.js +0 -11
  137. package/models/FormattedPercentageCrossed.js +0 -12
  138. package/models/FormattedPoint.js +0 -10
  139. package/models/FormattedPolygon.js +0 -11
  140. package/models/FormattedPolyline.js +0 -11
  141. package/models/FormattedRectangle.js +0 -12
  142. package/models/FormattedTimeActive.js +0 -17
  143. package/models/FormattedZone.js +0 -28
  144. package/models/GeoFence.js +0 -23
  145. package/models/Geofeature.js +0 -26
  146. package/models/PercentageCrossedFilter.js +0 -19
  147. package/models/Point.js +0 -20
  148. package/models/Polygon.js +0 -18
  149. package/models/Polyline.js +0 -18
  150. package/models/PropertyMapping.js +0 -17
  151. package/models/Reference.js +0 -19
  152. package/models/SequenceFilter.js +0 -18
  153. package/models/SimpleCheckInZone.js +0 -29
  154. package/models/SoundAction.js +0 -28
  155. package/models/SpeedFilter.js +0 -19
  156. package/models/TimeOfDayRangeFilter.js +0 -19
  157. package/models/URLAction.js +0 -31
  158. package/models/VibrationAction.js +0 -28
  159. package/models/property.js +0 -25
  160. package/package-lock.json.bak +0 -170
@@ -0,0 +1,36 @@
1
+ // In V2 this is becoming "Travel Path" condition
2
+ let mongoose = require('mongoose')
3
+ let uuidv4 = require('uuid/v4')
4
+
5
+ let schema = {
6
+ minimumGeofeaturesCrossed: {
7
+ type: Number,
8
+ required: [true, 'minimumGeofeaturesCrossed is required.'],
9
+ min: 2
10
+ },
11
+ timeOutSeconds: {type: Number, required: [true, 'timeOutSeconds is required. '], min: 60, max: 86400},
12
+ sequential: {type: Boolean, default: false}
13
+ }
14
+
15
+ let TravelPathConditionSchema = new mongoose.Schema(schema, {
16
+ _id: false,
17
+ versionKey: false
18
+ })
19
+
20
+ TravelPathConditionSchema.add({
21
+ type: {type: String, required: true}
22
+ })
23
+
24
+ // optional mongoose
25
+ let model = (mongooseInstance) => {
26
+ let TravelPathCondition
27
+ if (mongooseInstance) {
28
+ TravelPathCondition = mongooseInstance.model('TravelPathCondition', TravelPathConditionSchema)
29
+ } else {
30
+ TravelPathCondition = mongoose.model('TravelPathCondition', TravelPathConditionSchema)
31
+ }
32
+
33
+ return TravelPathCondition
34
+ }
35
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
36
+ module.exports = { Model: model, Schema: TravelPathConditionSchema, rawSchema: schema }
@@ -0,0 +1,44 @@
1
+ let mongoose = require('mongoose')
2
+ let StateEnum = require('@bluedot-innovation/enums').StateEnum
3
+ let uuidv4 = require('uuid/v4')
4
+ let Point = require('../../schemas/Point')
5
+ let helpers = require('../../helpers')
6
+
7
+ const schema = {
8
+ _id: {type: String, default: uuidv4},
9
+ geometry: {type: {}},
10
+ name: {type: String, required: [true, 'name is required.']},
11
+ color: String,
12
+ northEast: {type: Point, required: [true, 'northEast is required.']},
13
+ southWest: {
14
+ type: Point, required: [true, 'southWest is required.'],
15
+ validate: {
16
+ isAsync: true,
17
+ validator: helpers.checkIsDifferent('northEast'),
18
+ message: 'northEast and southWest cannot be the same'
19
+ }
20
+ }
21
+ }
22
+
23
+ const BoundingBoxSchema = new mongoose.Schema(schema, {
24
+ _id: false,
25
+ versionKey: false
26
+ })
27
+
28
+ BoundingBoxSchema.add({
29
+ type: {type: String, required: true}
30
+ })
31
+
32
+ // optional mongoose
33
+ let model = (mongooseInstance) => {
34
+ let BoundingBox
35
+ if (mongooseInstance) {
36
+ BoundingBox = mongooseInstance.model('BoundingBox', BoundingBoxSchema)
37
+ } else {
38
+ BoundingBox = mongoose.model('BoundingBox', BoundingBoxSchema)
39
+ }
40
+
41
+ return BoundingBox
42
+ }
43
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
44
+ module.exports = { Model: model, Schema: BoundingBoxSchema, boundingBoxSchema: schema }
@@ -0,0 +1,39 @@
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
+
6
+ let schema = {
7
+ _id: {type: String, default: uuidv4},
8
+ geometry: {type: {}},
9
+ name: {type: String, required: [true, 'name is required. ']},
10
+ color: String,
11
+ radius: {type: Number, required: [true, 'radius is required. '], min: 5},
12
+ center: {type: Point, required: [true, 'center is required. ']}
13
+ }
14
+
15
+ let CircleSchema = new mongoose.Schema(
16
+ schema,
17
+ {
18
+ _id: false,
19
+ __v: false
20
+ })
21
+
22
+ CircleSchema.add({
23
+ type:
24
+ {type: String, required: true}
25
+ })
26
+
27
+ // optional mongoose
28
+ let model = (mongooseInstance) => {
29
+ let Circle
30
+ if (mongooseInstance) {
31
+ Circle = mongooseInstance.model('Circle', CircleSchema)
32
+ } else {
33
+ Circle = mongoose.model('Circle', CircleSchema)
34
+ }
35
+
36
+ return Circle
37
+ }
38
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
39
+ module.exports = { Model: model, Schema: CircleSchema, circleSchema: schema }
@@ -0,0 +1,34 @@
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
+
6
+ const schema = {
7
+ _id: {type: String, default: uuidv4},
8
+ geometry: {type: {}},
9
+ name: {type: String, required: [true, 'name is required. ']},
10
+ vertices: {type: [Point], required: [true, 'vertices are required. ']}
11
+ }
12
+
13
+ const GeolineSchema = new mongoose.Schema(schema, {
14
+ _id: false,
15
+ versionKey: false
16
+ })
17
+
18
+ GeolineSchema.add({
19
+ type: {type: String, required: true}
20
+ })
21
+
22
+ // optional mongoose
23
+ let model = (mongooseInstance) => {
24
+ let Geoline
25
+ if (mongooseInstance) {
26
+ Geoline = mongooseInstance.model('Geoline', GeolineSchema)
27
+ } else {
28
+ Geoline = mongoose.model('Geoline', GeolineSchema)
29
+ }
30
+
31
+ return Geoline
32
+ }
33
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
34
+ module.exports = { Model: model, Schema: GeolineSchema, geolineSchema: schema }
@@ -0,0 +1,45 @@
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')
8
+
9
+ const schema = {
10
+ _id: {type: String, default: uuidv4},
11
+ geometry: {type: {}},
12
+ name: {type: String, required: [true, 'name is required.']},
13
+ color: String,
14
+ vertices: {
15
+ type: [Point], required: [true, 'vertices is required.'],
16
+ validate: [{
17
+ validator: helpers.threeOrMoreVertices,
18
+ msg: CANT_HAVE_THREE_OR_MORE_VERTICES
19
+ }, {
20
+ validator: checkDistanceBetweenVertices,
21
+ msg: VERTICES_TOO_CLOSE
22
+ }]
23
+ }
24
+ }
25
+
26
+ const PolygonSchema = new mongoose.Schema(schema, {
27
+ _id: false,
28
+ versionKey: false
29
+ })
30
+
31
+ PolygonSchema.add({type: {type: String, required: true}})
32
+
33
+ // optional mongoose
34
+ let model = (mongooseInstance) => {
35
+ let Polygon
36
+ if (mongooseInstance) {
37
+ Polygon = mongooseInstance.model('Polygon', PolygonSchema)
38
+ } else {
39
+ Polygon = mongoose.model('Polygon', PolygonSchema)
40
+ }
41
+
42
+ return Polygon
43
+ }
44
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
45
+ module.exports = { Model: model, Schema: PolygonSchema, polygonSchema: schema }
@@ -0,0 +1,28 @@
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 }
package/package.json CHANGED
@@ -1,15 +1,39 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "4.0.0",
4
- "description": "",
3
+ "version": "5.0.0",
5
4
  "main": "Models.js",
6
5
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
6
+ "lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",
7
+ "test": "mocha --require __tests__/setup __tests__/**/*.spec.js __tests__/**/**/*.spec.js",
8
+ "test:coverage": "nyc mocha --require __tests__/setup __tests__/**/*.spec.js __tests__/**/**/*.spec.js",
9
+ "test:coverage-check": "nyc check-coverage --lines 90 --functions 90 --branches 70",
10
+ "test:ci": "npm run lint && npm run test:coverage-check && npm test"
8
11
  },
9
- "author": "",
10
12
  "license": "UNLICENSED",
13
+ "devDependencies": {
14
+ "babel-eslint": "^8.2.3",
15
+ "chai": "^4.1.2",
16
+ "describe": "*",
17
+ "eslint": "^4.12.0",
18
+ "eslint-config-prettier": "^2.9.0",
19
+ "eslint-config-standard": "^10.2.1",
20
+ "eslint-plugin-import": "^2.8.0",
21
+ "eslint-plugin-node": "^5.2.1",
22
+ "eslint-plugin-prettier": "^2.6.0",
23
+ "eslint-plugin-promise": "^3.6.0",
24
+ "eslint-plugin-standard": "^3.0.1",
25
+ "mocha": "^4.0.1",
26
+ "nyc": "^11.3.0",
27
+ "prettier": "^1.13.5",
28
+ "sinon": "^4.1.2"
29
+ },
11
30
  "dependencies": {
12
- "mongoose": "^5.7.13",
13
- "@bluedot-innovation/enums": "^1.0.2"
31
+ "@bluedot-innovation/enums": "2.1.1",
32
+ "deep-equal": "^1.0.1",
33
+ "fast-deep-equal": "^2.0.1",
34
+ "lodash.clonedeep": "4.5.0",
35
+ "mongoose": "^4.13.9",
36
+ "mongoose-json-patch": "^1.1.1",
37
+ "uuid": "^3.1.0"
14
38
  }
15
39
  }
@@ -0,0 +1,14 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let MultiLineStringSchema = new mongoose.Schema(
4
+ {
5
+ type: {type: String, match: /MultiLineString/},
6
+ coordinates: [[[{type: Number, required: [true, 'longitude is required. '], min: -180, max: 180},
7
+ {type: Number, required: [true, 'latitude is required. '], min: -90, max: 90}]]]
8
+ },
9
+ {
10
+ _id: false,
11
+ __v: false
12
+ })
13
+
14
+ module.exports = MultiLineStringSchema
@@ -0,0 +1,13 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let PointSchema = new mongoose.Schema({
4
+ type: {type: String},
5
+ latitude: {type: Number, required: [true, 'latitude is required. '], min: -90, max: 90},
6
+ longitude: {type: Number, required: [true, 'longitude is required. '], min: -180, max: 180}
7
+ },
8
+ {
9
+ _id: false,
10
+ __v: false
11
+ })
12
+
13
+ module.exports = PointSchema
@@ -0,0 +1,14 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let PointGeometrySchema = new mongoose.Schema(
4
+ {
5
+ type: {type: String, match: /Point/},
6
+ coordinates: [{type: Number, required: [true, 'longitude is required. '], min: -180, max: 180},
7
+ {type: Number, required: [true, 'latitude is required. '], min: -90, max: 90}]
8
+ },
9
+ {
10
+ _id: false,
11
+ __v: false
12
+ })
13
+
14
+ module.exports = PointGeometrySchema
@@ -0,0 +1,14 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let PolygonGeometrySchema = new mongoose.Schema(
4
+ {
5
+ type: {type: String, match: /Polygon/},
6
+ coordinates: [[[{type: Number, required: [true, 'longitude is required. '], min: -180, max: 180},
7
+ {type: Number, required: [true, 'latitude is required. '], min: -90, max: 90}]]]
8
+ },
9
+ {
10
+ _id: false,
11
+ __v: false
12
+ })
13
+
14
+ module.exports = PolygonGeometrySchema
@@ -0,0 +1,21 @@
1
+ const mongoose = require('mongoose')
2
+ const helpers = require('../helpers')
3
+ const PermissionsEnum = require('@bluedot-innovation/enums').PermissionsEnum
4
+
5
+ const ProjectPermissionSchema = new mongoose.Schema({
6
+ permissionLevel: {
7
+ type: String,
8
+ required: true,
9
+ enum: [PermissionsEnum.READ_WRITE, PermissionsEnum.READ]
10
+ },
11
+ projectId: {
12
+ type: String,
13
+ required: true,
14
+ validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'projectId not in UUID v4 format'}
15
+ }
16
+ }, {
17
+ _id: false,
18
+ __v: false
19
+ })
20
+
21
+ module.exports = ProjectPermissionSchema
@@ -0,0 +1,27 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let typeEnum = {
4
+ values: ['trigger', 'configurationChanged'],
5
+ message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
6
+ }
7
+
8
+ let WebhookSchema = new mongoose.Schema({
9
+ type: {type: String, enum: typeEnum, required: [true, 'webhook type is required']}, // configurationChanged, trigger
10
+ url: {type: String, required: [true, 'URL is required'], validate: {isAsync: false, validator: validateUrl, msg: 'Please enter a valid URL'}},
11
+ headers: {type: mongoose.Schema.Types.Mixed, validate: { isAsync: false, validator: validateHeaderlength, msg: 'Header object can only have 1 key <-> value pair' }}
12
+ }, {
13
+ _id: false
14
+ })
15
+
16
+ function validateUrl (url) {
17
+ const expression = 'https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)'
18
+ const regex = new RegExp(expression, 'i')
19
+ return regex.test(url)
20
+ }
21
+
22
+ // https://bluedotinnovation.atlassian.net/browse/BP-1500 -- Limiting headers to 1
23
+ function validateHeaderlength (headers) {
24
+ return Object.keys(headers).length <= 1
25
+ }
26
+
27
+ module.exports = WebhookSchema
package/utils/index.js ADDED
@@ -0,0 +1,39 @@
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) => {
12
+ const scaleFactor = Math.pow(10, n)
13
+ return Math.floor(num * scaleFactor)/scaleFactor
14
+ }
15
+
16
+ const notSameThreeInARow = function(list) {
17
+ const cleanList = clean(list)
18
+ let hasSame = false;
19
+
20
+ for (var i = 0; i < cleanList.length; i++) {
21
+ const itemA = cleanList[i]
22
+ const itemB = cleanList[i - 1]
23
+ const itemC = cleanList[i - 2]
24
+
25
+ if (equal(itemA, itemB)) {
26
+ hasSame = true;
27
+ }
28
+ if (equal(itemA, itemC)) {
29
+ hasSame = true;
30
+ }
31
+ }
32
+ return !hasSame;
33
+ }
34
+
35
+ module.exports = {
36
+ clean,
37
+ trimDecimals,
38
+ notSameThreeInARow,
39
+ }
@@ -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 geolines. 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;