From 80bc96421ae8c6ee6ec5d17cda60574c394fa4aa Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Mon, 6 Nov 2023 19:06:01 +0800 Subject: [PATCH] fix linter and minor issues --- addon/models/fuel-report.js | 1 + addon/models/issue.js | 45 ++++++++++++++++++++++++++++++++ addon/serializers/fleet.js | 2 +- addon/serializers/fuel-report.js | 28 +++----------------- addon/serializers/issue.js | 35 ++++--------------------- 5 files changed, 55 insertions(+), 56 deletions(-) diff --git a/addon/models/fuel-report.js b/addon/models/fuel-report.js index 2da7119..2b85c6c 100644 --- a/addon/models/fuel-report.js +++ b/addon/models/fuel-report.js @@ -18,6 +18,7 @@ export default class FuelReportModel extends Model { @belongsTo('user') reporter; /** @attributes */ + @attr('string') reporter_name; @attr('string') driver_name; @attr('string') vehicle_name; @attr('string') report; diff --git a/addon/models/issue.js b/addon/models/issue.js index 0d2afd3..9b85934 100644 --- a/addon/models/issue.js +++ b/addon/models/issue.js @@ -1,6 +1,8 @@ import Model, { attr, belongsTo } from '@ember-data/model'; import { computed } from '@ember/object'; +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'; export default class IssueModel extends Model { /** @ids */ @@ -19,6 +21,10 @@ export default class IssueModel extends Model { @belongsTo('driver') driver; /** @attributes */ + @attr('string') driver_name; + @attr('string') vehicle_name; + @attr('string') assignee_name; + @attr('string') reporter_name; @attr('string') type; @attr('string') category; @attr('string') report; @@ -75,4 +81,43 @@ export default class IssueModel extends Model { } return formatDate(this.created_at, 'PP'); } + + /** @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); + }); + } + + loadDriver() { + const owner = getOwner(this); + const store = owner.lookup('service:store'); + + return new Promise((resolve, reject) => { + if (isRelationMissing(this, 'driver')) { + return store + .findRecord('driver', this.driver_uuid) + .then((driver) => { + this.driver = driver; + resolve(driver); + }) + .catch(reject); + } + + resolve(this.driver); + }); + } } diff --git a/addon/serializers/fleet.js b/addon/serializers/fleet.js index d5c6dbf..5f03b17 100644 --- a/addon/serializers/fleet.js +++ b/addon/serializers/fleet.js @@ -18,7 +18,7 @@ export default class FleetSerializer extends ApplicationSerializer.extend(Embedd vehicles: { embedded: 'always' }, }; } - + serializeBelongsTo(snapshot, json, relationship) { let key = relationship.key; diff --git a/addon/serializers/fuel-report.js b/addon/serializers/fuel-report.js index 6067413..379d592 100644 --- a/addon/serializers/fuel-report.js +++ b/addon/serializers/fuel-report.js @@ -1,7 +1,5 @@ import ApplicationSerializer from '@fleetbase/ember-core/serializers/application'; import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest'; -import { get } from '@ember/object'; -import { isNone } from '@ember/utils'; export default class FuelReportSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) { /** @@ -25,31 +23,11 @@ export default class FuelReportSerializer extends ApplicationSerializer.extend(E * @param {Object} options * @return {Object} json */ - serialize(snapshot) { + serialize() { const json = super.serialize(...arguments); - // for each relationship make sure the id is set - snapshot.eachRelationship((key, relationship) => { - const { kind } = relationship.meta; - - if (kind === 'belongsTo') { - const relationSnapshot = snapshot.belongsTo(key); - - key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo', 'serialize') : key; - - if (isNone(relationSnapshot)) { - return; - } - - if (key === 'reporter') { - json['reported_by_uuid'] = get(json, `${key}.uuid`); - delete json.reporter; - return; - } - - json[`${key}_uuid`] = relationSnapshot.id; - } - }); + // remove automatically set `reporter_uuid` + delete json.reporter_uuid; return json; } diff --git a/addon/serializers/issue.js b/addon/serializers/issue.js index 4822e27..e9e6910 100644 --- a/addon/serializers/issue.js +++ b/addon/serializers/issue.js @@ -1,7 +1,5 @@ import ApplicationSerializer from '@fleetbase/ember-core/serializers/application'; import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest'; -import { get } from '@ember/object'; -import { isNone } from '@ember/utils'; export default class IssueSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) { /** @@ -26,37 +24,14 @@ export default class IssueSerializer extends ApplicationSerializer.extend(Embedd * @param {Object} options * @return {Object} json */ - serialize(snapshot) { + serialize() { const json = super.serialize(...arguments); - // for each relationship make sure the id is set - snapshot.eachRelationship((key, relationship) => { - const { kind } = relationship.meta; + // remove automatically set `reporter_uuid` + delete json.reporter_uuid; - if (kind === 'belongsTo') { - const relationSnapshot = snapshot.belongsTo(key); - - key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo', 'serialize') : key; - - if (isNone(relationSnapshot)) { - return; - } - - if (key === 'reporter') { - json['reported_by_uuid'] = get(json, `${key}.uuid`); - delete json.reporter; - return; - } - - if (key === 'assignee') { - json['assigned_to_uuid'] = get(json, `${key}.uuid`); - delete json.assignee; - return; - } - - json[`${key}_uuid`] = relationSnapshot.id; - } - }); + // remove automatically set `assignee_uuid` + delete json.assignee_uuid; return json; }