@bluedot-innovation/models 7.10.0 → 7.11.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/.gitlab-ci.yml CHANGED
@@ -63,7 +63,7 @@ create_dev_indexes:
63
63
  - master
64
64
 
65
65
  script:
66
- - ENVIRONMENT=dev MONGO_DB_URI=$DEV_MONGO_DB_URI make deploy-db-changes
66
+ - ENVIRONMENT=dev DB_NAME='config-api-v2' MONGO_DB_URI="$DEV_MONGO_DB_URI" make deploy-db-changes
67
67
 
68
68
  # Create any newly required indexes in the production dbs
69
69
  create_au_production_indexes:
@@ -76,7 +76,7 @@ create_au_production_indexes:
76
76
  - master
77
77
 
78
78
  script:
79
- - ENVIRONMENT=prod_au MONGO_DB_URI="$AU_MONGO_DB_URI" make deploy-db-changes
79
+ - ENVIRONMENT=prod_au DB_NAME='config-api-v2' MONGO_DB_URI="$AU_MONGO_DB_URI" make deploy-db-changes
80
80
 
81
81
  create_eu_production_indexes:
82
82
  stage: release
@@ -88,7 +88,7 @@ create_eu_production_indexes:
88
88
  - master
89
89
 
90
90
  script:
91
- - ENVIRONMENT=prod_eu MONGO_DB_URI="$EU_MONGO_DB_URI" make deploy-db-changes
91
+ - ENVIRONMENT=prod_eu DB_NAME='config-api-v2' MONGO_DB_URI="$EU_MONGO_DB_URI" make deploy-db-changes
92
92
 
93
93
  create_us_production_indexes:
94
94
  stage: release
@@ -100,4 +100,4 @@ create_us_production_indexes:
100
100
  - master
101
101
 
102
102
  script:
103
- - ENVIRONMENT=prod_us MONGO_DB_URI="$US_MONGO_DB_URI" make deploy-db-changes
103
+ - ENVIRONMENT=prod_us DB_NAME='config-api-v2' MONGO_DB_URI="$US_MONGO_DB_URI" make deploy-db-changes
package/CHANGELOG.md CHANGED
@@ -1,9 +1,18 @@
1
+ ## 7.11.1
2
+ - Added unique index to Destination model for Config API testing
3
+
4
+ ## 7.11.0
5
+ - Added Destination model
6
+
1
7
  ## 7.10.0
2
8
  - Add Bbot Integration to Project
3
9
 
4
10
  ## 7.9.0
5
11
  - Add helloOrder webhook
6
12
 
13
+ ## 7.8.0
14
+ - Add oneSignal integration to project
15
+
7
16
  ## 7.7.1
8
17
  - Add Hyrbid-mode configurations to IosConfig
9
18
 
package/Models.js CHANGED
@@ -7,7 +7,7 @@ let Models = {}
7
7
 
8
8
  Models.Account = require('./models/Account').Model
9
9
  Models.Beacon = require('./models/Beacon').Model
10
-
10
+ Models.Destination = require('./models/Destination').Model
11
11
  Models.MessageAction = require('./models/MessageAction').Model
12
12
  Models.Project = require('./models/Project').Model
13
13
  Models.UrlAction = require('./models/UrlAction').Model
@@ -50,6 +50,7 @@ let Schemas = {}
50
50
 
51
51
  Schemas.Account = require('./models/Account').Schema
52
52
  Schemas.Beacon = require('./models/Beacon').Schema
53
+ Schemas.Destination = require('./models/Destination').Schema
53
54
  Schemas.MessageAction = require('./models/MessageAction').Schema
54
55
  Schemas.Application = require('./models/Application').Schema
55
56
  Schemas.Project = require('./models/Project').Schema
