feat(purchase): add expiration time calculations to PurchaseResource and implement related tests
This commit is contained in:
@@ -17,6 +17,8 @@ class PurchaseResource extends JsonResource
|
|||||||
*/
|
*/
|
||||||
public function toArray(Request $request): array
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
|
$serverTime = now();
|
||||||
|
$expiresAt = $this->stockReservation?->expires_at;
|
||||||
$items = $this->resource->relationLoaded('items')
|
$items = $this->resource->relationLoaded('items')
|
||||||
? $this->resource->getRelation('items')
|
? $this->resource->getRelation('items')
|
||||||
: collect();
|
: collect();
|
||||||
@@ -48,7 +50,11 @@ class PurchaseResource extends JsonResource
|
|||||||
'created_at' => $this->created_at,
|
'created_at' => $this->created_at,
|
||||||
'status' => $this->status,
|
'status' => $this->status,
|
||||||
'payment_method' => $this->payment_method,
|
'payment_method' => $this->payment_method,
|
||||||
'expires_at' => $this->stockReservation?->expires_at,
|
'expires_at' => $expiresAt,
|
||||||
|
'expires_in_seconds' => $expiresAt === null
|
||||||
|
? null
|
||||||
|
: max(0, $expiresAt->getTimestamp() - $serverTime->getTimestamp()),
|
||||||
|
'server_time' => $serverTime,
|
||||||
'dni' => $this->dni,
|
'dni' => $this->dni,
|
||||||
'transfer_payer_dni' => $this->transfer_payer_dni,
|
'transfer_payer_dni' => $this->transfer_payer_dni,
|
||||||
'telefono' => $this->telefono,
|
'telefono' => $this->telefono,
|
||||||
|
|||||||
69
tests/Unit/Purchase/PurchaseResourceTest.php
Normal file
69
tests/Unit/Purchase/PurchaseResourceTest.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Purchase;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Models\StockReservation;
|
||||||
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
use App\Domains\Purchase\Resources\PurchaseResource;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class PurchaseResourceTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_it_exposes_the_remaining_checkout_time_using_the_server_clock(): void
|
||||||
|
{
|
||||||
|
$now = now()->startOfSecond();
|
||||||
|
$this->travelTo($now);
|
||||||
|
$expiresAt = $now->copy()->addMinutes(12);
|
||||||
|
|
||||||
|
$resource = $this->resourceFor(Purchase::STATUS_PENDING_PAYMENT, $expiresAt);
|
||||||
|
|
||||||
|
$this->assertTrue($expiresAt->equalTo($resource['expires_at']));
|
||||||
|
$this->assertSame(720, $resource['expires_in_seconds']);
|
||||||
|
$this->assertTrue($now->equalTo($resource['server_time']));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_clamps_an_overdue_checkout_to_zero_seconds(): void
|
||||||
|
{
|
||||||
|
$now = now()->startOfSecond();
|
||||||
|
$this->travelTo($now);
|
||||||
|
|
||||||
|
$resource = $this->resourceFor(
|
||||||
|
Purchase::STATUS_CREATED,
|
||||||
|
$now->copy()->subSecond(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(0, $resource['expires_in_seconds']);
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_exposes_null_expiration_after_the_purchase_enters_review(): void
|
||||||
|
{
|
||||||
|
$resource = $this->resourceFor(
|
||||||
|
Purchase::STATUS_IN_REVIEW,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNull($resource['expires_at']);
|
||||||
|
$this->assertNull($resource['expires_in_seconds']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
private function resourceFor(string $status, ?Carbon $expiresAt): array
|
||||||
|
{
|
||||||
|
$purchase = (new Purchase)->forceFill([
|
||||||
|
'status' => $status,
|
||||||
|
'total' => '0.00',
|
||||||
|
]);
|
||||||
|
$purchase->setRelation('stockReservation', (new StockReservation)->forceFill([
|
||||||
|
'status' => StockReservation::STATUS_ACTIVE,
|
||||||
|
'expires_at' => $expiresAt,
|
||||||
|
]));
|
||||||
|
|
||||||
|
return (new PurchaseResource($purchase))->toArray(Request::create('/'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user