@bluedot-innovation/models 9.1.1 → 10.0.0-beta-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.
@@ -17,73 +17,52 @@ beforeEach(() => {
17
17
 
18
18
  describe('Zone', () => {
19
19
  describe('Valid Zone ', () => {
20
- it('should validate a valid zone ', (done) => {
20
+ it('should validate a valid zone ', async () => {
21
21
  let Zone = new ZoneModel(zone)
22
- Zone.validate((err, res) => {
23
- assert.notExists(res)
24
- assert.notExists(err)
25
- done()
26
- })
22
+ const res = await Zone.validate()
23
+ assert.notExists(res)
27
24
  })
28
25
 
29
- it('should validate a valid zone ', (done) => {
26
+ it('should validate a valid zone ', async () => {
30
27
  let Zone = new ZoneModelBuiltInMongoose(zone)
31
- Zone.validate((err, res) => {
32
- assert.notExists(res)
33
- assert(err === null, 'err equals null')
34
- done()
35
- })
28
+ const res = await Zone.validate()
29
+ assert.notExists(res)
36
30
  })
37
31
 
38
- it('should instantiate a circle', (done) => {
32
+ it('should instantiate a circle', async () => {
39
33
  zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^circle$/))
40
34
  let Zone = new ZoneModel(zone)
41
- Zone.validate((err, res) => {
42
- assert.notExists(res)
43
- assert.notExists(err)
44
- done()
45
- })
35
+ const res = await Zone.validate()
36
+ assert.notExists(res)
46
37
  })
47
38
 
48
- it('should instantiate a bounding box', (done) => {
39
+ it('should instantiate a bounding box', async () => {
49
40
  zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^boundingBox$/))
50
41
  let Zone = new ZoneModel(zone)
51
- Zone.validate((err, res) => {
52
- assert.notExists(res)
53
- assert.notExists(err)
54
- done()
55
- })
42
+ const res = await Zone.validate()
43
+ assert.notExists(res)
56
44
  })
57
45
 
58
- it('should instantiate a polygon', (done) => {
46
+ it('should instantiate a polygon', async () => {
59
47
  zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^polygon$/))
60
48
  let Zone = new ZoneModel(zone)
61
- Zone.validate((err, res) => {
62
- assert.notExists(res)
63
- assert.notExists(err)
64
- done()
65
- })
49
+ const res = await Zone.validate()
50
+ assert.notExists(res)
66
51
  })
67
52
 
68
- it('should instantiate a geoline', (done) => {
53
+ it('should instantiate a geoline', async () => {
69
54
  zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^geoline$/))
70
55
  let Zone = new ZoneModel(zone)
71
- Zone.validate((err, res) => {
72
- assert.notExists(res)
73
- assert.notExists(err)
74
- done()
75
- })
56
+ const res = await Zone.validate()
57
+ assert.notExists(res)
76
58
  })
77
59
 
78
- it('should set triggerOnExit default and save without errors', (done) => {
60
+ it('should set triggerOnExit default and save without errors', async () => {
79
61
  delete zone.triggerOnExit
80
62
 
81
63
  let Zone = new ZoneModel(zone)
82
- Zone.validate((err, res) => {
83
- assert.notExists(res)
84
- assert.notExists(err)
85
- done()
86
- })
64
+ const res = await Zone.validate()
65
+ assert.notExists(res)
87
66
  })
88
67
 
89
68
  it('should set tags to lowercase and remove the duplicated tag', () => {
@@ -94,281 +73,307 @@ describe('Zone', () => {
94
73
  assert.strictEqual(zone.tags[1], "1")
95
74
  })
96
75
 
97
- it('should save without errors when destinationId is null', (done) => {
76
+ it('should save without errors when destinationId is null', async () => {
98
77
  zone.destinationId = null;
99
78
  let Zone = new ZoneModel(zone)
100
- Zone.validate((err, res) => {
101
- assert.notExists(res)
102
- assert.notExists(err)
103
- done()
104
- })
79
+ const res = await Zone.validate()
80
+ assert.notExists(res)
105
81
  })
106
82
 
107
83
  const validTempoGeofeatureTypes = ['boundingBox', 'circle', 'polygon']
108
84
 
109
85
  validTempoGeofeatureTypes.map(type => {
110
- it(`should save without errors when destinationId is set and there is a single ${type} geofeature`, (done) => {
86
+ it(`should save without errors when destinationId is set and there is a single ${type} geofeature`, async () => {
111
87
  zone.destinationId = "test-destination-id";
112
88
  zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(new RegExp(type)))
113
89
  let Zone = new ZoneModel(zone)
114
- Zone.validate((err, res) => {
115
- assert.notExists(res)
116
- assert.notExists(err)
117
- done()
118
- })
90
+ const res = await Zone.validate()
91
+ assert.notExists(res)
119
92
  })
120
93
  })
121
94
  })
122
95
 
123
96
  describe('Invalid Zone ', () => {
124
- it('should throw an error when validating an invalid zone ', (done) => {
97
+ it('should throw an error when validating an invalid zone ', async () => {
125
98
  delete zone.name
126
99
  let Zone = new ZoneModel(zone)
127
- Zone.validate((err, res) => {
100
+ try {
101
+ await Zone.validate()
102
+ } catch (err) {
128
103
  assert.exists(err, 'err object should exist')
129
104
  assert.isObject(err.errors)
130
- done()
131
- })
105
+ }
132
106
  })
133
107
 
134
- it('should throw an error when a condition is invalid', (done) => {
108
+ it('should throw an error when a condition is invalid', async () => {
135
109
  zone.conditions[0].startDate = 'abc'
136
110
  let Zone = new ZoneModel(zone)
137
- Zone.validate((err, res) => {
111
+ try {
112
+ await Zone.validate()
113
+ } catch (err) {
138
114
  assert.exists(err, 'err object should exist')
139
115
  assert.isObject(err.errors)
140
116
  assert('conditions.0.startDate' in err.errors, `errors should have a 'conditions.0.startDate' property`)
141
- done()
142
- })
117
+ }
143
118
  })
144
119
 
145
- it('should throw an error when a condition type is missing', (done) => {
120
+ it('should throw an error when a condition type is missing', async () => {
146
121
  delete zone.conditions[0].type
147
122
  let Zone = new ZoneModel(zone)
148
- Zone.validate((err, res) => {
123
+ try {
124
+ await Zone.validate()
125
+ } catch (err) {
149
126
  assert.exists(err, 'err object should exist')
150
127
  assert.isObject(err.errors)
151
128
  assert.match(err.message, /(Condition requires a type of `dayRange`, `speed`, `timeActive`, `travelDirection` or `travelPath`.)/i)
152
- done()
153
- })
129
+ }
154
130
  })
155
131
 
156
- it('should throw an error when a condition object is empty', (done) => {
132
+ it('should throw an error when a condition object is empty', async () => {
157
133
  zone.conditions = [{}]
158
134
  let Zone = new ZoneModel(zone)
159
- Zone.validate((err, res) => {
135
+ try {
136
+ await Zone.validate()
137
+ } catch (err) {
160
138
  assert.exists(err, 'err object should exist')
161
139
  assert.isObject(err.errors)
162
140
  assert.match(err.message, /(Condition requires a type of `dayRange`, `speed`, `timeActive`, `travelDirection` or `travelPath`.)/i)
163
- done()
164
- })
141
+ }
165
142
  })
166
143
 
167
- it('should throw an error when a conditions is null', (done) => {
144
+ it('should throw an error when a conditions is null', async () => {
168
145
  zone.conditions = null
169
146
  let Zone = new ZoneModel(zone)
170
- Zone.validate((err, res) => {
147
+ try {
148
+ await Zone.validate()
149
+ } catch (err) {
171
150
  assert.exists(err, 'err object should exist')
172
151
  assert.isObject(err.errors)
173
152
  assert.match(err.message, /(conditions must have a value)/i)
174
- done()
175
- })
153
+ }
176
154
  })
177
155
 
178
- it('should throw an error when custom data is null', (done) => {
156
+ it('should throw an error when custom data is null', async () => {
179
157
  zone.customData = null
180
158
  let Zone = new ZoneModel(zone)
181
- Zone.validate((err, res) => {
159
+ try {
160
+ await Zone.validate()
161
+ } catch (err) {
182
162
  assert.exists(err, 'err object should exist')
183
163
  assert.isObject(err.errors)
184
164
  assert.match(err.message, /Zone validation failed: customData/i)
185
- done()
186
- })
165
+ }
187
166
  })
188
167
 
189
- it('should throw an error when custom data is undefined', (done) => {
168
+ it('should not throw an error when custom data is undefined', async () => {
190
169
  zone.customData = undefined
191
170
  let Zone = new ZoneModel(zone)
192
- const b = Zone
193
- Zone.validate((err, res) => {
194
- assert.notExists(err, 'err is neither `null` nor `undefined`')
195
- done()
196
- })
171
+ const res = await Zone.validate()
172
+ assert.notExists(res)
197
173
  })
198
174
 
199
- it('should throw an error when custom data is not an object', (done) => {
175
+ it('should throw an error when custom data is not an object', async () => {
200
176
  zone.customData = 1
201
177
  let Zone = new ZoneModel(zone)
202
- Zone.validate((err, res) => {
178
+ try {
179
+ await Zone.validate()
180
+ } catch (err) {
203
181
  assert.exists(err, 'err object should exist')
204
182
  assert.isObject(err.errors)
205
183
  assert.match(err.message, /Zone validation failed: customData/i)
206
- done()
207
- })
184
+ }
208
185
  })
209
186
 
210
- it('should throw an error if CustomData key does not have alpha numeric and _ characters only', (done) => {
187
+ it('should throw an error if CustomData key does not have alpha numeric and _ characters only', async () => {
211
188
  zone.customData["name#3"] = "A Custom action"
212
189
  let Zone = new ZoneModel(zone)
213
- Zone.validate((err, res) => {
190
+ try {
191
+ await Zone.validate()
192
+ } catch (err) {
193
+ assert.exists(err, 'err object should exist')
194
+ assert.isObject(err.errors)
214
195
  assert.match(err.message, /(CustomData key should have alpha numeric, - and _ characters only)/i);
215
- done()
216
- })
196
+ }
217
197
  })
218
198
 
219
- it('should throw an error if custom data keys count is greater than 20', (done) => {
199
+ it('should throw an error if custom data keys count is greater than 20', async () => {
220
200
  for (let i = 0; i <= 21; i++) {
221
201
  zone.customData[`key${i}`] = `value ${i}`
222
202
  }
223
203
 
224
204
  let Zone = new ZoneModel(zone)
225
- Zone.validate((err, res) => {
205
+ try {
206
+ await Zone.validate()
207
+ } catch (err) {
208
+ assert.exists(err, 'err object should exist')
226
209
  assert.match(err.message, /(Only 20 custom data fields are allowed to be added per zone)/i)
227
- done()
228
- })
210
+ }
229
211
  })
230
212
 
231
- it('should throw an error if CustomData value is null', (done) => {
213
+ it('should throw an error if CustomData value is null', async () => {
232
214
  zone.customData["name3"] = null;
233
215
 
234
216
  let Zone = new ZoneModel(zone)
235
- Zone.validate((err, res) => {
217
+ try {
218
+ await Zone.validate()
219
+ } catch (err) {
220
+ assert.exists(err, 'err object should exist')
236
221
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
237
- done()
238
- })
222
+ }
239
223
  })
240
224
 
241
- it('should throw an error if CustomData value is undefined', (done) => {
225
+ it('should throw an error if CustomData value is undefined', async () => {
242
226
  zone.customData["name3"] = undefined;
243
227
 
244
228
  let Zone = new ZoneModel(zone)
245
- Zone.validate((err, res) => {
229
+ try {
230
+ await Zone.validate()
231
+ } catch (err) {
232
+ assert.exists(err, 'err object should exist')
246
233
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
247
- done()
248
- })
234
+ }
249
235
  })
250
236
 
251
- it('should throw an error if CustomData value is an empty string', (done) => {
237
+ it('should throw an error if CustomData value is an empty string', async () => {
252
238
  zone.customData["name3"] = ' ';
253
239
 
254
240
  let Zone = new ZoneModel(zone)
255
- Zone.validate((err, res) => {
241
+ try {
242
+ await Zone.validate()
243
+ } catch (err) {
244
+ assert.exists(err, 'err object should exist')
256
245
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
257
- done()
258
- })
246
+ }
259
247
  })
260
248
 
261
- it('should throw an error if CustomData value is an empty object', (done) => {
249
+ it('should throw an error if CustomData value is an empty object', async () => {
262
250
  zone.customData["name3"] = {};
263
251
 
264
252
  let Zone = new ZoneModel(zone)
265
- Zone.validate((err, res) => {
253
+ try {
254
+ await Zone.validate()
255
+ } catch (err) {
256
+ assert.exists(err, 'err object should exist')
266
257
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
267
- done()
268
- })
258
+ }
269
259
  })
270
260
 
271
- it('should throw an error if CustomData value is an object', (done) => {
261
+ it('should throw an error if CustomData value is an object', async () => {
272
262
  zone.customData["name3"] = { key: 'value' };
273
263
 
274
264
  let Zone = new ZoneModel(zone)
275
- Zone.validate((err, res) => {
265
+ try {
266
+ await Zone.validate()
267
+ } catch (err) {
268
+ assert.exists(err, 'err object should exist')
276
269
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
277
- done()
278
- })
270
+ }
279
271
  })
280
272
 
281
- it('should throw an error if CustomData value is an empty array', (done) => {
273
+ it('should throw an error if CustomData value is an empty array', async () => {
282
274
  zone.customData["name3"] = [];
283
275
 
284
276
  let Zone = new ZoneModel(zone)
285
- Zone.validate((err, res) => {
277
+ try {
278
+ await Zone.validate()
279
+ } catch (err) {
280
+ assert.exists(err, 'err object should exist')
286
281
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
287
- done()
288
- })
282
+ }
289
283
  })
290
284
 
291
- it('should throw an error if CustomData value is an array', (done) => {
285
+ it('should throw an error if CustomData value is an array', async () => {
292
286
  zone.customData["name3"] = ['value1', 'value2'];
293
287
 
294
288
  let Zone = new ZoneModel(zone)
295
- Zone.validate((err, res) => {
289
+ try {
290
+ await Zone.validate()
291
+ } catch (err) {
292
+ assert.exists(err, 'err object should exist')
296
293
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
297
- done()
298
- })
294
+ }
299
295
  })
300
296
 
301
- it('should throw an error if CustomData value is a number', (done) => {
297
+ it('should throw an error if CustomData value is a number', async () => {
302
298
  zone.customData["name3"] = 1;
303
299
 
304
300
  let Zone = new ZoneModel(zone)
305
- Zone.validate((err, res) => {
301
+ try {
302
+ await Zone.validate()
303
+ } catch (err) {
304
+ assert.exists(err, 'err object should exist')
306
305
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
307
- done()
308
- })
306
+ }
309
307
  })
310
308
 
311
- it('should throw an error if CustomData value is a boolean', (done) => {
309
+ it('should throw an error if CustomData value is a boolean', async () => {
312
310
  zone.customData["name3"] = true;
313
311
 
314
312
  let Zone = new ZoneModel(zone)
315
- Zone.validate((err, res) => {
313
+ try {
314
+ await Zone.validate()
315
+ } catch (err) {
316
+ assert.exists(err, 'err object should exist')
316
317
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
317
- done()
318
- })
318
+ }
319
319
  })
320
320
 
321
- it('should throw an error if CustomData value is a NaN', (done) => {
321
+ it('should throw an error if CustomData value is a NaN', async () => {
322
322
  zone.customData["name3"] = NaN;
323
323
 
324
324
  let Zone = new ZoneModel(zone)
325
- Zone.validate((err, res) => {
325
+ try {
326
+ await Zone.validate()
327
+ } catch (err) {
328
+ assert.exists(err, 'err object should exist')
326
329
  assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
327
- done()
328
- })
330
+ }
329
331
  })
330
332
 
331
- it('should throw an error when it contains both a beacon and a travelDirection condition', (done) => {
333
+ it('should throw an error when it contains both a beacon and a travelDirection condition', async () => {
332
334
  zone.conditions.push(require('../condition/travelDirection/travelDirection.data.json'));
333
335
  let Zone = new ZoneModel(zone)
334
- Zone.validate((err, res) => {
336
+ try {
337
+ await Zone.validate()
338
+ } catch (err) {
335
339
  assert.exists(err, 'err object should exist')
336
340
  assert(err instanceof Error, "Should throw error.")
337
- done()
338
- })
341
+ }
339
342
  })
340
343
 
341
- it('should throw an error when it contains both a beacon and a speed condition', (done) => {
344
+ it('should throw an error when it contains both a beacon and a speed condition', async () => {
342
345
  zone.conditions.push(require('../condition/speed/speed.data.json'));
343
346
  let Zone = new ZoneModel(zone)
344
- Zone.validate((err, res) => {
347
+ try {
348
+ await Zone.validate()
349
+ } catch (err) {
345
350
  assert.exists(err, 'err object should exist')
346
351
  assert(err instanceof Error, "Should throw error.")
347
- done()
348
- })
352
+ }
349
353
  })
350
354
 
351
- it('should throw an error when triggerOnExit is null', (done) => {
355
+ it('should throw an error when triggerOnExit is null', async () => {
352
356
  zone.triggerOnExit = null
353
- helpers.testNullValue(zone, ZoneModel, done)
357
+ await helpers.testNullValue(zone, ZoneModel)
354
358
  })
355
359
 
356
- it('should throw an error when tags is null', (done) => {
360
+ it('should throw an error when tags is null', async () => {
357
361
  zone.tags = null
358
- helpers.testNullValue(zone, ZoneModel, done)
362
+ await helpers.testNullValue(zone, ZoneModel)
359
363
  })
360
364
 
361
- it('should throw an error when projectId is invalid', (done) => {
365
+ it('should throw an error when projectId is invalid', async () => {
362
366
  zone.projectId = "invalid id"
363
367
  let Zone = new ZoneModel(zone)
364
- Zone.validate((err, res) => {
368
+ try {
369
+ await Zone.validate()
370
+ } catch (err) {
365
371
  assert.exists(err, 'err object should exist')
366
372
  assert.match(err.message, /(Invalid Project Id)/i)
367
- done()
368
- })
373
+ }
369
374
  })
370
375
 
371
- it('should throw an error when a zone has a low accuracy fence with Speed/travelDirection condition', (done) => {
376
+ it('should throw an error when a zone has a low accuracy fence with Speed/travelDirection condition', async () => {
372
377
  zone.conditions.push({
373
378
  "type": "speed",
374
379
  "minimumSpeed": 0,
@@ -390,11 +395,12 @@ describe('Zone', () => {
390
395
  }
391
396
  ]
392
397
  let Zone = new ZoneModel(zone)
393
- Zone.validate((err, res) => {
398
+ try {
399
+ await Zone.validate()
400
+ } catch (err) {
394
401
  assert.exists(err, 'err object should exist')
395
402
  assert.match(err.message, /(This Zone contains one or more large Geofences. Travel Direction\/Speed conditions cannot be added to a Zone that contains large Geofences.)/i)
396
- done()
397
- })
403
+ }
398
404
  })
399
405
 
400
406
  describe('should throw an error when any of the values of the array contains a falsy value', () => {
@@ -402,32 +408,34 @@ describe('Zone', () => {
402
408
 
403
409
  arrayTypeFields.map(arrayField => {
404
410
  FALSY_VALUES.map(falsyValue => {
405
- it(`should trow an error when it contains ${falsyValue} on a ${arrayField}`, (done) => {
406
- helpers.testFalsyValue(falsyValue, arrayField, zone, ZoneModel, done)
411
+ it(`should trow an error when it contains ${falsyValue} on a ${arrayField}`, async () => {
412
+ await helpers.testFalsyValue(falsyValue, arrayField, zone, ZoneModel)
407
413
  })
408
414
  })
409
415
  })
410
416
  })
411
417
 
412
- it('should throw an error when geofeatures have the same _id', (done) => {
418
+ it('should throw an error when geofeatures have the same _id', async () => {
413
419
  zone.geofeatures[0]._id = GEOFEATURE_ID
414
420
  zone.geofeatures[1]._id = GEOFEATURE_ID
415
421
  let Zone = new ZoneModel(zone)
416
- Zone.validate((err, res) => {
422
+ try {
423
+ await Zone.validate()
424
+ } catch (err) {
417
425
  assert.exists(err, 'err object should exist')
418
426
  assert.match(err.message, /(Duplicate values are not allowed for geofeatures _id.)/i)
419
- done()
420
- })
427
+ }
421
428
  })
422
429
 
423
- it('should throw an error when destinationId is longer than 128 characters', (done) => {
430
+ it('should throw an error when destinationId is longer than 128 characters', async () => {
424
431
  zone.destinationId = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s';
425
432
  let Zone = new ZoneModel(zone)
426
- Zone.validate((err, res) => {
433
+ try {
434
+ await Zone.validate()
435
+ } catch (err) {
427
436
  assert.exists(err, 'err object should exist')
428
437
  assert.match(err.message, /(destinationId must be less than 129 characters long)/i)
429
- done()
430
- })
438
+ }
431
439
  })
432
440
  })
433
441
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluedot-innovation/models",
3
- "version": "9.1.1",
3
+ "version": "10.0.0-beta-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",
@@ -31,14 +31,14 @@
31
31
  "dependencies": {
32
32
  "@bluedot-innovation/enums": "^2.1.5",
33
33
  "@bluedot-innovation/mongoose-json-patch": "^0.0.9",
34
- "@mongoosejs/double": "^0.2.0",
34
+ "@mongoosejs/double": "^0.3.0",
35
35
  "@turf/area": "^6.0.1",
36
36
  "@turf/helpers": "^6.1.4",
37
37
  "deep-equal": "^1.0.1",
38
38
  "fast-deep-equal": "^2.0.1",
39
39
  "lodash.clonedeep": "4.5.0",
40
40
  "moment-timezone": "^0.5.45",
41
- "mongoose": "6.12.0",
41
+ "mongoose": "^8.5.4",
42
42
  "uuid": "^3.1.0"
43
43
  }
44
44
  }
@@ -143,7 +143,7 @@ const createAllIndexesIfNotExists = async db => {
143
143
  const createIndexPromises = []
144
144
  for(const collectionName in indexes){
145
145
  if(!await db.listCollections({ name: collectionName }).hasNext()){
146
- db.createCollection(collectionName)
146
+ await db.createCollection(collectionName)
147
147
  }
148
148
  const collection = db.collection(collectionName);
149
149
  for(const index of indexes[collectionName]){
@@ -1,4 +1,3 @@
1
- const {val} = require("mongoose/lib/helpers/populate/SkipPopulateValue");
2
1
  const URL_REGEX = new RegExp("^(https?|ftp)://[^\\s/$.?#].[^\\s]*$", "i")
3
2
  exports.validateUrl = function(url) {
4
3
  return URL_REGEX.test(url)