@@ -0,0 +1,51 @@
1
+ {
2
+ "lastUpdateTime": "2018-02-14T04:12:02.103Z",
3
+ "creationTime": "2018-02-14T04:12:02.103Z",
4
+ "name": "A Test Destination",
5
+ "email": "test@bluedot.io",
6
+ "accountId":"b0824b76-ce73-4fcf-81f8-1b63edb1114e",
7
+ "projectId":"b0824b76-ce73-4fcf-81f8-1b63edb1114e",
8
+ "destinationId": "test-destination-id",
9
+ "location": {
10
+ "latitude": -37.8159544565362,
11
+ "longitude": 144.9723565578461
12
+ },
13
+ "address": "48 Pirrama Rd, Pyrmont NSW 2009, Australia",
14
+ "address_components": [
15
+ {
16
+ "long_name": "48",
17
+ "short_name": "48",
18
+ "types": ["street_number"]
19
+ },
20
+ {
21
+ "long_name": "Pirrama Road",
22
+ "short_name": "Pirrama Rd",
23
+ "types": ["route"]
24
+ },
25
+ {
26
+ "long_name": "Pyrmont",
27
+ "short_name": "Pyrmont",
28
+ "types": ["locality", "political"]
29
+ },
30
+ {
31
+ "long_name": "City of Sydney",
32
+ "short_name": "City of Sydney",
33
+ "types": ["administrative_area_level_2", "political"]
34
+ },
35
+ {
36
+ "long_name": "New South Wales",
37
+ "short_name": "NSW",
38
+ "types": ["administrative_area_level_1", "political"]
39
+ },
40
+ {
41
+ "long_name": "Australia",
42
+ "short_name": "AU",
43
+ "types": ["country", "political"]
44
+ },
45
+ {
46
+ "long_name": "2009",
47
+ "short_name": "2009",
48
+ "types": ["postal_code"]
49
+ }
50
+ ]
51
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "requiredFields": ["name", "destinationId", "accountId", "location", "address", "address_components", "projectId"]
3
+ }
@@ -0,0 +1,160 @@
1
+ const mongoose = require('mongoose')
2
+ const rules = require("./destination.rules.json");
3
+ mongoose.Promise = global.Promise
4
+ const assert = require('chai').assert
5
+ const DestinationModel = require('../../Models').Destination(mongoose)
6
+ const DestinationModelBuiltInMongoose = require('../../Models').Destination()
7
+ const destinationJson = require('./destination.data.json')
8
+
9
+ let destination = JSON.parse(JSON.stringify(destinationJson))
10
+ describe('Valid Destination ', () => {
11
+ beforeEach(() => {
12
+ destination = JSON.parse(JSON.stringify(destinationJson))
13
+ })
14
+
15
+ it('should validate a valid destination ', (done) => {
16
+ let Destination = new DestinationModel(destination)
17
+ Destination.validate((err, res) => {
18
+ assert.notExists(res)
19
+ assert(err === null, 'err equals null')
20
+ done()
21
+ })
22
+ })
23
+
24
+ it('should validate a valid destination when using built in mongoose ', (done) => {
25
+ let Destination = new DestinationModelBuiltInMongoose(destination)
26
+ Destination.validate((err, res) => {
27
+ assert.notExists(res)
28
+ assert(err === null, 'err equals null')
29
+ done()
30
+ })
31
+ })
32
+
33
+ it('should validate a valid destination ', (done) => {
34
+ let Destination = new DestinationModel(destination)
35
+ Destination.validate((err, res) => {
36
+ assert.notExists(res)
37
+ assert(err === null, 'err equals null')
38
+ done()
39
+ })
40
+ })
41
+
42
+ it('should validate a valid destination without an email', (done) => {
43
+ delete destination.email
44
+ let Destination = new DestinationModel(destination)
45
+ Destination.validate((err, res) => {
46
+ assert.notExists(res)
47
+ assert(err === null, 'err equals null')
48
+ done()
49
+ })
50
+ })
51
+ })
52
+
53
+ describe('Invalid Destination ', () => {
54
+ beforeEach(() => {
55
+ destination = JSON.parse(JSON.stringify(destinationJson))
56
+ })
57
+
58
+ describe('required fields check', () => {
59
+ const requiredFields = rules.requiredFields
60
+ requiredFields.map((i) => {
61
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
62
+ destination = JSON.parse(JSON.stringify(destination))
63
+ delete destination[i]
64
+ let Destination = new DestinationModel(destination)
65
+ Destination.validate((err, res) => {
66
+ assert.notExists(res)
67
+ assert.exists(err, 'err object should exist')
68
+ assert.isObject(err.errors)
69
+ assert.nestedProperty(err, `errors.${i}`, 'error should have a radius property')
70
+ done()
71
+ })
72
+ })
73
+ })
74
+ })
75
+
76
+ it("should throw an error if location latitude is greater than 90", (done) => {
77
+ destination.location.latitude = 91.0213441
78
+ let Destination = new DestinationModel(destination)
79
+ Destination.validate((err, res) => {
80
+ assert.notExists(res)
81
+ assert.exists(err)
82
+ assert.match(err.message, /latitude must be between a value between -90 and 90/)
83
+ done()
84
+ })
85
+ })
86
+
87
+ it("should throw an error if location latitude is less than -90", (done) => {
88
+ destination.location.latitude = -999.0213441
89
+ let Destination = new DestinationModel(destination)
90
+ Destination.validate((err, res) => {
91
+ assert.notExists(res)
92
+ assert.exists(err)
93
+ assert.match(err.message, /latitude must be between a value between -90 and 90/)
94
+ done()
95
+ })
96
+ })
97
+
98
+ it("should throw an error if location longitude is greater than 180", (done) => {
99
+ destination.location.longitude = 181.0213441
100
+ let Destination = new DestinationModel(destination)
101
+ Destination.validate((err, res) => {
102
+ assert.notExists(res)
103
+ assert.exists(err)
104
+ assert.match(err.message, /longitude must be between a value between -180 and 180/)
105
+ done()
106
+ })
107
+ })
108
+
109
+ it("should throw an error if location longitude is less than -180", (done) => {
110
+ destination.location.longitude = -180.0213441
111
+ let Destination = new DestinationModel(destination)
112
+ Destination.validate((err, res) => {
113
+ assert.notExists(res)
114
+ assert.exists(err)
115
+ assert.match(err.message, /longitude must be between a value between -180 and 180/)
116
+ done()
117
+ })
118
+ })
119
+
120
+ it("should throw an error message if address_components is in the wrong format", (done) => {
121
+ const invalidTypeAddressComponents = {
122
+ long_name: "Wellington",
123
+ types: ["City"]
124
+ }
125
+ destination.address_components = [invalidTypeAddressComponents]
126
+ let Destination = new DestinationModel(destination)
127
+ Destination.validate((err, res) => {
128
+ assert.notExists(res)
129
+ assert.exists(err)
130
+ assert.match(err.message, /`short_name` is required/)
131
+ done()
132
+ })
133
+ })
134
+
135
+ it("should throw an error if destinationId is longer than 128 characters", (done) => {
136
+ let destinationId = ""
137
+ for(let i = 0; i < 129; i++){
138
+ destinationId += "a"
139
+ }
140
+ destination.destinationId = destinationId
141
+ let Destination = new DestinationModel(destination)
142
+ Destination.validate((err, res) => {
143
+ assert.notExists(res)
144
+ assert.exists(err)
145
+ assert.match(err.message, /destinationId must be less than 129 characters long/)
146
+ done()
147
+ })
148
+ })
149
+
150
+ it("should throw an error if email is not a valid email", (done) => {
151
+ destination.email = "invalid-email"
152
+ let Destination = new DestinationModel(destination)
153
+ Destination.validate((err, res) => {
154
+ assert.notExists(res)
155
+ assert.exists(err)
156
+ assert.match(err.message, /is not a valid email/)
157
+ done()
158
+ })
159
+ })
160
+ })
@@ -30,6 +30,7 @@ describe('Project', () => {
30
30
  done()
31
31
  })
