@fleetbase/fleetops-data
Version:
Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.
191 lines (149 loc) • 5.9 kB
JavaScript
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { computed } from '@ember/object';
import { notEmpty } from '@ember/object/computed';
import { format as formatDate, isValid as isValidDate, formatDistanceToNow } from 'date-fns';
export default class PayloadModel extends Model {
/** @ids */
public_id;
current_waypoint_uuid;
pickup_uuid;
dropoff_uuid;
return_uuid;
/** @relationships */
pickup;
dropoff;
return;
waypoints;
entities;
/** @attributes */
meta;
cod_amount;
cod_currency;
cod_payment_method;
type;
deleted_at;
created_at;
updated_at;
/** @computed */
hasEntities;
hasWaypoints;
hasPickup;
hasDropoff;
hasReturn;
get isMultiDrop() {
return this.waypoints.length > 0 && !this.pickup_uuid && !this.dropoff_uuid;
}
get firstWaypoint() {
return this.waypoints.firstObject;
}
get lastWaypoint() {
if (1 >= this.waypoints.length) {
return null;
}
return this.waypoints.lastObject;
}
get currentWaypoint() {
return this.waypoints.find((waypoint) => waypoint.id === this.current_waypoint_uuid);
}
get nextStop() {
const { currentWaypoint, firstWaypoint, isMultiDrop, dropoff } = this;
if (isMultiDrop) {
return currentWaypoint ?? firstWaypoint;
}
return dropoff;
}
// eslint-disable-next-line ember/use-brace-expansion
get middleWaypoints() {
const waypoints = this.waypoints;
const middleWaypoints = waypoints.slice(1, waypoints.length - 1);
return middleWaypoints;
}
get entitiesByDestination() {
const groups = [];
// create groups
this.waypoints.forEach((waypoint) => {
const destinationId = waypoint.id;
if (destinationId) {
const entities = this.entities.filter((entity) => entity.destination_uuid === destinationId);
if (entities.length === 0) {
return;
}
const group = {
destinationId,
waypoint,
entities,
};
groups.pushObject(group);
}
});
return groups;
}
get orderWaypoints() {
if (this.waypoints && typeof this.waypoints.toArray === 'function') {
return this.waypoints.toArray();
}
return this.waypoints;
}
get payloadCoordinates() {
let waypoints = [];
let coordinates = [];
waypoints.pushObjects([this.pickup, ...this.waypoints.toArray(), this.dropoff]);
waypoints.forEach((place) => {
if (place && place.get('longitude') && place.get('latitude')) {
if (place.hasInvalidCoordinates) {
return;
}
coordinates.pushObject([place.get('longitude'), place.get('latitude')]);
}
});
return coordinates;
}
get routeWaypoints() {
let waypoints = [];
let coordinates = [];
waypoints.pushObjects([this.pickup, ...this.waypoints.toArray(), this.dropoff]);
waypoints.forEach((place) => {
if (place && place.get('longitude') && place.get('latitude')) {
if (place.hasInvalidCoordinates) {
return;
}
coordinates.pushObject([place.get('latitude'), place.get('longitude')]);
}
});
return coordinates;
}
/** computed dates */
get updatedAgo() {
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
return formatDate(this.updated_at, 'PPP p');
}
get updatedAtShort() {
return formatDate(this.updated_at, 'dd, MMM');
}
get createdAgo() {
return formatDistanceToNow(this.created_at);
}
get createdAt() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'PPP p');
}
get createdAtShort() {
return formatDate(this.created_at, 'dd, MMM');
}
/** @methods */
setWaypoints(_waypoints = []) {
let waypoints = [..._waypoints];
waypoints.forEach((waypoint, index) => waypoint.set('order', index));
this.setProperties({ waypoints });
return this;
}
setEntities(_entities = []) {
let entities = [..._entities];
this.setProperties({ entities });
return this;
}
}