@bluedot-innovation/models 2.0.0-beta.7 → 2.0.0-beta.9

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/Models.js CHANGED
@@ -18,6 +18,7 @@ Models.Polyline = require('./models/Polyline').Model
18
18
  Models.SpeedFilter = require('./models/SpeedFilter').Model
19
19
  Models.TimeOfDayRangeFilter = require('./models/TimeOfDayRangeFilter').Model
20
20
  Models.UrlAction = require('./models/UrlAction').Model
21
+ Models.User = require('./models/User').Model
21
22
  Models.Zone = require('./models/Zone').Model
22
23
  Models.Project = require('./models/Project').Model
23
24
  Models.Project = require('./models/Project').Model
@@ -40,6 +41,7 @@ Schemas.Polyline = require('./models/Polyline').Schema
40
41
  Schemas.SpeedFilter = require('./models/SpeedFilter').Schema
41
42
  Schemas.TimeOfDayRangeFilter = require('./models/TimeOfDayRangeFilter').Schema
42
43
  Schemas.UrlAction = require('./models/UrlAction').Schema
44
+ Schemas.User = require('./models/User').Schema
43
45
  Schemas.Zone = require('./models/Zone').Schema
44
46
  Schemas.Project = require('./models/Project').Schema
45
47
  Schemas.MonthlyStat = require('./models/MonthlyStats').Schema
@@ -9,5 +9,6 @@
9
9
  "type": "trigger",
10
10
  "url": "https://webhooklistener.bluedotinnovation.com",
11
11
  "enabled": true
12
- }
12
+ },
13
+ "firebaseApiKey": "88888888-4444-4444-4444-121212121212"
13
14
  }
@@ -39,7 +39,6 @@ testCase('Project', () => {
39
39
  })
40
40
  })
41
41
  })
42
-
43
42
  testCase('Invalid Zone ', () => {
44
43
  testCase('recursive property check', () => {
45
44
  let requiredFields = rules.requiredFields
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ username: 'test@bluedot.io',
3
+ emailAddress: 'test@bluedot.io',
4
+ password: 'jfmamjasond',
5
+ firstName: 'John',
6
+ lastName: 'Citizen',
7
+ phoneNumber: '23429384',
8
+ state: 'ACTIVE',
9
+ lastUpdateTime: '2018-02-14T04:12:02.103Z',
10
+ creationTime: '2018-02-14T04:12:02.103Z',
11
+ isAdmin: true,
12
+ accountId: '37ac5be3-60ec-4fb7-9f88-a4f6e8895f81'
13
+ }
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ requiredFields: ['username', 'password', 'firstName', 'lastName', 'isAdmin', 'accountId'],
3
+ customValidators: [
4
+ {name: 'phoneNumber', invalidInputs: ['409203492039284232034398']},
5
+ {name: 'creationTime', invalidInputs: ['21/07/2017', 'thursday']},
6
+ {name: 'lastUpdateTime', invalidInputs: ['21/07/2017', 'saturday']}
7
+ ]
8
+ }
@@ -0,0 +1,83 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ let UserModel = require('../../Models').User(mongoose)
4
+ let UserModelBuiltInMongoose = require('../../Models').User()
5
+ let testCase = require('mocha').describe
6
+ let assertions = require('mocha').it
7
+ let rules = require('./user.rules.js')
8
+ let chai = require('chai')
9
+ let assert = chai.assert
10
+ let mocha = require('mocha')
11
+ let beforeEach = mocha.beforeEach
12
+
13
+ let user
14
+
15
+ beforeEach(() => {
16
+ user = require('./user.data.js')
17
+ })
18
+
19
+ testCase('User', () => {
20
+ testCase('Valid User ', () => {
21
+ assertions('should validate a valid user when initializing the model with an external mongoose instance', (done) => {
22
+ let User = new UserModel(user)
23
+
24
+ User.validate((err, res) => {
25
+ assert.notExists(res)
26
+ assert(err === null, 'err equals null')
27
+ done()
28
+ })
29
+ })
30
+
31
+ assertions('should validate a valid user', (done) => {
32
+ let User = new UserModelBuiltInMongoose(user)
33
+
34
+ User.validate((err, res) => {
35
+ assert.notExists(res)
36
+ assert(err === null, 'err equals null')
37
+ done()
38
+ })
39
+ })
40
+ })
41
+
42
+ testCase('Invalid User', () => {
43
+ testCase('recursive property check', () => {
44
+ let requiredFields = rules.requiredFields
45
+ requiredFields.forEach((requiredField) => {
46
+ assertions(`should throw an error with property '${requiredField}' if '${requiredField}' is not present`, (done) => {
47
+ user = JSON.parse(JSON.stringify(user))
48
+ delete user[requiredField]
49
+ let User = new UserModel(user)
50
+ User.validate((err, res) => {
51
+ assert.notExists(res)
52
+ assert.exists(err, 'err object should exist')
53
+ assert.isObject(err)
54
+ assert.nestedProperty(err, `errors.${requiredField}`, `error should have a ${requiredField} property`)
55
+ done()
56
+ })
57
+ })
58
+ })
59
+ })
60
+ })
61
+
62
+ testCase('recursive invalid entry check', () => {
63
+ const { customValidators } = rules
64
+
65
+ customValidators.forEach((customValidator) => {
66
+ customValidator.invalidInputs.forEach((invalidInput) => {
67
+ assertions(`should throw an error with property '${customValidator.name}' if '${customValidator.name}' is invalid => ${customValidator.name} = ${invalidInput}`, (done) => {
68
+ user = JSON.parse(JSON.stringify(user))
69
+ user[customValidator.name] = invalidInput
70
+ let User = new UserModel(user)
71
+
72
+ User.validate((err, res) => {
73
+ assert.notExists(res)
74
+ assert.exists(err, 'err object should exist')
75
+ assert.isObject(err)
76
+ assert.nestedProperty(err, `errors.${customValidator.name}`, `error should have a ${customValidator.name} property`)
77
+ done()
78
+ })
79
+ })
80
+ })
81
+ })
82
+ })
83
+ })
package/helpers.js CHANGED
@@ -6,4 +6,9 @@ helpers.validateUUIDv4 = function (id) {
6
6
  return regex.test(id)
7
7
  }
8
8
 
9
+ helpers.validateEmail = function (value) {
10
+ const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
11
+ return regex.test(value);
12
+ }
13
+
9
14
  module.exports = helpers
package/models/Account.js CHANGED
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * Created by Richard Tan on 27/02/18.
3
3
  * Copyright : Bluedot Innovation.
4
- * Owner :
5
4
  */
6
5
 
7
6
  let mongoose = require('mongoose')
package/models/Project.js CHANGED
@@ -31,7 +31,8 @@ let ProjectSchema = new mongoose.Schema({
31
31
  name: {type: String, required: [true, 'name is required']},
32
32
  accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}},
33
33
  webhooks: [WebhookSchema],
34
- configurationParameters: {type: Schema.Types.Mixed, default: null} // http://collab.bluedot.io/display/PD/Remote+configuration
34
+ configurationParameters: {type: Schema.Types.Mixed, default: null}, // http://collab.bluedot.io/display/PD/Remote+configuration
35
+ firebaseApiKey: {type: String} // BP-491
35
36
  },
36
37
  {collection: 'project'})
