@@ -7,6 +7,7 @@ import { isBlank } from '@ember/utils';
7
7
import { getOwner } from '@ember/application' ;
8
8
import { format as formatDate , formatDistanceToNow , isValid as isValidDate } from 'date-fns' ;
9
9
import isNotModel from '@fleetbase/ember-core/utils/is-not-model' ;
10
+ import shouldNotLoadRelation from '../utils/should-not-load-relation' ;
10
11
11
12
export default class OrderModel extends Model {
12
13
/** @ids */
@@ -51,6 +52,7 @@ export default class OrderModel extends Model {
51
52
/** @attributes */
52
53
@attr ( 'string' ) tracking ;
53
54
@attr ( 'string' ) qr_code ;
55
+ @attr ( 'string' ) barcode ;
54
56
@attr ( 'string' ) pickup_name ;
55
57
@attr ( 'string' ) dropoff_name ;
56
58
@attr ( 'string' ) driver_name ;
@@ -79,6 +81,8 @@ export default class OrderModel extends Model {
79
81
@attr ( 'boolean' ) facilitator_is_vendor ;
80
82
@attr ( 'raw' ) meta ;
81
83
@attr ( 'raw' ) options ;
84
+ @attr ( 'raw' ) tracker_data ;
85
+ @attr ( 'raw' ) eta ;
82
86
83
87
/** @dates */
84
88
@attr ( 'date' ) scheduled_at ;
@@ -284,6 +288,10 @@ export default class OrderModel extends Model {
284
288
return this . payload ?. isMultiDrop ;
285
289
}
286
290
291
+ @computed ( 'status' ) get hasActiveStatus ( ) {
292
+ return this . status !== 'canceled' && this . status !== 'completed' ;
293
+ }
294
+
287
295
@computed ( 'has_driver_assigned' , 'driver_assigned' ) get canLoadDriver ( ) {
288
296
return this . has_driver_assigned && ! this . driver_assigned ;
289
297
}
@@ -322,7 +330,6 @@ export default class OrderModel extends Model {
322
330
}
323
331
324
332
setProperties ( this , { payload } ) ;
325
-
326
333
return this ;
327
334
}
328
335
@@ -353,7 +360,6 @@ export default class OrderModel extends Model {
353
360
}
354
361
355
362
setProperties ( this , { meta } ) ;
356
-
357
363
return this ;
358
364
}
359
365
@@ -383,7 +389,6 @@ export default class OrderModel extends Model {
383
389
}
384
390
385
391
setProperties ( this , { meta } ) ;
386
-
387
392
return this ;
388
393
}
389
394
@@ -408,7 +413,6 @@ export default class OrderModel extends Model {
408
413
}
409
414
410
415
setProperties ( this , { meta : serializedMeta } ) ;
411
-
412
416
return this ;
413
417
}
414
418
@@ -425,165 +429,158 @@ export default class OrderModel extends Model {
425
429
options . onBefore ( this ) ;
426
430
}
427
431
428
- return fetch . put ( `orders/${ this . id } ` , properties , { normalizeToEmberData : true , normalizeModelType : 'order' } ) . then ( ( order ) => {
429
- if ( typeof options . onAfter === 'function' ) {
430
- options . onAfter ( order ) ;
431
- }
432
- } ) ;
432
+ const order = await fetch . put ( `orders/${ this . id } ` , properties , { normalizeToEmberData : true , normalizeModelType : 'order' } ) ;
433
+ if ( typeof options . onAfter === 'function' ) {
434
+ options . onAfter ( order ) ;
435
+ }
433
436
}
434
437
435
438
async loadPayload ( options = { } ) {
436
439
const owner = getOwner ( this ) ;
437
440
const store = owner . lookup ( 'service:store' ) ;
438
-
439
- if ( ! this . payload_uuid || ! isBlank ( this . payload ) ) {
441
+ if ( shouldNotLoadRelation ( this , 'payload' ) ) {
440
442
return ;
441
443
}
442
444
443
- return store
444
- . queryRecord (
445
- 'payload' ,
446
- {
447
- uuid : this . payload_uuid ,
448
- single : true ,
449
- with : [ 'pickup' , 'dropoff' , 'return' , 'waypoints' , 'entities' ] ,
450
- } ,
451
- options
452
- )
453
- . then ( ( payload ) => {
454
- this . set ( 'payload' , payload ) ;
455
- return payload ;
456
- } ) ;
445
+ const payload = await store . queryRecord (
446
+ 'payload' ,
447
+ {
448
+ uuid : this . payload_uuid ,
449
+ single : true ,
450
+ with : [ 'pickup' , 'dropoff' , 'return' , 'waypoints' , 'entities' ] ,
451
+ } ,
452
+ options
453
+ ) ;
454
+
455
+ this . set ( 'payload' , payload ) ;
456
+ return payload ;
457
457
}
458
458
459
459
async loadCustomer ( options = { } ) {
460
460
const owner = getOwner ( this ) ;
461
461
const store = owner . lookup ( 'service:store' ) ;
462
+ if ( shouldNotLoadRelation ( this , 'customer' ) ) {
463
+ return ;
464
+ }
462
465
463
466
if ( ! this . customer_uuid || ! isBlank ( this . customer ) ) {
464
467
return ;
465
468
}
466
469
467
- return store . findRecord ( `customer-${ this . customer_type } ` , this . customer_uuid , options ) . then ( ( customer ) => {
468
- this . set ( 'customer' , customer ) ;
469
- return customer ;
470
- } ) ;
470
+ const customer = await store . findRecord ( `customer-${ this . customer_type } ` , this . customer_uuid , options ) ;
471
+ this . set ( 'customer' , customer ) ;
472
+ return customer ;
471
473
}
472
474
473
475
async loadPurchaseRate ( options = { } ) {
474
476
const owner = getOwner ( this ) ;
475
477
const store = owner . lookup ( 'service:store' ) ;
476
-
477
- if ( ! this . purchase_rate_uuid || ! isBlank ( this . purchase_rate ) ) {
478
+ if ( shouldNotLoadRelation ( this , 'purchase_rate' ) ) {
478
479
return ;
479
480
}
480
481
481
- return store . findRecord ( 'purchase-rate' , this . purchase_rate_uuid , options ) . then ( ( purchaseRate ) => {
482
- this . set ( 'purchase_rate' , purchaseRate ) ;
483
- return purchaseRate ;
484
- } ) ;
482
+ const purchaseRate = await store . findRecord ( 'purchase-rate' , this . purchase_rate_uuid , options ) ;
483
+ this . set ( 'purchase_rate' , purchaseRate ) ;
484
+ return purchaseRate ;
485
485
}
486
486
487
487
async loadOrderConfig ( options = { } ) {
488
488
const owner = getOwner ( this ) ;
489
489
const store = owner . lookup ( 'service:store' ) ;
490
-
491
- if ( ! this . order_config_uuid || ! isBlank ( this . order_config ) ) {
490
+ if ( shouldNotLoadRelation ( this , 'order_config' ) ) {
492
491
return ;
493
492
}
494
493
495
- return store . findRecord ( 'order-config' , this . order_config_uuid , options ) . then ( ( orderConfig ) => {
496
- this . set ( 'order_config' , orderConfig ) ;
497
- return orderConfig ;
498
- } ) ;
494
+ const orderConfig = await store . findRecord ( 'order-config' , this . order_config_uuid , options ) ;
495
+ this . set ( 'order_config' , orderConfig ) ;
496
+ return orderConfig ;
499
497
}
500
498
501
499
async loadDriver ( options = { } ) {
502
500
const owner = getOwner ( this ) ;
503
501
const store = owner . lookup ( 'service:store' ) ;
504
-
505
- if ( ! this . driver_assigned_uuid || ! isBlank ( this . driver_assigned ) ) {
502
+ if ( shouldNotLoadRelation ( this , 'driver_assigned' ) ) {
506
503
return ;
507
504
}
508
505
509
- return store . findRecord ( 'driver' , this . driver_assigned_uuid , options ) . then ( ( driver ) => {
510
- this . set ( 'driver_assigned' , driver ) ;
511
- return driver ;
512
- } ) ;
506
+ const driverAssigned = await store . findRecord ( 'driver' , this . driver_assigned_uuid , options ) ;
507
+ this . set ( 'driver_assigned' , driverAssigned ) ;
508
+ return driverAssigned ;
513
509
}
514
510
515
511
async loadTrackingNumber ( options = { } ) {
516
512
const owner = getOwner ( this ) ;
517
513
const store = owner . lookup ( 'service:store' ) ;
518
-
519
- if ( ! this . tracking_number_uuid || ! isBlank ( this . tracking_number ) ) {
514
+ if ( shouldNotLoadRelation ( this , 'tracking_number' ) ) {
520
515
return ;
521
516
}
522
517
523
- return store . findRecord ( 'tracking-number' , this . tracking_number_uuid , options ) . then ( ( trackingNumber ) => {
524
- this . set ( 'tracking_number' , trackingNumber ) ;
525
- return trackingNumber ;
526
- } ) ;
518
+ const trackingNumber = await store . findRecord ( 'tracking-number' , this . tracking_number_uuid , options ) ;
519
+ this . set ( 'tracking_number' , trackingNumber ) ;
520
+ return trackingNumber ;
527
521
}
528
522
529
523
async loadTrackingActivity ( options = { } ) {
530
524
const owner = getOwner ( this ) ;
531
525
const store = owner . lookup ( 'service:store' ) ;
532
-
533
526
if ( ! this . tracking_number_uuid ) {
534
527
return ;
535
528
}
536
529
537
- return store
538
- . query (
539
- 'tracking-status' ,
540
- {
541
- tracking_number_uuid : this . tracking_number_uuid ,
542
- } ,
543
- options
544
- )
545
- . then ( ( activity ) => {
546
- this . set ( 'tracking_statuses' , activity . toArray ( ) ) ;
547
- return activity ;
548
- } ) ;
530
+ const activity = await store . query (
531
+ 'tracking-status' ,
532
+ {
533
+ tracking_number_uuid : this . tracking_number_uuid ,
534
+ } ,
535
+ options
536
+ ) ;
537
+
538
+ this . set ( 'tracking_statuses' , activity . toArray ( ) ) ;
539
+ return activity ;
549
540
}
550
541
551
542
async loadComments ( options = { } ) {
552
543
const owner = getOwner ( this ) ;
553
544
const store = owner . lookup ( 'service:store' ) ;
554
545
555
- return store
556
- . query (
557
- 'comment' ,
558
- {
559
- subject_uuid : this . id ,
560
- withoutParent : 1 ,
561
- sort : '-created_at' ,
562
- } ,
563
- options
564
- )
565
- . then ( ( comments ) => {
566
- this . set ( 'comments' , comments ) ;
567
- return comments ;
568
- } ) ;
546
+ const comments = await store . query (
547
+ 'comment' ,
548
+ {
549
+ subject_uuid : this . id ,
550
+ withoutParent : 1 ,
551
+ sort : '-created_at' ,
552
+ } ,
553
+ options
554
+ ) ;
555
+
556
+ this . set ( 'comments' , comments ) ;
557
+ return comments ;
569
558
}
570
559
571
560
async loadFiles ( options = { } ) {
572
561
const owner = getOwner ( this ) ;
573
562
const store = owner . lookup ( 'service:store' ) ;
563
+ const files = await store . query ( 'file' , { subject_uuid : this . id , sort : '-created_at' } , options ) ;
564
+
565
+ this . set ( 'files' , files ) ;
566
+ return files ;
567
+ }
568
+
569
+ async loadTrackerData ( params = { } , options = { } ) {
570
+ const owner = getOwner ( this ) ;
571
+ const fetch = owner . lookup ( 'service:fetch' ) ;
572
+ const trackerData = await fetch . get ( `orders/${ this . id } /tracker` , params , options ) ;
573
+
574
+ this . set ( 'tracker_data' , trackerData ) ;
575
+ return trackerData ;
576
+ }
577
+
578
+ async loadETA ( params = { } , options = { } ) {
579
+ const owner = getOwner ( this ) ;
580
+ const fetch = owner . lookup ( 'service:fetch' ) ;
581
+ const eta = await fetch . get ( `orders/${ this . id } /eta` , params , options ) ;
574
582
575
- return store
576
- . query (
577
- 'file' ,
578
- {
579
- subject_uuid : this . id ,
580
- sort : '-created_at' ,
581
- } ,
582
- options
583
- )
584
- . then ( ( files ) => {
585
- this . set ( 'files' , files ) ;
586
- return files ;
587
- } ) ;
583
+ this . set ( 'eta' , eta ) ;
584
+ return eta ;
588
585
}
589
586
}
0 commit comments