Skip to content

Commit 1a35da3

Browse files
committed
several data improvements
1 parent a4f1eb7 commit 1a35da3

16 files changed

+277
-156
lines changed

addon/models/driver.js

+14-21
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { getOwner } from '@ember/application';
55
import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns';
66
import isRelationMissing from '@fleetbase/ember-core/utils/is-relation-missing';
77
import isValidCoordinates from '@fleetbase/ember-core/utils/is-valid-coordinates';
8-
import isLatitude from '@fleetbase/ember-core/utils/is-latitude';
9-
import isLongitude from '@fleetbase/ember-core/utils/is-longitude';
108
import config from 'ember-get-config';
119

1210
export default class DriverModel extends Model {
@@ -123,33 +121,24 @@ export default class DriverModel extends Model {
123121
return formatDate(this.created_at, 'dd, MMM');
124122
}
125123

126-
@computed('location') get latitude() {
127-
if (this.location) {
128-
let x = get(this.location, 'coordinates.0');
129-
let y = get(this.location, 'coordinates.1');
130-
131-
return isLatitude(x) ? x : y;
132-
}
133-
134-
return 0;
135-
}
136-
137124
@computed('location') get longitude() {
138-
if (this.location) {
139-
let x = get(this.location, 'coordinates.0');
140-
let y = get(this.location, 'coordinates.1');
141-
142-
return isLongitude(y) ? y : x;
143-
}
125+
return get(this.location, 'coordinates.0');
126+
}
144127

145-
return 0;
128+
@computed('location') get latitude() {
129+
return get(this.location, 'coordinates.1');
146130
}
147131

148132
@computed('latitude', 'longitude') get coordinates() {
149133
// eslint-disable-next-line ember/no-get
150134
return [get(this, 'latitude'), get(this, 'longitude')];
151135
}
152136

137+
@computed('latitude', 'longitude') get positionString() {
138+
// eslint-disable-next-line ember/no-get
139+
return `${get(this, 'latitude')} ${get(this, 'longitude')}`;
140+
}
141+
153142
@computed('latitude', 'longitude') get latlng() {
154143
return {
155144
// eslint-disable-next-line ember/no-get
@@ -168,7 +157,11 @@ export default class DriverModel extends Model {
168157
};
169158
}
170159

171-
@computed('coordinates') get hasValidCoordinates() {
160+
@computed('coordinates', 'latitude', 'longitude') get hasValidCoordinates() {
161+
if (this.longitude === 0 || this.latitude === 0) {
162+
return false;
163+
}
164+
172165
return isValidCoordinates(this.coordinates);
173166
}
174167

addon/models/order.js

+90-93
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isBlank } from '@ember/utils';
77
import { getOwner } from '@ember/application';
88
import { format as formatDate, formatDistanceToNow, isValid as isValidDate } from 'date-fns';
99
import isNotModel from '@fleetbase/ember-core/utils/is-not-model';
10+
import shouldNotLoadRelation from '../utils/should-not-load-relation';
1011

1112
export default class OrderModel extends Model {
1213
/** @ids */
@@ -51,6 +52,7 @@ export default class OrderModel extends Model {
5152
/** @attributes */
5253
@attr('string') tracking;
5354
@attr('string') qr_code;
55+
@attr('string') barcode;
5456
@attr('string') pickup_name;
5557
@attr('string') dropoff_name;
5658
@attr('string') driver_name;
@@ -79,6 +81,8 @@ export default class OrderModel extends Model {
7981
@attr('boolean') facilitator_is_vendor;
8082
@attr('raw') meta;
8183
@attr('raw') options;
84+
@attr('raw') tracker_data;
85+
@attr('raw') eta;
8286

8387
/** @dates */
8488
@attr('date') scheduled_at;
@@ -284,6 +288,10 @@ export default class OrderModel extends Model {
284288
return this.payload?.isMultiDrop;
285289
}
286290

291+
@computed('status') get hasActiveStatus() {
292+
return this.status !== 'canceled' && this.status !== 'completed';
293+
}
294+
287295
@computed('has_driver_assigned', 'driver_assigned') get canLoadDriver() {
288296
return this.has_driver_assigned && !this.driver_assigned;
289297
}
@@ -322,7 +330,6 @@ export default class OrderModel extends Model {
322330
}
323331

324332
setProperties(this, { payload });
325-
326333
return this;
327334
}
328335

@@ -353,7 +360,6 @@ export default class OrderModel extends Model {
353360
}
354361

355362
setProperties(this, { meta });
356-
357363
return this;
358364
}
359365

@@ -383,7 +389,6 @@ export default class OrderModel extends Model {
383389
}
384390

385391
setProperties(this, { meta });
386-
387392
return this;
388393
}
389394

@@ -408,7 +413,6 @@ export default class OrderModel extends Model {
408413
}
409414

410415
setProperties(this, { meta: serializedMeta });
411-
412416
return this;
413417
}
414418

@@ -425,165 +429,158 @@ export default class OrderModel extends Model {
425429
options.onBefore(this);
426430
}
427431

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+
}
433436
}
434437

