Skip to content

Commit 8f8e9a1

Browse files
committed
feat: Remove usage of window.Fleetbase, improve several order related functions
1 parent 66edd91 commit 8f8e9a1

File tree

9 files changed

+61
-8
lines changed

9 files changed

+61
-8
lines changed

addon/components/customer/orders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class CustomerOrdersComponent extends Component {
4242

4343
get modalsManager() {
4444
const owner = getOwner(this);
45-
const application = typeof this.universe.getApplicationInstance === 'function' ? this.universe.getApplicationInstance() : window.Fleetbase;
45+
const application = this.universe.getApplicationInstance();
4646
const modalsManager = application ? application.lookup('service:modals-manager') : owner.lookup('service:modals-manager');
4747
return modalsManager;
4848
}

addon/services/leaflet-routing-control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default class LeafletRoutingControlService extends Service {
6767

6868
#initializeRegistry() {
6969
const registry = 'registry:routing-controls';
70-
const application = typeof this.universe?.getApplicationInstance === 'function' ? this.universe.getApplicationInstance() : window.Fleetbase;
70+
const application = this.universe.getApplicationInstance();
7171
if (!application.hasRegistration(registry)) {
7272
application.register(registry, new RoutingControlRegistry(), { instantiate: false });
7373
}

addon/services/movement-tracker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export class EventBuffer {
122122

123123
export default class MovementTrackerService extends Service {
124124
@service socket;
125+
@service universe;
125126
@tracked channels = [];
126127
@tracked buffers = new Map();
127128

@@ -131,7 +132,7 @@ export default class MovementTrackerService extends Service {
131132
}
132133

133134
#getOwner(owner = null) {
134-
return owner ?? window.Fleetbase ?? getOwner(this);
135+
return owner ?? this.universe.getApplicationInstance() ?? getOwner(this);
135136
}
136137

137138
#getBuffer(key, model, opts = {}) {

addon/services/route-optimization.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default class RouteOptimizationService extends Service {
5757

5858
#initializeRegistry() {
5959
const registry = 'registry:route-optimization-engines';
60-
const application = typeof this.universe?.getApplicationInstance === 'function' ? this.universe.getApplicationInstance() : window.Fleetbase;
60+
const application = this.universe.getApplicationInstance();
6161
if (!application.hasRegistration(registry)) {
6262
application.register(registry, new RouteOptimizationRegistry(), { instantiate: false });
6363
}

server/src/Http/Controllers/Internal/v1/LiveController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ function ($q) {
139139

140140
if ($active) {
141141
$query->whereHas('driverAssigned');
142+
$query->whereNotIn('status', ['created', 'completed', 'expired', 'order_canceled', 'canceled', 'pending']);
142143
}
143144

144145
if ($unassigned) {
@@ -166,7 +167,7 @@ public function drivers(Request $request)
166167

167168
return LiveCacheService::remember('drivers', $cacheParams, function () use ($bounds) {
168169
$query = Driver::where(['company_uuid' => session('company')])
169-
->with(['user', 'vehicle', 'currentJob'])
170+
->with(['user', 'vehicle'])
170171
->applyDirectivesForPermissions('fleet-ops list driver');
171172

172173
// Filter out drivers with invalid coordinates

server/src/Http/Filter/OrderFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function active(bool $active = false)
116116
$this->builder->where(
117117
function ($q) {
118118
$q->whereHas('driverAssigned');
119-
$q->whereNotIn('status', ['created', 'canceled', 'order_canceled', 'completed']);
119+
$q->whereNotIn('status', ['created', 'completed', 'expired', 'order_canceled', 'canceled', 'pending']);
120120
}
121121
);
122122
}

server/src/Http/Resources/v1/Order.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function toArray($request): array
8484
'pod_method' => $this->pod_method,
8585
'pod_required' => (bool) data_get($this, 'pod_required', false),
8686
'dispatched' => (bool) data_get($this, 'dispatched', false),
87+
'started' => (bool) data_get($this, 'started', false),
8788
'adhoc' => (bool) data_get($this, 'adhoc', false),
8889
'adhoc_distance' => (int) $this->getAdhocDistance(),
8990
'distance' => (int) $this->distance,

server/src/Models/Driver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ public function vendor(): BelongsTo|Builder
262262

263263
public function currentJob(): BelongsTo|Builder
264264
{
265-
return $this->belongsTo(Order::class)->select(['id', 'uuid', 'public_id', 'payload_uuid', 'driver_assigned_uuid'])->without(['driver']);
265+
return $this->belongsTo(Order::class)->without(['driver']);
266266
}
267267

268268
public function currentOrder(): BelongsTo|Builder
269269
{
270-
return $this->belongsTo(Order::class, 'current_job_uuid')->select(['id', 'uuid', 'public_id', 'payload_uuid', 'driver_assigned_uuid'])->without(['driver']);
270+
return $this->belongsTo(Order::class, 'current_job_uuid')->without(['driver']);
271271
}
272272

273273
public function jobs(): HasMany|Builder

server/src/Observers/OrderObserver.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public function created(Order $order)
1818
$this->invalidateCache($order);
1919
}
2020

21+
/**
22+
* Handle the Order "updating" event.
23+
*
24+
* This event is fired before the order is persisted to the database.
25+
* It is used to mutate attributes as part of the same update operation
26+
* without triggering additional save cycles.
27+
*
28+
* @param Order $order The order being updated
29+
*/
30+
public function updating(Order $order): void
31+
{
32+
$this->ensureOrderStarted($order);
33+
}
34+
2135
/**
2236
* Handle the Order "updated" event.
2337
*
@@ -62,4 +76,40 @@ protected function invalidateCache(?Order $order = null): void
6276
Cache::forget("order:{$order->uuid}:tracker");
6377
}
6478
}
79+
80+
/**
81+
* Detects when an order has just transitioned to the "started" status
82+
* and initializes start-related fields.
83+
*
84+
* This method should be called during the "updating" lifecycle event
85+
* to ensure that the changes are persisted as part of the same database
86+
* update and do not trigger additional observer events.
87+
*
88+
* An order is considered "started" when:
89+
* - The "status" attribute is being changed in the current update
90+
* - The previous status was not "started"
91+
* - The new status is "started"
92+
*
93+
* When these conditions are met, the order's start timestamp and
94+
* started flag are set if they have not already been initialized.
95+
*
96+
* @param Order $order The order being evaluated for a start transition
97+
*/
98+
protected function ensureOrderStarted(Order $order): void
99+
{
100+
if (
101+
$order->isDirty('status')
102+
&& $order->getOriginal('status') === 'dispatched'
103+
&& $order->status === 'started'
104+
) {
105+
// Only set defaults if not explicitly provided
106+
if (is_null($order->started_at)) {
107+
$order->started_at = now();
108+
}
109+
110+
if (!$order->started) {
111+
$order->started = true;
112+
}
113+
}
114+
}
65115
}

0 commit comments

Comments
 (0)