feat(purchase): restore in-review transfer lifecycle
This commit is contained in:
@@ -22,6 +22,11 @@ class ReleaseCheckoutService
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED);
|
||||
}
|
||||
|
||||
public function cancelFromAdmin(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED, allowInReviewCancellation: true);
|
||||
}
|
||||
|
||||
public function expire(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_EXPIRED);
|
||||
@@ -61,9 +66,16 @@ class ReleaseCheckoutService
|
||||
return $expiredCount;
|
||||
}
|
||||
|
||||
private function release(Purchase $purchase, string $targetStatus): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase, $targetStatus): Purchase {
|
||||
private function release(
|
||||
Purchase $purchase,
|
||||
string $targetStatus,
|
||||
bool $allowInReviewCancellation = false,
|
||||
): Purchase {
|
||||
return DB::transaction(function () use (
|
||||
$purchase,
|
||||
$targetStatus,
|
||||
$allowInReviewCancellation,
|
||||
): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($purchase->status === Purchase::STATUS_PAID) {
|
||||
@@ -76,6 +88,14 @@ class ReleaseCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
if (
|
||||
$purchase->status === Purchase::STATUS_IN_REVIEW
|
||||
&& $targetStatus === Purchase::STATUS_CANCELLED
|
||||
&& ! $allowInReviewCancellation
|
||||
) {
|
||||
return $this->createNewCartWithoutCancelling($purchase);
|
||||
}
|
||||
|
||||
if ($this->isAlreadyReleased($purchase)) {
|
||||
return $this->loadPurchase($purchase);
|
||||
}
|
||||
@@ -153,6 +173,33 @@ class ReleaseCheckoutService
|
||||
}
|
||||
}
|
||||
|
||||
private function createNewCartWithoutCancelling(Purchase $purchase): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($purchase->status !== Purchase::STATUS_IN_REVIEW || $purchase->user_id === null) {
|
||||
return $this->loadPurchase($purchase);
|
||||
}
|
||||
|
||||
/** @var Cart|null $sourceCart */
|
||||
$sourceCart = $purchase->cart()->withTrashed()->lockForUpdate()->first();
|
||||
if ($sourceCart !== null && ! $sourceCart->trashed() && $sourceCart->status === 'active') {
|
||||
$sourceCart->update(['status' => 'checkout']);
|
||||
}
|
||||
|
||||
Cart::query()->firstOrCreate([
|
||||
'tenant_codigo' => $purchase->tenant_codigo,
|
||||
'user_id' => $purchase->user_id,
|
||||
'guest_token' => null,
|
||||
'status' => 'active',
|
||||
'origin' => Cart::ORIGIN_USER,
|
||||
]);
|
||||
|
||||
return $this->loadPurchase($purchase);
|
||||
});
|
||||
}
|
||||
|
||||
private function isAlreadyReleased(Purchase $purchase): bool
|
||||
{
|
||||
return in_array($purchase->status, [
|
||||
|
||||
Reference in New Issue
Block a user