Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions addon/models/asset-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Model, { attr, belongsTo } from '@ember-data/model';

export default class AssetConnectionModel extends Model {
@attr('string') uuid;
@attr('string') public_id;
@attr('string') company_uuid;
@attr('string') connector_type;
@attr('string') connector_uuid;
@attr('string') connected_type;
@attr('string') connected_uuid;
@attr('string', { defaultValue: 'towing' }) relationship_type;
@attr('number', { defaultValue: 1 }) position;
@attr('string') source;
@attr('string') confidence;
@attr('string') notes;
@attr('raw') meta;
@attr('boolean') active;
@attr('date') connected_at;
@attr('date') disconnected_at;
@attr('date') created_at;
@attr('date') updated_at;

@belongsTo('vehicle', { async: false }) vehicle;
@belongsTo('trailer', { async: false }) trailer;
}
16 changes: 15 additions & 1 deletion addon/models/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ export default class AssetModel extends Model {
@attr('string') odometer_unit;
@attr('string') transmission;
@attr('string') fuel_volume_unit;
@attr('string') fuel_Type;
@attr('string') fuel_type;
@attr('string') ownership_type;
@attr('string') engine_hours;
@attr('string') gvw;
@attr('number') width;
@attr('number') length;
@attr('number') height;
@attr('number') tare_weight;
@attr('number') gvwr;
@attr('number') payload_capacity;
@attr('number') cargo_volume;
@attr('string') currency;
@attr('string') acquisition_cost;
@attr('string') current_value;
@attr('string') insurance_value;
@attr('string') depreciation_rate;
@attr('raw') capacity;
@attr('raw') specs;
@attr('raw') attributes;
Expand All @@ -77,6 +89,8 @@ export default class AssetModel extends Model {

/** @dates */
@attr('date') deleted_at;
@attr('date') purchased_at;
@attr('date') lease_expires_at;
@attr('date') created_at;
@attr('date') updated_at;

Expand Down
11 changes: 11 additions & 0 deletions addon/models/attachable-trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AttachableAssetModel from './attachable-asset';
import { attr } from '@ember-data/model';

/** Concrete polymorphic model for a Trailer attached to a device. */
export default class AttachableTrailerModel extends AttachableAssetModel {
@attr('string') body_type;
@attr('string') coupling_type;
@attr('number') axle_count;
@attr('boolean') refrigerated;
@attr('string') current_vehicle_name;
}
2 changes: 1 addition & 1 deletion addon/models/attachable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { format as formatDate, isValid as isValidDate, formatDistanceToNow } fro

/**
* Abstract base model for resources a device can be attached to.
* Concrete types: attachable-vehicle, attachable-asset.
* Concrete types: attachable-vehicle, attachable-asset, attachable-trailer.
*/
export default class AttachableModel extends Model {
/** @ids */
Expand Down
1 change: 1 addition & 0 deletions addon/models/equipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class EquipmentModel extends Model {
/** @relationships */
@belongsTo('warranty', { async: false }) warranty;
@belongsTo('file', { async: false }) photo;
@belongsTo('attachable', { polymorphic: true, async: false }) equipable;
@hasMany('maintenance', { async: false }) maintenances;
@hasMany('custom-field-value', { async: false }) custom_field_values;

Expand Down
15 changes: 15 additions & 0 deletions addon/models/maintenance-subject-trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import MaintenanceSubjectModel from './maintenance-subject';
import { attr } from '@ember-data/model';

/** Concrete polymorphic model for Trailer maintenance targets. */
export default class MaintenanceSubjectTrailerModel extends MaintenanceSubjectModel {
@attr('string') code;
@attr('string') vin;
@attr('string') plate_number;
@attr('string') make;
@attr('string') model;
@attr('string') year;
@attr('string') body_type;
@attr('number') axle_count;
@attr('string') current_vehicle_name;
}
4 changes: 3 additions & 1 deletion addon/models/maintenance-subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { format as formatDate, isValid as isValidDate, formatDistanceToNow } fro

/**
* Abstract base model for polymorphic maintenance subjects.
* Concrete types: maintenance-subject-vehicle, maintenance-subject-equipment
* Concrete types: maintenance-subject-vehicle, maintenance-subject-trailer,
* maintenance-subject-equipment
*
* The backend stores the type as a PolymorphicType cast string, e.g.:
* 'fleet-ops:vehicle' -> maintenance-subject-vehicle
* 'fleet-ops:trailer' -> maintenance-subject-trailer
* 'fleet-ops:equipment' -> maintenance-subject-equipment
*/
export default class MaintenanceSubjectModel extends Model {
Expand Down
90 changes: 90 additions & 0 deletions addon/models/trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import AssetModel from './asset';
import { attr, belongsTo, hasMany } from '@ember-data/model';
import { computed, get } from '@ember/object';
import { not } from '@ember/object/computed';
import isValidCoordinates from '@fleetbase/ember-core/utils/is-valid-coordinates';

/**
* A first-class towed fleet asset.
*
* Trailer records share the common Asset contract while exposing the
* operational, connection, capacity, and telemetry fields used by Fleet-Ops.
*/
export default class TrailerModel extends AssetModel {
/** @relationships */
@belongsTo('vehicle', { async: false, inverse: null }) current_vehicle;
@belongsTo('asset-connection', { async: false, inverse: null }) current_connection;
@hasMany('asset-connection', { async: false, inverse: null }) connections;
@hasMany('maintenance-schedule', { async: false, inverse: null }) maintenance_schedules;
@hasMany('work-order', { async: false, inverse: null }) work_orders;
@hasMany('position', { async: false, inverse: null }) positions;

/** @classification */
@attr('string', { defaultValue: 'trailer' }) asset_class;
@attr('string') body_type;
@attr('string') coupling_type;
@attr('string') brake_type;

/** @capacity and dimensions */
@attr('number') length;
@attr('number') width;
@attr('number') height;
@attr('number') tare_weight;
@attr('number') gvwr;
@attr('number') payload_capacity;
@attr('number') cargo_volume;
@attr('number') axle_count;
@attr('number') tire_count;
@attr('number') door_count;

/** @specialized trailer capabilities */
@attr('boolean') abs_equipped;
@attr('boolean') ebs_equipped;
@attr('boolean') refrigerated;
@attr('number') temperature_min;
@attr('number') temperature_max;
@attr('number') reefer_engine_hours;

/** @current operational projections */
@attr('boolean') online;
@attr('string') connectivity_status;
@attr('string') movement_status;
@attr('raw') telematics;
@attr('raw') resolved_location;
@attr('string') current_vehicle_name;
@attr('string') current_vehicle_id;
@attr('date') attached_at;
@attr('number') devices_count;
@attr('number') equipment_count;
@attr('date') last_online_at;

@computed('name', 'display_name', 'code', 'plate_number', 'vin', 'serial_number', 'yearMakeModel') get searchString() {
return [this.name, this.display_name, this.code, this.plate_number, this.vin, this.serial_number, this.yearMakeModel].filter(Boolean).join(' ');
}

@computed('location') get longitude() {
return get(this.location, 'coordinates.0');
}

@computed('location') get latitude() {
return get(this.location, 'coordinates.1');
}

@computed('latitude', 'longitude') get coordinates() {
return [get(this, 'latitude'), get(this, 'longitude')];
}

@computed('latitude', 'longitude') get latlng() {
return { lat: get(this, 'latitude'), lng: get(this, 'longitude') };
}

@computed('coordinates', 'latitude', 'longitude') get hasValidCoordinates() {
if (this.longitude === 0 || this.latitude === 0) {
return false;
}

return isValidCoordinates(this.coordinates);
}

@not('hasValidCoordinates') hasInvalidCoordinates;
}
3 changes: 3 additions & 0 deletions addon/models/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default class VehicleModel extends Model {
@belongsTo('driver', { async: false }) driver;
@belongsTo('vendor', { async: false }) vendor;
@hasMany('device', { async: false }) devices;
@hasMany('trailer', { async: false }) trailers;
@hasMany('asset-connection', { async: false }) trailer_connections;
@hasMany('equipment', { async: false }) equipments;
@hasMany('custom-field-value', { async: false }) custom_field_values;

/** @attributes */
Expand Down
11 changes: 11 additions & 0 deletions addon/serializers/asset-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ApplicationSerializer from '@fleetbase/ember-core/serializers/application';
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';

export default class AssetConnectionSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {
get attrs() {
return {
vehicle: { embedded: 'always' },
trailer: { embedded: 'always' },
};
}
}
2 changes: 1 addition & 1 deletion addon/serializers/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class DeviceSerializer extends ApplicationSerializer.extend(Embed
.replace(/^attachable-/, '')
.toLowerCase();

if (!['vehicle', 'asset'].includes(type)) {
if (!['vehicle', 'asset', 'trailer'].includes(type)) {
return undefined;
}

Expand Down
65 changes: 64 additions & 1 deletion addon/serializers/equipment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
import ApplicationSerializer from '@fleetbase/ember-core/serializers/application';
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';
import { isBlank } from '@ember/utils';

export default class EquipmentSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {}
export default class EquipmentSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {
get attrs() {
return {
warranty: { embedded: 'always' },
photo: { embedded: 'always' },
equipable: { embedded: 'always' },
custom_field_values: { embedded: 'always' },
};
}

normalize(model, hash, prop) {
const equipableDomainType = hash?.equipable?.type;

if (hash?.equipable) {
hash.equipable.type = this.equipableModelNameFromType(hash.equipable_type);
}

const normalized = super.normalize(model, hash, prop);

if (equipableDomainType && !this.equipableModelNameFromType(equipableDomainType)) {
const equipable = normalized?.data?.relationships?.equipable?.data;
const included = normalized?.included?.find((resource) => resource.type === equipable?.type && resource.id === equipable?.id);

if (included) {
included.attributes = included.attributes ?? {};
included.attributes.type = equipableDomainType;
}
}

return normalized;
}

serializePolymorphicType(snapshot, json, relationship) {
let key = relationship.key;

if (key !== 'equipable') {
return typeof super.serializePolymorphicType === 'function' ? super.serializePolymorphicType(...arguments) : undefined;
}

const belongsTo = snapshot.belongsTo(key);

if (!isBlank(snapshot.attr(`${key}_type`))) {
return;
}

key = this.keyForAttribute ? this.keyForAttribute(key, 'serialize') : key;
json[`${key}_type`] = belongsTo ? `fleet-ops:${belongsTo.modelName.replace(/^attachable-/, '')}` : null;
}

equipableModelNameFromType(type) {
if (!type || typeof type !== 'string') {
return undefined;
}

const normalized = type
.split('\\')
.pop()
.replace(/^fleet-ops:/, '')
.replace(/^attachable-/, '')
.toLowerCase();
return ['vehicle', 'trailer', 'driver', 'asset'].includes(normalized) ? `attachable-${normalized}` : undefined;
}
}
18 changes: 18 additions & 0 deletions addon/serializers/trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ApplicationSerializer from '@fleetbase/ember-core/serializers/application';
import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';

export default class TrailerSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {
get attrs() {
return {
vendor: { embedded: 'always' },
warranty: { embedded: 'always' },
photo: { embedded: 'always' },
current_vehicle: { embedded: 'always', serialize: false },
current_connection: { embedded: 'always', serialize: false },
connections: { embedded: 'always', serialize: false },
devices: { embedded: 'always', serialize: false },
equipments: { embedded: 'always', serialize: false },
custom_field_values: { embedded: 'always' },
};
}
}
1 change: 1 addition & 0 deletions app/models/asset-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/models/asset-connection';
1 change: 1 addition & 0 deletions app/models/attachable-trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/models/attachable-trailer';
1 change: 1 addition & 0 deletions app/models/maintenance-subject-trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/models/maintenance-subject-trailer';
1 change: 1 addition & 0 deletions app/models/trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/models/trailer';
1 change: 1 addition & 0 deletions app/serializers/asset-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/serializers/asset-connection';
1 change: 1 addition & 0 deletions app/serializers/trailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/fleetops-data/serializers/trailer';
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@babel/eslint-parser": "^7.22.15",
"@babel/plugin-proposal-decorators": "^7.23.2",
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/test-setup": "^3.0.2",
"@glimmer/component": "^1.1.2",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/unit/models/asset-connection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { module, test } from 'qunit';
import { setupTest } from 'dummy/tests/helpers';

module('Unit | Model | asset-connection', function (hooks) {
setupTest(hooks);

test('it represents an effective-dated towing relationship', function (assert) {
const store = this.owner.lookup('service:store');
const connection = store.createRecord('asset-connection', { relationship_type: 'towing', active: true, position: 1 });

assert.strictEqual(connection.relationship_type, 'towing');
assert.true(connection.active);
assert.strictEqual(connection.position, 1);
});
});
23 changes: 23 additions & 0 deletions tests/unit/models/trailer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { module, test } from 'qunit';
import { setupTest } from 'dummy/tests/helpers';

module('Unit | Model | trailer', function (hooks) {
setupTest(hooks);

test('it exposes first-class identity, capacity, connection, and telemetry state', function (assert) {
const store = this.owner.lookup('service:store');
const trailer = store.createRecord('trailer', {
name: 'Reefer 12',
year: '2026',
make: 'Utility',
model: '3000R',
location: { type: 'Point', coordinates: [106.9, 47.9] },
payload_capacity: 20000,
});

assert.strictEqual(trailer.asset_class, 'trailer');
assert.strictEqual(trailer.yearMakeModel, '2026 Utility 3000R');
assert.strictEqual(trailer.payload_capacity, 20000);
assert.deepEqual(trailer.coordinates, [47.9, 106.9]);
});
});
Loading
Loading