From 8400f3d6f9a0d7e0b66d7c9e71c64298f91c3a10 Mon Sep 17 00:00:00 2001 From: Prem Kumar Sharma Date: Tue, 2 Jun 2026 16:35:26 +0530 Subject: [PATCH] fix: emit order completed event after status update Move Order::complete() so it applies the completed activity before firing the completion event. This keeps lifecycle event consumers from seeing stale order state while preserving the existing behavior that complete() emits the order completed event even when the completed activity was already applied by the caller. --- server/src/Models/Order.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/src/Models/Order.php b/server/src/Models/Order.php index e0269bc2..a71b8e13 100644 --- a/server/src/Models/Order.php +++ b/server/src/Models/Order.php @@ -1379,8 +1379,9 @@ public function notifyCompleted() * * The process involves the following steps: * 1. Creating a new Activity instance with the 'completed' code and relevant details. - * 2. Notifying that the order has been completed via `notifyCompleted`. - * 3. Updating the order's activity with the new 'completed' activity through `updateActivity`. + * 2. Updating the order's activity with the new 'completed' activity through `updateActivity`, + * which applies and persists the 'completed' status. + * 3. Notifying that the order has been completed via `notifyCompleted`. * * @param Proof|null $proof Optional. Additional proof or details for the activity update. * @@ -1388,15 +1389,15 @@ public function notifyCompleted() */ public function complete(?Proof $proof = null): self { - $this->notifyCompleted(); - $doesntHaveCompletedActivity = TrackingStatus::where(['tracking_number_uuid' => $this->tracking_number_uuid, 'code' => 'COMPLETED'])->doesntExist(); if ($doesntHaveCompletedActivity) { $activity = $this->config()->getCompletedActivity(); - return $this->updateActivity($activity, $proof); + $this->updateActivity($activity, $proof); } + $this->notifyCompleted(); + return $this; }