@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
190 lines (163 loc) • 5.81 kB
JavaScript
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { get, computed } from '@ember/object';
import { not } from '@ember/object/computed';
import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns';
import { getOwner } from '@ember/application';
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 VehicleModel extends Model {
/** @ids */
uuid;
public_id;
internal_id;
company_uuid;
photo_uuid;
vendor_uuid;
online;
/** @relationships */
driver;
vendor;
devices;
/** @attributes */
photo_url;
driver_name;
vendor_name;
display_name;
avatar_url;
avatar_value;
location;
make;
model;
year;
trim;
type;
plate_number;
vin;
vin_data;
model_data;
telematics;
meta;
status;
slug;
/** @dates */
deleted_at;
created_at;
updated_at;
/** @computed */
get displayName() {
const nameSegments = [this.year, this.make, this.model, this.trim, this.plate_number, this.internal_id];
return nameSegments.filter(Boolean).join(' ').trim();
}
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;
/** @methods */
loadDriver() {
const owner = getOwner(this);
const store = owner.lookup('service:store');
return new Promise((resolve) => {
if (isRelationMissing(this, 'driver')) {
return store
.findRecord('driver', this.driver_uuid)
.then((driver) => {
this.driver = driver;
resolve(driver);
})
.catch(() => {
resolve(null);
});
}
resolve(this.driver);
});
}
loadDevices() {
const owner = getOwner(this);
const store = owner.lookup('service:store');
return new Promise((resolve, reject) => {
return store
.findRecord('vehicle-device', { vehicle_uuid: this.id })
.then((devices) => {
this.vehicle_devices = devices;
resolve(devices);
})
.catch(reject);
});
}
}