Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Add Inventory Management #686

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions laravel/app/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateInventoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('inventory', function (Blueprint $table) {
$table->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');
}
}
27 changes: 27 additions & 0 deletions laravel/database/seeders/InventorySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Inventory;

class InventorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$products = [
['id' => 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']]);
}
}
}
101 changes: 101 additions & 0 deletions laravel/tests/Unit/InventoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Product;
use App\Inventory;
use Illuminate\Foundation\Testing\RefreshDatabase;

class InventoryTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

// Create test products
Product::create([
'id' => 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' => '[email protected]',
'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' => '[email protected]',
'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);
}
}
Loading