From 9f985a5368ccc6bd38fb377ebb375eb9c46737af Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Mon, 13 May 2024 18:04:04 +0200 Subject: [PATCH] verifica consistenza dati ordini e unit test --- code/app/Order.php | 25 ++++++++++++++ code/tests/Services/OrdersServiceTest.php | 42 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/code/app/Order.php b/code/app/Order.php index 2085b395..d3fd0f0b 100644 --- a/code/app/Order.php +++ b/code/app/Order.php @@ -316,8 +316,33 @@ private function extractProductPrices($product) return json_encode($row); } + /* + Dato il nuovo elenco di prodotti abilitati nell'ordine, verifica la + consistenza delle relative prenotazioni. + Se ci sono prodotti già prenotati ma che non appaiono nel suddetto + elenco, l'intera applicazione va in errore + */ + private function checkConsistency($new_products) + { + $order_id = $this->id; + + $booked_products = DB::table('booked_products')->select('product_id')->distinct()->join('bookings', function($join) use ($order_id) { + $join->on('booking_id', '=', 'bookings.id')->where('order_id', $order_id); + })->get(); + + $products_ids = $new_products->pluck('id')->toArray(); + + foreach($booked_products as $bp) { + if (in_array($bp->product_id, $products_ids) == false) { + throw new \Exception("Un prodotto già prenotato non è nell'elenco dei nuovi prodotti per l'ordine! Ordine: " . $this->id . ', prodotto: ' . $bp->product_id, 1); + } + } + } + public function syncProducts($products, $update_prices) { + $this->checkConsistency($products); + if ($update_prices) { $data = []; diff --git a/code/tests/Services/OrdersServiceTest.php b/code/tests/Services/OrdersServiceTest.php index 4a80ddb7..df414f71 100644 --- a/code/tests/Services/OrdersServiceTest.php +++ b/code/tests/Services/OrdersServiceTest.php @@ -410,6 +410,48 @@ public function testRemoveProduct() $this->assertNull($booking); } + /* + Preserva prodotto eliminato dal listino + */ + public function testKeepRemovedProduct() + { + $this->actingAs($this->userWithBasePerms); + + $count_products = $this->order->products()->count(); + $target_product_1 = $this->order->products()->orderBy('id', 'asc')->first(); + $target_product_2 = $this->order->products()->orderBy('id', 'asc')->skip(1)->first(); + + $data = [ + 'action' => 'booked', + $target_product_1->id => 2, + $target_product_2->id => 3, + ]; + + $booking = $this->updateAndFetch($data, $this->order, $this->userWithBasePerms, false); + + $this->nextRound(); + + $this->actingAs($this->userReferrer); + app()->make('ProductsService')->destroy($target_product_2->id); + $this->order = app()->make('OrdersService')->show($this->order->id); + $this->assertEquals($count_products, $this->order->products()->count()); + + $this->nextRound(); + + app()->make('OrdersService')->update($this->order->id, [ + 'supplier_id' => $this->order->supplier_id, + 'start' => printableDate($this->order->start), + 'end' => printableDate($this->order->end), + 'shipping' => printableDate($this->order->shipping), + 'status' => 'open', + 'enabled' => $this->order->products->pluck('id')->toArray(), + ]); + + $this->nextRound(); + $this->order = app()->make('OrdersService')->show($this->order->id); + $this->assertEquals($count_products, $this->order->products()->count()); + } + /* Cambio prezzo di un prodotto */