From 5d1d9bc9422d9059e597b75c578b28e2f33c37fa Mon Sep 17 00:00:00 2001 From: "sentry-autofix[bot]" <157164994+sentry-autofix[bot]@users.noreply.github.com> Date: Fri, 14 Feb 2025 22:26:46 +0000 Subject: [PATCH] Add Inventory Management --- laravel/app/Inventory.php | 9 ++ ...24_01_15_000001_create_inventory_table.php | 33 ++++++ laravel/database/seeders/InventorySeeder.php | 27 +++++ laravel/tests/Unit/InventoryTest.php | 101 ++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 laravel/database/migrations/2024_01_15_000001_create_inventory_table.php create mode 100644 laravel/database/seeders/InventorySeeder.php create mode 100644 laravel/tests/Unit/InventoryTest.php diff --git a/laravel/app/Inventory.php b/laravel/app/Inventory.php index e9557bb7f..5f942bb3d 100644 --- a/laravel/app/Inventory.php +++ b/laravel/app/Inventory.php @@ -10,4 +10,13 @@ class Inventory extends Model use HasFactory; protected $table = 'inventory'; + protected $fillable = ['product_id', 'quantity']; + + /** + * Get the product that owns the inventory + */ + public function product() + { + return $this->belongsTo(Product::class); + } } diff --git a/laravel/database/migrations/2024_01_15_000001_create_inventory_table.php b/laravel/database/migrations/2024_01_15_000001_create_inventory_table.php new file mode 100644 index 000000000..44eb35cb4 --- /dev/null +++ b/laravel/database/migrations/2024_01_15_000001_create_inventory_table.php @@ -0,0 +1,33 @@ +id(); + $table->foreignId('product_id')->constrained('products')->onDelete('cascade'); + $table->integer('quantity')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('inventory'); + } +} \ No newline at end of file diff --git a/laravel/database/seeders/InventorySeeder.php b/laravel/database/seeders/InventorySeeder.php new file mode 100644 index 000000000..534b2a22a --- /dev/null +++ b/laravel/database/seeders/InventorySeeder.php @@ -0,0 +1,27 @@ + 1, 'quantity' => 10], // wrench + ['id' => 2, 'quantity' => 10], // nails + ['id' => 3, 'quantity' => 10] // hammer + ]; + + foreach ($products as $product) { + Inventory::create(['product_id' => $product['id'], 'quantity' => $product['quantity']]); + } + } +} \ No newline at end of file diff --git a/laravel/tests/Unit/InventoryTest.php b/laravel/tests/Unit/InventoryTest.php new file mode 100644 index 000000000..2d827cbf8 --- /dev/null +++ b/laravel/tests/Unit/InventoryTest.php @@ -0,0 +1,101 @@ + 1, + 'name' => 'Test Wrench', + 'price' => 10.00 + ]); + } + + /** + * Test checkout with insufficient inventory + */ + public function testCheckoutWithInsufficientInventory() + { + // Create cart with quantity more than available inventory + $cart = [ + (object)[ + 'id' => 1, + 'quantity' => 2 // More than available inventory (1) + ] + ]; + + // Set initial inventory + Inventory::create([ + 'product_id' => 1, + 'quantity' => 1 + ]); + + // Make POST request to checkout endpoint + $response = $this->postJson('/checkout', [ + 'cart' => $cart, + 'validate_inventory' => 'true', + 'form' => [ + 'email' => 'test@example.com', + 'firstName' => 'Test', + 'lastName' => 'User' + ] + ]); + + // Assert response status is 400 (Bad Request) + $response->assertStatus(400); + + // Assert error message + $response->assertJson([ + 'error' => 'Insufficient inventory for product: Test Wrench' + ]); + } + + /** + * Test checkout with sufficient inventory + */ + public function testCheckoutWithSufficientInventory() + { + // Create cart with quantity less than or equal to available inventory + $cart = [ + (object)[ + 'id' => 1, + 'quantity' => 1 + ] + ]; + + // Set initial inventory + Inventory::create([ + 'product_id' => 1, + 'quantity' => 1 + ]); + + // Make POST request to checkout endpoint + $response = $this->postJson('/checkout', [ + 'cart' => $cart, + 'validate_inventory' => 'true', + 'form' => [ + 'email' => 'test@example.com', + 'firstName' => 'Test', + 'lastName' => 'User' + ] + ]); + + // Assert response is successful + $response->assertStatus(200); + + // Assert inventory was decremented + $this->assertEquals(0, Inventory::where('product_id', 1)->first()->quantity); + } +} \ No newline at end of file