37
38
 
package/models/User.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Created by Richard Tan on 27/02/18.
3
+ * Copyright : Bluedot Innovation.
4
+ */
5
+
6
+ let StateEnum = require('@bluedot-innovation/enums').StateEnum
7
+ let mongoose = require('mongoose')
8
+ let patcher = require('mongoose-json-patch')
9
+ const helpers = require('../helpers')
10
+
11
+ let UserSchema = new mongoose.Schema({
12
+ username: {
13
+ type: String,
14
+ required: true,
15
+ maxlength: 100
16
+ },
17
+ emailAddress: {
18
+ type: String,
19
+ required: true,
20
+ validate: {
21
+ validator: helpers.validateEmail,
22
+ message: '{VALUE} is not a valid email'
23
+ }
24
+ },
25
+ password: {type: String, required: true},
26
+ firstName: {type: String, required: true},
27
+ lastName: {type: String, required: true},
28
+ phoneNumber: {type: String, maxlength: 20},
29
+ state: {type: String, default: StateEnum.active},
30
+ creationTime: {type: Date, default: Date.now},
31
+ lastUpdateTime: {type: Date, default: Date.now},
32
+ isAdmin: {type: Boolean, required: true},
33
+ secret: String,
34
+ projectPermissions: Array,
35
+ accountId: {
36
+ type: String,
37
+ required: true,
38
+ validator: helpers.validateUUIDv4
39
+ }
40
+ },
41
+ {collection: 'user'})
42
+
43
+ UserSchema.plugin(patcher)
44
+
45
+ // optional mongoose
46
+ let model = (mongooseInstance) => {
47
+ let User
48
+ if (mongooseInstance) {
49
+ User = mongooseInstance.model('User', UserSchema)
50
+ } else {
51
+ User = mongoose.model('User', UserSchema)
52
+ }
53
+
54
+ return User
55
+ }
56
+
57
+ // Exports Mongoose Model, Mongoose Schema, JSON schema (required locally for discriminators)
58
+ module.exports = { Model: model, Schema: UserSchema }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "",
5
5
  "main": "Models.js",
6
6
  "scripts": {
@@ -1,11 +0,0 @@
1
- /**
2
- * Created by Arun on 10/09/2014.
3
- * Copyright : Bluedot Innovations.
4
- * Owner :
5
- */
6
- let mongoose = require('mongoose')
7
- let AdminUser = mongoose.model('AdminUser', new mongoose.Schema({},
8
- { collection: 'AdminUser' }
9
- ))
10
-
11
- module.exports = AdminUser
@@ -1,38 +0,0 @@
1
- /**
2
- * Created by Arun on 10/09/2014.
3
- * Copyright : Bluedot Innovations.
4
- * Owner :
5
- */
6
- let mongoose = require('mongoose')
7
- let StateEnum = require('@bluedot-innovation/enums').StateEnum
8
- let Schema = mongoose.Schema
9
-
10
- let Customer = mongoose.model('Customer', new mongoose.Schema(
11
- {
12
- _id: String,
13
- customerApiKey: String,
14
- email: String,
15
- password: String,
16
- name: String,
17
- userName: String,
18
- state: {type: String, default: StateEnum.active},
19
- 'creationTime': {type: Date, default: Date.now},
20
- 'lastUpdateTime': {type: Date, default: Date.now},
21
- 'dataSource': {type: String, select: false, default: 'openApi'},
22
- integrations: Schema.Types.Mixed,
23
- firstName: String,
24
- lastName: String,
25
- oldPassword: String,
26
- organisationWebsite: String,
27
- organisationName: String,
28
- phoneNumber: String,
29
- createdBy: String,
30
- poiCredits: {type: Number, default: 0},
31
- address: []
32
- },
33
- {
34
- collection: 'customer'
35
- })
36
- )
37
-
38
- module.exports = Customer