@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
276 lines (234 loc) • 8.41 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 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;
category_uuid;
warranty_uuid;
telematic_uuid;
/** @relationships */
driver;
vendor;
devices;
custom_field_values;
/** @attributes */
photo_url;
name;
description;
driver_name;
vendor_name;
display_name;
avatar_url;
avatar_value;
/** Position */
location;
speed;
heading;
altitude;
/** Basic vehicle info */
make;
model;
model_type;
year;
trim;
color;
transmission;
type;
class;
/** Measurement & fuel */
measurement_system;
fuel_type;
fuel_volume_unit;
/** Body / usage */
body_type;
body_sub_type;
usage_type;
ownership_type;
/** Odometer */
odometer;
odometer_unit;
odometer_at_purchase;
/** Registration / identifiers */
plate_number;
call_sign;
serial_number;
vin;
/** Financing & lifecycle */
financing_status;
loan_number_of_payments;
loan_first_payment;
loan_amount;
estimated_service_life_distance_unit;
estimated_service_life_distance;
estimated_service_life_months;
/** Capacity & dimensions */
cargo_volume;
passenger_volume;
interior_volume;
weight;
width;
length;
height;
towing_capacity;
payload_capacity;
seating_capacity;
ground_clearance;
bed_length;
fuel_capacity;
/** Regulatory / compliance */
emission_standard;
dpf_equipped;
scr_equipped;
gvwr;
gcwr;
/** Engine specs */
engine_number;
engine_model;
engine_make;
engine_family;
engine_configuration;
engine_displacement;
engine_size;
horsepower;
horsepower_rpm;
torque;
torque_rpm;
number_of_cylinders;
cylinder_arrangement;
/** Financial values */
currency;
insurance_value;
depreciation_rate;
current_value;
acquisition_cost;
/** Misc text / meta */
notes;
status;
slug;
online;
vin_data;
specs;
telematics;
meta;
/** @dates */
purchased_at;
lease_expires_at;
deleted_at;
created_at;
updated_at;
/** @computed */
get displayName() {
return this.name ?? this.display_name;
}
get yearMakeModel() {
return [this.year, this.make, this.model].filter(Boolean).join(' ');
}
get searchString() {
return [this.name, this.display_name, this.vin, this.serial_number, this.call_sign, this.plate_number, this.yearMakeModel].filter(Boolean).join(' ');
}
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, 'yyyy-MM-dd HH:mm');
}
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, 'yyyy-MM-dd HH:mm');
}
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 */
async loadDriver() {
if (!this.driver_uuid) return;
const owner = getOwner(this);
const store = owner.lookup('service:store');
const driver = await store.findRecord('driver', this.driver_uuid);
this.driver = driver;
return driver;
}
async loadDevices() {
const owner = getOwner(this);
const store = owner.lookup('service:store');
const devices = await store.query('device', { vehicle_uuid: this.id });
this.devices = devices;
return devices;
}
}