32
32
  })
33
+
33
34
  assertions('should validate a valid project with android config', (done) => {
34
35
  let project = JSON.parse(JSON.stringify(require('./project.remote.config.json')))
35
36
  let Project = new ProjectModel(project)
@@ -424,30 +424,5 @@ describe('Zone', () => {
424
424
  done()
425
425
  })
426
426
  })
427
-
428
- const invalidTempoGeofeatures = ['geoline', 'beacon']
429
-
430
- invalidTempoGeofeatures.map(type => {
431
- it(`should throw an error when destinationId is set and there is a single ${type} geofeature`, (done) => {
432
- zone.destinationId = "test-destination-id";
433
- zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(new RegExp(type)))
434
- let Zone = new ZoneModel(zone)
435
- Zone.validate((err, res) => {
436
- assert.exists(err, 'err object should exist')
437
- assert.match(err.message, /(destinationId cannot be set on zones with a geoline or beacon)/i)
438
- done()
439
- })
440
- })
441
- })
442
-
443
- it('should throw an error when destinationId is set and there is more than one geofeature', (done) => {
444
- zone.destinationId = "test-destination-id";
445
- let Zone = new ZoneModel(zone)
446
- Zone.validate((err, res) => {
447
- assert.exists(err, 'err object should exist')
448
- assert.match(err.message, /(destinationId can only be set on zones with a single geofeature)/i)
449
- done()
450
- })
451
- })
452
427
  })
