From 75a116a275250b767ddadc8fc92d31de4b436b9b Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Mon, 11 Mar 2024 15:22:13 +0800 Subject: [PATCH] new methods, hotfixes for fleet-ops data --- addon/models/driver.js | 3 +- addon/models/order-config.js | 3 +- addon/models/order.js | 41 +++++++++++++++------ addon/models/place.js | 3 +- addon/serializers/order-config.js | 4 ++ addon/serializers/order.js | 3 +- app/serializers/order-config.js | 1 + package.json | 2 +- tests/unit/serializers/order-config-test.js | 24 ++++++++++++ 9 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 addon/serializers/order-config.js create mode 100644 app/serializers/order-config.js create mode 100644 tests/unit/serializers/order-config-test.js diff --git a/addon/models/driver.js b/addon/models/driver.js index 5876400..69017fe 100644 --- a/addon/models/driver.js +++ b/addon/models/driver.js @@ -49,7 +49,8 @@ export default class DriverModel extends Model { @attr('string') drivers_license_number; @attr('string', { defaultValue: get(config, 'defaultValues.driverAvatar'), - }) avatar_url; + }) + avatar_url; @attr('string') avatar_value; @attr('point') location; @attr('number') heading; diff --git a/addon/models/order-config.js b/addon/models/order-config.js index 001bc43..12108c5 100644 --- a/addon/models/order-config.js +++ b/addon/models/order-config.js @@ -21,10 +21,11 @@ export default class OrderConfigModel extends Model { @attr('string') key; @attr('string') status; @attr('string') version; + @attr('string') type; @attr('boolean', { defaultValue: false }) core_service; @attr('array') tags; - @attr('object') flow; @attr('array') entities; + @attr('object') flow; @attr('object') meta; /** @computed */ diff --git a/addon/models/order.js b/addon/models/order.js index bfd9ea5..13d4001 100644 --- a/addon/models/order.js +++ b/addon/models/order.js @@ -6,7 +6,6 @@ import { isArray } from '@ember/array'; import { isBlank } from '@ember/utils'; import { getOwner } from '@ember/application'; import { format as formatDate, formatDistanceToNow, isValid as isValidDate } from 'date-fns'; -import groupBy from '@fleetbase/ember-core/utils/macros/group-by'; import isNotModel from '@fleetbase/ember-core/utils/is-not-model'; export default class OrderModel extends Model { @@ -23,12 +22,14 @@ export default class OrderModel extends Model { @attr('string') tracking_number_uuid; @attr('string') driver_assigned_uuid; @attr('string') service_quote_uuid; + @attr('string') order_config_uuid; @attr('string') payload_id; @attr('string') purchase_rate_id; @attr('string') driver_id; /** @relationships */ @belongsTo('company') company; + @belongsTo('order-config') order_config; @belongsTo('customer', { polymorphic: true, async: false }) customer; @belongsTo('facilitator', { polymorphic: true, async: false }) facilitator; @belongsTo('transaction', { async: false }) transaction; @@ -37,10 +38,10 @@ export default class OrderModel extends Model { @belongsTo('route', { async: false }) route; @belongsTo('purchase-rate', { async: false }) purchase_rate; @belongsTo('tracking-number', { async: false }) tracking_number; - @belongsTo('order-config', { async: false }) order_config; @hasMany('tracking-status', { async: false }) tracking_statuses; @hasMany('comment', { async: false }) comments; @hasMany('file', { async: false }) files; + @hasMany('custom-field-value', { async: false }) custom_field_values; /** @aliases */ @alias('driver_assigned') driver; @@ -101,8 +102,6 @@ export default class OrderModel extends Model { @not('hasPayload') missing_payload; @bool('dispatched') isDispatched; @not('dispatched') isNotDispatched; - @equal('type', 'storefront') isStorefrontOrder; - @groupBy('order_config.meta.fields', 'group') groupedMetaFields; @computed('payload.{pickup.name,current_waypoint_uui,waypoints.@each.name}') get pickupName() { @@ -411,6 +410,26 @@ export default class OrderModel extends Model { return this; } + async persistProperty(key, value, options = {}) { + return this.persistProperties({ [key]: value }, options); + } + + async persistProperties(properties = {}, options = {}) { + const owner = getOwner(this); + const fetch = owner.lookup('service:fetch'); + + this.setProperties(properties); + if (typeof options.onBefore === 'function') { + options.onBefore(this); + } + + return fetch.put(`orders/${this.id}`, properties, { normalizeToEmberData: true, normalizeModelType: 'order' }).then((order) => { + if (typeof options.onAfter === 'function') { + options.onAfter(order); + } + }); + } + async loadPayload(options = {}) { const owner = getOwner(this); const store = owner.lookup('service:store'); @@ -465,18 +484,16 @@ export default class OrderModel extends Model { async loadOrderConfig(options = {}) { const owner = getOwner(this); - const fetch = owner.lookup('service:fetch'); + const store = owner.lookup('service:store'); - if (!isBlank(this.order_config)) { + if (!this.order_config_uuid || !isBlank(this.order_config)) { return; } - return fetch - .get('fleet-ops/order-configs/get-installed', { key: this.type ?? 'default', single: true }, { normalizeToEmberData: true, normalizeModelType: 'order-config', ...options }) - .then((orderConfig) => { - this.set('order_config', orderConfig); - return orderConfig; - }); + return store.findRecord('order-config', this.order_config_uuid, options).then((orderConfig) => { + this.set('order_config', orderConfig); + return orderConfig; + }); } async loadDriver(options = {}) { diff --git a/addon/models/place.js b/addon/models/place.js index 3a435fe..38c03b6 100644 --- a/addon/models/place.js +++ b/addon/models/place.js @@ -18,7 +18,8 @@ export default class PlaceModel extends Model { @attr('string') type; @attr('string', { defaultValue: get(config, 'defaultValues.placeAvatar'), - }) avatar_url; + }) + avatar_url; @attr('string') avatar_value; @attr('string') address; @attr('string') address_html; diff --git a/addon/serializers/order-config.js b/addon/serializers/order-config.js new file mode 100644 index 0000000..100bdc3 --- /dev/null +++ b/addon/serializers/order-config.js @@ -0,0 +1,4 @@ +import ApplicationSerializer from '@fleetbase/ember-core/serializers/application'; +import { EmbeddedRecordsMixin } from '@ember-data/serializer/rest'; + +export default class OrderConfigSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {} diff --git a/addon/serializers/order.js b/addon/serializers/order.js index e7cbcd8..2c546e7 100644 --- a/addon/serializers/order.js +++ b/addon/serializers/order.js @@ -10,6 +10,7 @@ export default class OrderSerializer extends ApplicationSerializer.extend(Embedd */ get attrs() { return { + order_config: { embedded: 'always' }, payload: { embedded: 'always' }, driver_assigned: { embedded: 'always' }, facilitator: { embedded: 'always' }, @@ -21,6 +22,7 @@ export default class OrderSerializer extends ApplicationSerializer.extend(Embedd tracking_statuses: { embedded: 'always' }, files: { embedded: 'always' }, comments: { embedded: 'always' }, + custom_field_values: { embedded: 'always' }, }; } @@ -35,7 +37,6 @@ export default class OrderSerializer extends ApplicationSerializer.extend(Embedd const json = super.serialize(snapshot, options); const unshiftAttributes = [ 'order_config', - 'order_config_uuid', 'driver_name', 'tracking', 'total_entities', diff --git a/app/serializers/order-config.js b/app/serializers/order-config.js new file mode 100644 index 0000000..f7fc019 --- /dev/null +++ b/app/serializers/order-config.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/fleetops-data/serializers/order-config'; diff --git a/package.json b/package.json index 03a53aa..896a8c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/fleetops-data", - "version": "0.1.11", + "version": "0.1.12", "description": "Fleetbase Fleet-Ops based models, serializers, transforms, adapters and GeoJson utility functions.", "keywords": [ "fleetbase-data", diff --git a/tests/unit/serializers/order-config-test.js b/tests/unit/serializers/order-config-test.js new file mode 100644 index 0000000..0e025f1 --- /dev/null +++ b/tests/unit/serializers/order-config-test.js @@ -0,0 +1,24 @@ +import { module, test } from 'qunit'; + +import { setupTest } from 'dummy/tests/helpers'; + +module('Unit | Serializer | order config', function (hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function (assert) { + let store = this.owner.lookup('service:store'); + let serializer = store.serializerFor('order-config'); + + assert.ok(serializer); + }); + + test('it serializes records', function (assert) { + let store = this.owner.lookup('service:store'); + let record = store.createRecord('order-config', {}); + + let serializedRecord = record.serialize(); + + assert.ok(serializedRecord); + }); +});