-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
028fda7
commit 8433f1a
Showing
14 changed files
with
1,565 additions
and
4,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Data\BlackFridayRewardData; | ||
use App\Enums\BlackFridayRewardType; | ||
use App\Models\User; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class DetermineBlackFridayRewardAction | ||
{ | ||
public function execute( | ||
User $user, | ||
int $day, | ||
?BlackFridayRewardType $predefinedRewardType = null, | ||
): BlackFridayRewardData { | ||
$redeemedReward = BlackFridayRewardData::forUserAndDay($user, $day); | ||
|
||
if($redeemedReward) { | ||
return $redeemedReward; | ||
} | ||
|
||
$specialRewards = DB::table('bf24_rewards') | ||
->where('available_at', '<=', now()) | ||
->where('day', $day) | ||
->whereNotExists(fn (Builder $query) => $query | ||
->select(DB::raw(1)) | ||
->from('bf24_redeemed_rewards') | ||
->whereColumn('bf24_rewards.id', 'bf24_redeemed_rewards.reward_id')) | ||
->get(); | ||
|
||
if ($specialRewards->isNotEmpty()) { | ||
$reward = $specialRewards->random(); | ||
|
||
return $this->redeemReward( | ||
$user, | ||
$day, | ||
BlackFridayRewardType::from($reward->type), | ||
rewardId: $reward->id | ||
); | ||
} | ||
|
||
$rewardTypes = [ | ||
BlackFridayRewardType::Flare50Off, | ||
BlackFridayRewardType::Mailcoach50Off, | ||
BlackFridayRewardType::MerchDiscount, | ||
BlackFridayRewardType::NextPurchaseDiscount, | ||
]; | ||
|
||
/** @var BlackFridayRewardType $rewardType */ | ||
$rewardType = $predefinedRewardType ?? Arr::random($rewardTypes); | ||
|
||
if (! $rewardType->requiresSaasCode()) { | ||
return $this->redeemNonSaasCodeReward($user, $day, $rewardType); | ||
} | ||
|
||
$code = $this->getSaasCode($rewardType); | ||
|
||
if ($code) { | ||
return $this->redeemReward($user, $day, $rewardType, code: $code); | ||
} | ||
|
||
return $this->redeemNonSaasCodeReward( | ||
$user, | ||
$day, | ||
Arr::random([BlackFridayRewardType::NextPurchaseDiscount, BlackFridayRewardType::MerchDiscount]) | ||
); | ||
} | ||
|
||
protected function redeemReward( | ||
User $user, | ||
int $day, | ||
BlackFridayRewardType $rewardType, | ||
?int $rewardId = null, | ||
?string $code = null | ||
): BlackFridayRewardData { | ||
DB::table('bf24_redeemed_rewards')->insert([ | ||
'user_id' => $user->id, | ||
'day' => $day, | ||
'type' => $rewardType, | ||
'reward_id' => $rewardId, | ||
'code' => $code, | ||
'created_at' => now(), | ||
'updated_at' => now(), | ||
]); | ||
|
||
return new BlackFridayRewardData(type: $rewardType, code: $code); | ||
} | ||
|
||
protected function redeemNonSaasCodeReward( | ||
User $user, | ||
int $day, | ||
BlackFridayRewardType $rewardType, | ||
): BlackFridayRewardData { | ||
$code = match ($rewardType) { | ||
BlackFridayRewardType::MerchDiscount => config('black-friday.merch_discount_code'), | ||
BlackFridayRewardType::NextPurchaseDiscount => config('black-friday.next_purchase_discount_code'), | ||
default => throw new \Exception('Invalid reward type'), | ||
}; | ||
|
||
return $this->redeemReward($user, $day, $rewardType, code: $code); | ||
} | ||
|
||
protected function getSaasCode(BlackFridayRewardType $rewardType): ?string | ||
{ | ||
$code = DB::table('bf24_codes_pool') | ||
->select('id', 'code') | ||
->where('type', $rewardType) | ||
->first(); | ||
|
||
if ($code === null) { | ||
return null; | ||
} | ||
|
||
DB::table('bf24_codes_pool') | ||
->where('id', $code->id) | ||
->delete(); | ||
|
||
return $code->code; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Data; | ||
|
||
use App\Enums\BlackFridayRewardType; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
use Livewire\Wireable; | ||
|
||
class BlackFridayRewardData implements Wireable | ||
{ | ||
public function __construct( | ||
public BlackFridayRewardType $type, | ||
public ?string $code = null, | ||
public bool $enteredRaffle = false, | ||
) { | ||
} | ||
|
||
public static function forUserAndDay(User $user, int $day): ?self | ||
{ | ||
$redeem = DB::table('bf24_redeemed_rewards') | ||
->where('user_id', $user->id) | ||
->where('day', $day) | ||
->first(); | ||
|
||
if (! $redeem) { | ||
return null; | ||
} | ||
|
||
return new BlackFridayRewardData( | ||
type: BlackFridayRewardType::from($redeem->type), | ||
code: $redeem->code, | ||
enteredRaffle: $redeem->entered_raffle, | ||
); | ||
} | ||
|
||
public function toLivewire(): array | ||
{ | ||
return [ | ||
'type' => $this->type->value, | ||
'code' => $this->code, | ||
'enteredRaffle' => $this->enteredRaffle, | ||
]; | ||
} | ||
|
||
public static function fromLivewire($value): self | ||
{ | ||
return new BlackFridayRewardData( | ||
type: BlackFridayRewardType::from($value['type']), | ||
code: $value['code'], | ||
enteredRaffle: $value['enteredRaffle'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
enum BlackFridayRewardType: string | ||
{ | ||
case NextPurchaseDiscount = 'next_purchase_discount'; | ||
case MerchDiscount = 'merch_discount'; | ||
case Mailcoach50Off = '50_off_mailcoach'; | ||
case Flare50Off = '50_off_flare'; | ||
case FreeMerch = 'free_merch'; | ||
case FreeRay = 'free_ray'; | ||
|
||
public function requiresSaasCode(): bool | ||
{ | ||
return in_array($this, [ | ||
self::Mailcoach50Off, | ||
self::Flare50Off, | ||
]); | ||
} | ||
|
||
public function wonLabel(): string | ||
{ | ||
return match ($this) { | ||
self::NextPurchaseDiscount => 'Use the next coupon to get 20% discount on your next purchase on spatie.be:', | ||
self::MerchDiscount => 'Use the next coupon to get 30% discount on merchandise in our merch store:', | ||
self::Mailcoach50Off => 'Use the next coupon to get 50% off Mailcoach plan:', | ||
self::Flare50Off => 'Use the next coupon to get 50% off Flare plan:', | ||
self::FreeMerch => 'A free piece of Spatie merch, we will contact you to get your details', | ||
self::FreeRay => 'A free yearly Ray license, we will contact you to get your details', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.