diff --git a/services/nginx/app/objects/order_bookings_o.php b/services/nginx/app/objects/order_bookings_o.php index b5a15ec7..729378e2 100644 --- a/services/nginx/app/objects/order_bookings_o.php +++ b/services/nginx/app/objects/order_bookings_o.php @@ -354,16 +354,14 @@ class order_bookings_o extends db /** * @throws Exception */ - public function completeBooking(int $user_id, string $safety_seal = null): void + public function completeBooking(int $user_id, ?string $safety_seal = null): void { self::requireSelected(); - $orderWasCreatedDuringCompletion = false; if (!$this->order_id->value()) { // Create order, if not already created $this->createOrderBy($user_id); // Add order items, re-calculate the prices to be customer-specific $this->createOrderItemsBy($user_id); - $orderWasCreatedDuringCompletion = true; } if (!$this->containsWashCertificateItem()) { @@ -377,12 +375,14 @@ class order_bookings_o extends db $order->objectChanged(); } - if (!$orderWasCreatedDuringCompletion) { + if ($order->hasWashCertificateAttached()) { return; } $this->attachWashCertificate($user_id, $order->getSafetySealValue()); - $this->sendWashCertificateToCustomer(); + if ($order->hasWashCertificateAttached()) { + $this->sendWashCertificateToCustomer(); + } } /** @@ -465,7 +465,7 @@ class order_bookings_o extends db /** * @throws Exception */ - protected function attachWashCertificate(int $user_id, string $safety_seal = null): void + protected function attachWashCertificate(int $user_id, ?string $safety_seal = null): void { self::requireSelected(); // Check if the order already has a wash certificate attached diff --git a/services/nginx/app/tests/Unit/Orders/OrderBookingsCompletionDedupTest.php b/services/nginx/app/tests/Unit/Orders/OrderBookingsCompletionDedupTest.php index ececa258..810f6172 100644 --- a/services/nginx/app/tests/Unit/Orders/OrderBookingsCompletionDedupTest.php +++ b/services/nginx/app/tests/Unit/Orders/OrderBookingsCompletionDedupTest.php @@ -19,9 +19,16 @@ if (!class_exists('OrderBookingsCompletionOrderDouble')) { $this->completed_at = new object_property('orders', -1, 'completed_at', 'timestamp', false); } + public bool $washCertificateAttached = false; + public function objectChanged(): void { } + + public function hasWashCertificateAttached(): bool + { + return $this->washCertificateAttached; + } } } @@ -65,9 +72,10 @@ if (!class_exists('OrderBookingsCompletionDouble')) { return $this->linkedOrder; } - protected function attachWashCertificate(int $user_id, string $safety_seal = null): void + protected function attachWashCertificate(int $user_id, ?string $safety_seal = null): void { $this->attachCalls++; + $this->linkedOrder->washCertificateAttached = true; } public function sendWashCertificateToCustomer(): void @@ -77,7 +85,7 @@ if (!class_exists('OrderBookingsCompletionDouble')) { } } -it('does not create or email a duplicate wash certificate when a booking is already linked to a pos order', function (): void { +it('attaches and emails a wash certificate when a booking is already linked to a pos order without one', function (): void { $order = new OrderBookingsCompletionOrderDouble(); $booking = new OrderBookingsCompletionDouble($order); $booking->order_id->set(321); @@ -85,6 +93,20 @@ it('does not create or email a duplicate wash certificate when a booking is alre $booking->completeBooking(77, 'LINKED-SEAL'); + expect($order->getSafetySealValue())->toBe('LINKED-SEAL'); + expect($booking->attachCalls)->toBe(1); + expect($booking->sendCalls)->toBe(1); +}); + +it('does not create or email a duplicate wash certificate when a linked pos order already has one', function (): void { + $order = new OrderBookingsCompletionOrderDouble(); + $order->washCertificateAttached = true; + $booking = new OrderBookingsCompletionDouble($order); + $booking->order_id->set(321); + $booking->containsWashCertificate = true; + + $booking->completeBooking(77, 'LINKED-SEAL'); + expect($order->getSafetySealValue())->toBe('LINKED-SEAL'); expect($booking->attachCalls)->toBe(0); expect($booking->sendCalls)->toBe(0);