Skip to content

Commit

Permalink
400. feature: apply pending promocode
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed May 15, 2024
1 parent 565169b commit 01e353b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/app/Filament/Resources/Promo/SaleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static function form(Form $form): Form
->prohibits('sale_fix')
->requiredWithout('sale_fix'),
Forms\Components\TextInput::make('sale_fix')
->disabled()
->helperText('Функционал в разработке')
->label('Фиксированный скидка')
->placeholder('В копейках')
->numeric()
Expand Down
27 changes: 27 additions & 0 deletions src/app/Listeners/Promo/ApplyPendingPromocode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Listeners\Promo;

use App\Services\SaleService;
use Illuminate\Auth\Events\Login;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class ApplyPendingPromocode
{
/**
* Create the event listener.
*/
public function __construct(private readonly SaleService $saleService)
{
//
}

/**
* Handle the event.
*/
public function handle(Login $event): void
{
$this->saleService->applyPendingPromocode($event->user);
}
}
2 changes: 2 additions & 0 deletions src/app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Listeners\MergeFavorites;
use App\Listeners\Order\CreateInstallment;
use App\Listeners\Order\DistributeOrder;
use App\Listeners\Promo\ApplyPendingPromocode;
use App\Listeners\SaveDevice;
use App\Listeners\SendOrderInformationNotification;
use App\Listeners\SyncOrderHistory;
Expand Down Expand Up @@ -45,6 +46,7 @@ class EventServiceProvider extends ServiceProvider
SaveDevice::class,
MergeFavorites::class,
SyncOrderHistory::class,
ApplyPendingPromocode::class,
// MergeCart::class,
],
Analytics\ProductView::class => [
Expand Down
41 changes: 37 additions & 4 deletions src/app/Services/SaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class SaleService
*/
const REVIEW_SALE_KEY = 'review_sale';

/**
* The key for cookie with pending promocode.
*/
const COOKIE_KEY_FOR_PENDING_PROMOCODE = 'pending_promocode';

/**
* List of discounts
*/
Expand Down Expand Up @@ -513,22 +518,50 @@ public function applyToOrder(Cart $cart, OrderData $orderData)
*/
public function applyPromocode(string $promocodeCode): void
{
/** @var Promocode */
$promocode = Promocode::query()->firstWhere('code', $this->preparePromocodeCode($promocodeCode));
if (!$promocode) {
if (!$promocode = $this->getPromocodeByCode($promocodeCode)) {
return;
}

$user = auth()->user();
if (!$user instanceof User) {
Cookie::queue('pending_promocode', $promocode->code, 60 * 24 * 7);
Cookie::queue(self::COOKIE_KEY_FOR_PENDING_PROMOCODE, $promocode->code, 60 * 24 * 7);

return;
}

$this->applyPromocodeToUser($promocode, $user);
}

/**
* Apply a pending promocode to the user's cart.
*/
public function applyPendingPromocode($user): void
{
if (!$user instanceof User) {
return;
}

$pendingPromocodeCode = Cookie::get(self::COOKIE_KEY_FOR_PENDING_PROMOCODE);
if (!$pendingPromocodeCode) {
return;
}
Cookie::queue(Cookie::forget(self::COOKIE_KEY_FOR_PENDING_PROMOCODE));

if (!$promocode = $this->getPromocodeByCode($pendingPromocodeCode)) {
return;
}

$this->applyPromocodeToUser($promocode, $user);
}

/**
* Retrieve a promocode by its code.
*/
private function getPromocodeByCode(string $code): ?Promocode
{
return Promocode::query()->firstWhere('code', $this->preparePromocodeCode($code));
}

/**
* Prepare the promocode code.
*/
Expand Down

0 comments on commit 01e353b

Please sign in to comment.