@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
213 lines (182 loc) • 6.41 kB
JavaScript
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { computed, get } from '@ember/object';
import { not } from '@ember/object/computed';
import { getOwner } from '@ember/application';
import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns';
import isRelationMissing from '@fleetbase/ember-core/utils/is-relation-missing';
import isValidCoordinates from '@fleetbase/ember-core/utils/is-valid-coordinates';
import config from 'ember-get-config';
export default class DriverModel extends Model {
/** @ids */
public_id;
company_uuid;
user_uuid;
vehicle_uuid;
vendor_uuid;
current_job_uuid;
photo_uuid;
vehicle_id;
vendor_id;
current_job_id;
internal_id;
/** @relationships */
user;
fleets;
devices;
jobs;
vehicle;
current_job;
vendor;
/** @attributes */
name;
phone;
email;
photo_url;
vehicle_name;
vehicle_avatar;
vendor_name;
drivers_license_number;
avatar_url;
avatar_value;
location;
heading;
country;
city;
status;
online;
/** @dates */
deleted_at;
created_at;
updated_at;
/** @computed */
get photoUrl() {
if (!this.photo_url) {
return get(config, 'defaultValues.driverImage');
}
return this.photo_url;
}
get displayName() {
if (!this.name) {
return this.public_id;
}
return this.name;
}
get updatedAgo() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDate(this.updated_at, 'PPP p');
}
get updatedAtShort() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDate(this.updated_at, 'dd, MMM');
}
get createdAgo() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDistanceToNow(this.created_at);
}
get createdAt() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'PPP p');
}
get createdAtShort() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'dd, MMM');
}
get longitude() {
return get(this.location, 'coordinates.0');
}
get latitude() {
return get(this.location, 'coordinates.1');
}
get coordinates() {
// eslint-disable-next-line ember/no-get
return [get(this, 'latitude'), get(this, 'longitude')];
}
get positionString() {
// eslint-disable-next-line ember/no-get
return `${get(this, 'latitude')} ${get(this, 'longitude')}`;
}
get latlng() {
return {
// eslint-disable-next-line ember/no-get
lat: get(this, 'latitude'),
// eslint-disable-next-line ember/no-get
lng: get(this, 'longitude'),
};
}
get latitudelongitude() {
return {
// eslint-disable-next-line ember/no-get
latitude: get(this, 'latitude'),
// eslint-disable-next-line ember/no-get
longitude: get(this, 'longitude'),
};
}
get hasValidCoordinates() {
if (this.longitude === 0 || this.latitude === 0) {
return false;
}
return isValidCoordinates(this.coordinates);
}
hasInvalidCoordinates;
get activeJobs() {
return this.jobs.filter((order) => !['completed', 'canceled'].includes(order.status));
}
/** @methods */
loadVehicle() {
const owner = getOwner(this);
const store = owner.lookup('service:store');
return new Promise((resolve, reject) => {
if (isRelationMissing(this, 'vehicle')) {
return store
.findRecord('vehicle', this.vehicle_uuid)
.then((vehicle) => {
this.vehicle = vehicle;
resolve(vehicle);
})
.catch(reject);
}
resolve(this.vehicle);
});
}
loadVendor() {
const owner = getOwner(this);
const store = owner.lookup('service:store');
return new Promise((resolve, reject) => {
if (isRelationMissing(this, 'vendor')) {
return store
.findRecord('vendor', this.vendor_uuid)
.then((vendor) => {
this.vendor = vendor;
resolve(vendor);
})
.catch(reject);
}
resolve(this.vendor);
});
}
}