Skip to content

Commit

Permalink
verifica consistenza dati ordini e unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed May 13, 2024
1 parent 1d4e072 commit 9f985a5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
42 changes: 42 additions & 0 deletions code/tests/Services/OrdersServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 9f985a5

Please sign in to comment.