feat(sale): implement confirm and cancel functionality for sales in AdminApp; update routes and tests
This commit is contained in:
@@ -4,12 +4,15 @@ namespace Tests\Feature\Sale;
|
||||
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Purchase\Events\PurchasePaid;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use Database\Seeders\AuthorizationSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -82,6 +85,100 @@ class AdminAppSaleControllerTest extends TestCase
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_an_adminapp_user_can_confirm_a_pending_sale(): void
|
||||
{
|
||||
Event::fake([PurchasePaid::class]);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'total' => '10000.00',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$purchase->id}/confirm")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_PAID);
|
||||
|
||||
$this->assertSame(Purchase::STATUS_PAID, $purchase->fresh()->status);
|
||||
$this->assertDatabaseHas('value_changes', [
|
||||
'trackable_id' => $purchase->id,
|
||||
'attribute' => 'status',
|
||||
'old_value' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'new_value' => Purchase::STATUS_PAID,
|
||||
'user_id' => $admin->id,
|
||||
]);
|
||||
Event::assertDispatchedTimes(PurchasePaid::class, 1);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$purchase->id}/confirm")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_PAID);
|
||||
Event::assertDispatchedTimes(PurchasePaid::class, 1);
|
||||
}
|
||||
|
||||
public function test_adminapp_cancellation_does_not_restore_the_source_cart(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $admin->id,
|
||||
'status' => 'converted',
|
||||
]);
|
||||
$cart->delete();
|
||||
$purchase = Purchase::query()->create([
|
||||
'cart_id' => $cart->id,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'total' => '10000.00',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$purchase->id}/cancel")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertSame(Purchase::STATUS_CANCELLED, $purchase->fresh()->status);
|
||||
$this->assertSoftDeleted('carritos', ['id' => $cart->id]);
|
||||
$this->assertSame('converted', Cart::withTrashed()->findOrFail($cart->id)->status);
|
||||
}
|
||||
|
||||
public function test_an_adminapp_user_cannot_change_a_sale_from_another_tenant(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$otherTenant = $this->createTenant('other');
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
|
||||
$foreignPurchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $otherTenant->codigo,
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'total' => '10000.00',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$foreignPurchase->id}/confirm")
|
||||
->assertNotFound();
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$foreignPurchase->id}/cancel")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_a_paid_sale_cannot_be_cancelled_from_adminapp(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'status' => Purchase::STATUS_PAID,
|
||||
'total' => '10000.00',
|
||||
]);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/sales/{$purchase->id}/cancel")
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('purchase');
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
return Tenant::query()->create([
|
||||
|
||||
Reference in New Issue
Block a user