Skip to content

Commit

Permalink
fix linter and minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Nov 6, 2023
1 parent 132bad0 commit 80bc964
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
1 change: 1 addition & 0 deletions addon/models/fuel-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions addon/models/issue.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
}
}
2 changes: 1 addition & 1 deletion addon/serializers/fleet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class FleetSerializer extends ApplicationSerializer.extend(Embedd
vehicles: { embedded: 'always' },
};
}

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

Expand Down
28 changes: 3 additions & 25 deletions addon/serializers/fuel-report.js
Original file line number Diff line number Diff line change
@@ -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) {
/**
Expand All @@ -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;
}
Expand Down
35 changes: 5 additions & 30 deletions addon/serializers/issue.js
Original file line number Diff line number Diff line change
@@ -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) {
/**
Expand All @@ -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;
}
Expand Down

0 comments on commit 80bc964

Please sign in to comment.