feat(food): add endpoint to update historical stock and separate current and historical food variants

This commit is contained in:
2026-09-15 15:03:41 -03:00
parent 9fa4da7de2
commit ff43ba32f3
9 changed files with 426 additions and 27 deletions

View File

@@ -8,12 +8,14 @@ use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Services\EventService;
use App\Domains\Menu\Models\Menu;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
@@ -32,6 +34,13 @@ class FoodControllerTest extends TestCase
]);
}
protected function tearDown(): void
{
Carbon::setTestNow();
parent::tearDown();
}
public function test_authentication_is_required(): void
{
$this->postJson('/api/v1/adminapp/tenant/foods', ['variants' => []])
@@ -142,6 +151,101 @@ class FoodControllerTest extends TestCase
]);
}
public function test_it_separates_current_and_historical_food_variants(): void
{
Carbon::setTestNow('2026-09-01 12:00:00');
[$tenant, $rescheduledDate, $suspendedDate] = $this->configuredTenant();
$completedDate = $tenant->eventDates()->create([
'date' => '2026-10-11',
'time_start' => '00:00',
'time_end' => '23:59',
]);
$activeDate = $tenant->eventDates()->create([
'date' => '2026-10-21',
'time_start' => '00:00',
'time_end' => '23:59',
]);
$admin = $this->createAdminAppUser($tenant);
Sanctum::actingAs($admin);
$this->postJson('/api/v1/adminapp/tenant/foods', [
'variants' => [
$this->variantPayload($rescheduledDate->id, 'Almuerzo', 'Comedor', 100, 10000),
$this->variantPayload($suspendedDate->id, 'Cena', 'Vianda', 90, 9000),
$this->variantPayload($completedDate->id, 'Cena', 'Comedor', 80, 8000),
$this->variantPayload($activeDate->id, 'Almuerzo', 'Vianda', 70, 7000),
],
])->assertOk();
$eventService = app(EventService::class);
$eventService->rescheduleDateForTenant(
$tenant,
$rescheduledDate,
['date' => '2026-10-20'],
$admin,
);
$eventService->suspendDateForTenant($tenant, $suspendedDate, $admin);
Carbon::setTestNow('2026-10-15 12:00:00');
$response = $this->getJson('/api/v1/adminapp/tenant/foods')->assertOk();
$response->assertJsonCount(2, 'data.variants')->assertJsonCount(3, 'data.history');
$history = collect($response->json('data.history'));
$rescheduled = $history->firstWhere('status', 'rescheduled');
$this->assertSame('REPROGRAMADA', $rescheduled['status_text']);
$this->assertSame('2026-10-09', $rescheduled['event_date']);
$this->assertSame('2026-10-20', $rescheduled['replacement_event_date']);
$this->assertCount(1, $rescheduled['variants']);
$suspended = $history->firstWhere('status', 'suspended');
$this->assertSame('CANCELADA', $suspended['status_text']);
$this->assertNull($suspended['replacement_event_date']);
$completed = $history->firstWhere('status', 'completed');
$this->assertSame('FINALIZADA', $completed['status_text']);
$this->assertSame('2026-10-11', $completed['event_date']);
$this->assertNull($completed['replacement_event_date']);
}
public function test_it_updates_only_the_stock_of_historical_food_variants(): void
{
[$tenant, $historicalDate, $activeDate] = $this->configuredTenant();
$admin = $this->createAdminAppUser($tenant);
Sanctum::actingAs($admin);
$created = $this->postJson('/api/v1/adminapp/tenant/foods', [
'variants' => [
$this->variantPayload($historicalDate->id, 'Almuerzo', 'Comedor', 100, 10000),
$this->variantPayload($activeDate->id, 'Cena', 'Vianda', 80, 8000),
],
])->assertOk();
$historicalVariantId = $created->json('data.variants.0.id');
$activeVariantId = $created->json('data.variants.1.id');
app(EventService::class)->suspendDateForTenant($tenant, $historicalDate, $admin);
$this->patchJson('/api/v1/adminapp/tenant/foods/history-stock', [
'variants' => [['id' => $historicalVariantId, 'stock' => 45]],
])
->assertOk()
->assertJsonPath('data.history.0.variants.0.id', $historicalVariantId)
->assertJsonPath('data.history.0.variants.0.stock', 45);
$historicalInventoryId = Variant::query()->findOrFail($historicalVariantId)->inventory_id;
$this->assertDatabaseHas('inventories', [
'id' => $historicalInventoryId,
'real_stock' => 45,
]);
$this->patchJson('/api/v1/adminapp/tenant/foods/history-stock', [
'variants' => [['id' => $activeVariantId, 'stock' => 20]],
])
->assertUnprocessable()
->assertJsonValidationErrors(['variants.0.id']);
}
public function test_it_deletes_food_records_and_removes_the_empty_product(): void
{
[$tenant, $firstDate, $secondDate] = $this->configuredTenant();

View File

@@ -65,6 +65,24 @@ class EventModelsTest extends TestCase
}
}
public function test_an_overnight_event_finishes_on_the_following_day(): void
{
Carbon::setTestNow('2026-10-10 01:00:00');
try {
$eventDate = new EventDate([
'date' => '2026-10-09',
'time_start' => '20:00:00',
'time_end' => '02:00:00',
]);
$this->assertSame('2026-10-10 02:00:00', $eventDate->endsAt()->format('Y-m-d H:i:s'));
$this->assertSame(EventDateStatus::InProgress, $eventDate->status);
} finally {
Carbon::setTestNow();
}
}
public function test_tenant_has_many_event_dates(): void
{
$tenant = new Tenant;