- Introduced a new configuration file for purchase settings, including checkout and payment expiration times. - Added a migration to include a `reservation_status` column in the `compra_items` table, defaulting to 'active' and updating existing records based on purchase status. - Created a migration to add an `expires_at` timestamp to the `compras` table, updating it for existing records based on their status. - Scoped unique indexes in the `carritos` table to only active carts, adding virtual columns for active user and guest tokens. - Implemented a console command to expire overdue purchases and release stock reservations, scheduled to run every minute. - Added tests for Google token exchange and login functionality, ensuring proper cart handling and user authentication. - Updated purchase-related tests to reflect changes in item handling and customer data validation.
50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Cart\Services;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Cart\Models\Cart;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GuestCartMergeService
|
|
{
|
|
public function merge(string $tenantCodigo, User $user, ?string $guestToken): void
|
|
{
|
|
if ($guestToken === null || $guestToken === '') {
|
|
return;
|
|
}
|
|
|
|
DB::transaction(function () use ($tenantCodigo, $user, $guestToken): void {
|
|
$guestCart = Cart::query()
|
|
->where('tenant_codigo', $tenantCodigo)
|
|
->where('guest_token', $guestToken)
|
|
->where('status', 'active')
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if ($guestCart === null || ! $guestCart->items()->exists()) {
|
|
return;
|
|
}
|
|
|
|
$userCart = Cart::query()
|
|
->where('tenant_codigo', $tenantCodigo)
|
|
->where('user_id', $user->getKey())
|
|
->where('status', 'active')
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if ($userCart !== null) {
|
|
$userCart->update([
|
|
'status' => 'converted',
|
|
]);
|
|
$userCart->delete();
|
|
}
|
|
|
|
$guestCart->update([
|
|
'user_id' => $user->getKey(),
|
|
'guest_token' => null,
|
|
]);
|
|
});
|
|
}
|
|
}
|