435438
async loadPayload(options = {}) {
436439
const owner = getOwner(this);
437440
const store = owner.lookup('service:store');
438-
439-
if (!this.payload_uuid || !isBlank(this.payload)) {
441+
if (shouldNotLoadRelation(this, 'payload')) {
440442
return;
441443
}
442444

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;
457457
}
458458

459459
async loadCustomer(options = {}) {
460460
const owner = getOwner(this);
461461
const store = owner.lookup('service:store');
462+
if (shouldNotLoadRelation(this, 'customer')) {
463+
return;
464+
}
462465

463466
if (!this.customer_uuid || !isBlank(this.customer)) {
464467
return;
465468
}
466469

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;
471473
}
472474

473475
async loadPurchaseRate(options = {}) {
474476
const owner = getOwner(this);
475477
const store = owner.lookup('service:store');
476-
477-
if (!this.purchase_rate_uuid || !isBlank(this.purchase_rate)) {
478+
if (shouldNotLoadRelation(this, 'purchase_rate')) {
478479
return;
479480
}
480481

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;
485485
}
486486

487487
async loadOrderConfig(options = {}) {
488488
const owner = getOwner(this);
489489
const store = owner.lookup('service:store');
490-
491-
if (!this.order_config_uuid || !isBlank(this.order_config)) {
490+
if (shouldNotLoadRelation(this, 'order_config')) {
492491
return;
493492
}
494493

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;
499497
}
500498

501499
async loadDriver(options = {}) {
502500
const owner = getOwner(this);
503501
const store = owner.lookup('service:store');
504-
505-
if (!this.driver_assigned_uuid || !isBlank(this.driver_assigned)) {
502+
if (shouldNotLoadRelation(this, 'driver_assigned')) {
506503
return;
507504
}
508505

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;
513509
}
514510

515511
async loadTrackingNumber(options = {}) {
516512
const owner = getOwner(this);
517513
const store = owner.lookup('service:store');
518-
519-
if (!this.tracking_number_uuid || !isBlank(this.tracking_number)) {
514+
if (shouldNotLoadRelation(this, 'tracking_number')) {
520515
return;
521516
}
522517

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;
527521
}
528522

529523
async loadTrackingActivity(options = {}) {
530524
const owner = getOwner(this);
531525
const store = owner.lookup('service:store');
532-
533526
if (!this.tracking_number_uuid) {
534527
return;
535528
}
536529

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;
549540
}
550541

551542
async loadComments(options = {}) {
552543
const owner = getOwner(this);
553544
const store = owner.lookup('service:store');
554545

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;
569558
}
570559

571560
async loadFiles(options = {}) {
572561
const owner = getOwner(this);
573562
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);
574582

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;
588585
}
589586
}

0 commit comments

Comments
 (0)