commit1dc4e29c69Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 13:58:03 2026 -0300 refactor(reservations): unify expiration command commit093e894cc3Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 13:48:09 2026 -0300 feat(cart): expire abandoned stock reservations commitfdf0f3328fAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:53:12 2026 -0300 refactor(stock): implement expiration for stock reservations and add configuration commit8d6bcdcc43Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:38:21 2026 -0300 refactor(cart): invalidate payment on actual changes commit3206e293ebAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:24:48 2026 -0300 refactor(cart): own checkout item editing commitaed99bd05eAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:14:57 2026 -0300 refactor(checkout): remove legacy purchase item reservations commitf1649e0e4bAuthor: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:06:29 2026 -0300 refactor(checkout): materialize purchase items on confirmation commite6c4b40a37Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 19 12:06:19 2026 -0300 feat(inventory): add traceable cart stock reservations
54 lines
1.6 KiB
PHP
54 lines
1.6 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->items()
|
|
->orderBy('id')
|
|
->pluck('id')
|
|
->each(fn (int $itemId) => $userCart->removeItem($itemId));
|
|
$userCart->update([
|
|
'status' => 'converted',
|
|
]);
|
|
$userCart->delete();
|
|
}
|
|
|
|
$guestCart->update([
|
|
'user_id' => $user->getKey(),
|
|
'guest_token' => null,
|
|
]);
|
|
});
|
|
}
|
|
}
|