453
428
  })
package/helpers.js CHANGED
@@ -247,25 +247,6 @@ helpers.validateDestinationIdLength = function(destinationId) {
247
247
  return true
248
248
  }
249
249
 
250
- helpers.validateDestinationSingleGeofeature = function(destinationId) {
251
- if(destinationId){
252
- const geofeatures = this.geofeatures
253
- if(!geofeatures.length){
254
- return false
255
- }
256
- return geofeatures.length === 1
257
- }
258
- return true
259
- }
260
-
261
- helpers.validateDestinationIdGeofeatureType = function(destinationId) {
262
- const geofeatures = this.geofeatures
263
- if(destinationId && geofeatures.length){
264
- return !(geofeatures[0].type === 'geoline' || geofeatures[0].type === 'beacon')
265
- }
266
- return true
267
- }
268
-
269
250
  helpers.validateNoDuplicateWebhooks = function(webhookArray) {
270
251
  const webhooksByType = {}
271
252
  webhookArray.forEach(webhook => {
@@ -289,5 +270,12 @@ helpers.validateNoDuplicateWebhooks = function(webhookArray) {
289
270
  return true;
290
271
  }
291
272
 
273
+ helpers.validateLatitude = function(latitude){
274
+ return latitude <= 90 && latitude >= -90
275
+ }
276
+
277
+ helpers.validateLongitude = function(longitude){
278
+ return longitude >= -180 && longitude <= 180
279
+ }
292
280
 
293
281
  module.exports = helpers
@@ -0,0 +1,52 @@
1
+ const mongoose = require('mongoose')
2
+ const uuidv4 = require('uuid/v4')
3
+ const patcher = require('@bluedot-innovation/mongoose-json-patch')
4
+ const Point = require('../schemas/Point')
5
+ const helpers = require('../helpers')
6
+ const AddressComponent = require('../schemas/AddressComponent')
7
+
8
+ const DestinationSchema = new mongoose.Schema({
9
+ _id: {type: String, default: uuidv4},
10
+ name: {type: String, required: true},
11
+ email: { type: String, validate: { validator: helpers.validateEmail, message: '{VALUE} is not a valid email'} },
12
+ destinationId: { type: String, required: true, validate: { validator: helpers.validateDestinationIdLength, msg: 'destinationId must be less than 129 characters long' } },
13
+ location: { type: Point, required: true},
14
+ geometry: {type: {}},
15
+ address: {type: String, required: true },
16
+ address_components: {type: [AddressComponent], required: true, default: undefined},
17
+ accountId: {type: String, required: true, validate: {validator: helpers.validateUUIDv4, msg: 'Invalid accountId'}},
18
+ projectId: {type: String, required: true, validate: {validator: helpers.validateUUIDv4, msg: 'Invalid projectId'}},
19
+ creationTime: {type: Date, default: Date.now},
20
+ lastUpdateTime: {type: Date, default: Date.now},
21
+ }, {collection: 'destination'})
22
+
23
+ DestinationSchema.plugin(patcher)
24
+
25
+ /*
26
+ This index is only used during testing to validate behaviour that relies on the index.
27
+ The real index is created by the script in utils/DbInitializer
28
+ */
29
+ DestinationSchema.index(
30
+ {
31
+ projectId: 1,
32
+ destinationId: 1
33
+ },
34
+ {
35
+ unique: true,
36
+ name: "destinationIdUniqueWithinProject"
37
+ })
38
+
39
+ // optional mongoose
40
+ const model = (mongooseInstance) => {
41
+ let Destination
42
+ if (mongooseInstance) {
43
+ Destination = mongooseInstance.model('Destination', DestinationSchema)
44
+ } else {
45
+ Destination = mongoose.model('Destination', DestinationSchema)
46
+ }
47
+ return Destination
48
+ }
49
+
50
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
51
+ module.exports = { Model: model, Schema: DestinationSchema }
52
+
package/models/Zone.js CHANGED
@@ -118,11 +118,7 @@ let zoneSchema = new Schema({
118
118
  projectId: {type: String, required: true, validate: {validator: helpers.validateUUIDv4, message: 'Invalid Project Id.'}},
119
119
  state: {type: String, default: StateEnum.ACTIVE, select: false},
120
120
  accountId: {type: String, required: true, validate: {validator: helpers.validateUUIDv4, msg: 'default error message'}},
121
- destinationId: {type: String, validate: [
122
- { validator: helpers.validateDestinationIdLength, msg: 'destinationId must be less than 129 characters long' },
123
- { validator: helpers.validateDestinationSingleGeofeature, msg: 'destinationId can only be set on zones with a single geofeature' },
124
- { validator: helpers.validateDestinationIdGeofeatureType, msg: 'destinationId cannot be set on zones with a geoline or beacon' }
125
- ]},
121
+ destinationId: {type: String, validate: { validator: helpers.validateDestinationIdLength, msg: 'destinationId must be less than 129 characters long' }},
126
122
  }, {collection: 'zone', autoIndex: false})
127
123
 
128
124
  zoneSchema.index(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "7.10.0",
3
+ "version": "7.11.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",
@@ -0,0 +1,11 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ const AddressComponent = new mongoose.Schema({
4
+ long_name: {type: String, required: true},
5
+ short_name: {type: String, required: true},
6
+ types: {type: [String], required: true, default: undefined}
7
+ }, {
8
+ _id: false
9
+ })
10
+
11
+ module.exports = AddressComponent;
package/schemas/Point.js CHANGED
@@ -1,10 +1,11 @@
1
1
  let mongoose = require('mongoose')
2
2
  const Double = require('@mongoosejs/double')
3
+ const helpers = require("../helpers")
3
4
 
4
5
  let PointSchema = new mongoose.Schema({
5
6
  type: {type: String},
6
- latitude: {type: Double, required: [true, 'latitude is required. '], min: -90, max: 90},
7
- longitude: {type: Double, required: [true, 'longitude is required. '], min: -180, max: 180}
7
+ latitude: {type: Double, required: [true, 'latitude is required. '], validate: {validator: helpers.validateLatitude, message: "latitude must be between a value between -90 and 90"}},
8
+ longitude: {type: Double, required: [true, 'longitude is required. '], validate: {validator: helpers.validateLongitude, message: "longitude must be between a value between -180 and 180"}}
8
9
  },
9
10
  {
10
11
  _id: false,
@@ -13,9 +13,45 @@ const indexes = {
13
13
  destinationId: 1
14
14
  },
15
15
  options: {
16
- unique: true,
16
+ unique: false,
17
17
  partialFilterExpression: { destinationId: { $exists:true, $type:"string" } },
18
- name: "destinationUniqueWithinProject"
18
+ name: "destinationIdWithinProject"
19
+ }
20
+ }
21
+ ],
22
+ destination: [
23
+ {
24
+ fields: {
25
+ projectId: 1,
26
+ destinationId: 1
27
+ },
28
+ options: {
29
+ unique: true,
30
+ name: "destinationIdUniqueWithinProject"
31
+ }
32
+ },
33
+ {
34
+ fields: {
35
+ name: 1
36
+ },
37
+ options: {
38
+ name: "name_index"
39
+ }
40
+ },
41
+ {
42
+ fields: {
43
+ formatted_address: 1
44
+ },
45
+ options: {
46
+ name: "address_index"
47
+ }
48
+ },
49
+ {
50
+ fields: {
51
+ geometry: "2dsphere"
52
+ },
53
+ options: {
54
+ name: "geometry_index"
19
55
  }
20
56
  }
21
57
  ]
@@ -51,9 +87,12 @@ const createIndexIfNotExists = async (collection, fields, options) => {
51
87
  }
52
88
  }
53
89
 
54
- const createAllIndexesIfNotExists = db => {
90
+ const createAllIndexesIfNotExists = async db => {
55
91
  const createIndexPromises = []
56
92
  for(const collectionName in indexes){
93
+ if(!await db.listCollections({ name: collectionName }).hasNext()){
94
+ db.createCollection(collectionName)
95
+ }
57
96
  const collection = db.collection(collectionName);
58
97
  for(const index of indexes[collectionName]){
59
98
  createIndexPromises.push(createIndexIfNotExists(collection, index.fields, index.options))
@@ -64,6 +103,7 @@ const createAllIndexesIfNotExists = db => {
64
103
  return Promise.allSettled(createIndexPromises).then(results => {
65
104
  const hasErrors = results.find(result => result.status === "rejected")
66
105
  if(hasErrors){
106
+ console.log(hasErrors)
67
107
  throw new Error("An error occurred creating indexes")
68
108
  }
69
109
  })