Add subscription tiers, notification preferences, and logging infrastructure
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

- Add database migrations for plans, subscriptions, notification preferences, and notification log tables
- Implement DispatchUserNotificationJob to handle channel resolution, daily limits, and logging (sent/tier_restricted/daily_limit)
- Add SendScheduledWhatsAppJob for scheduled notification delivery
- Create PlanFeatures service to resolve tier capabilities, check daily limits, and validate fuel
This commit is contained in:
Ovidiu U
2026-04-14 16:20:51 +01:00
parent 3cd3467178
commit 4220b1b86a
37 changed files with 2749 additions and 2 deletions

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Jobs;
use App\Models\NotificationLog;
use App\Models\User;
use App\Models\UserNotificationPreference;
use App\Services\PlanFeatures;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
/**
* Resolves allowed notification channels for a user and trigger, sends
* notifications, and logs every outcome (sent, daily_limit, tier_restricted).
*
* Actual sending is stubbed until FuelPriceAlert notification class exists.
*/
final class DispatchUserNotificationJob implements ShouldQueue
{
use Queueable;
/** @var string[] */
private const array ALL_CHANNELS = ['email', 'push', 'whatsapp', 'sms'];
public function __construct(
public readonly User $user,
public readonly string $triggerType,
public readonly string $fuelType,
public readonly ?float $price = null,
) {
$this->onQueue('notifications');
}
public function handle(): void
{
$features = PlanFeatures::for($this->user);
// Step 3: channels that pass tier + user-pref + daily-limit checks
$allowed = $features->channelsFor($this->triggerType);
// Step 4: send and log sent notifications
foreach ($allowed as $channel) {
// TODO: $this->user->notify(new FuelPriceAlert($this->triggerType, $this->fuelType, $this->price));
$this->log($channel, sent: true);
}
// Channels not in the allowed set — split into missed reasons
$notAllowed = array_diff(self::ALL_CHANNELS, $allowed);
foreach ($notAllowed as $channel) {
if (! $this->userHasEnabledPref($channel)) {
// User intentionally disabled — do not log (noise)
continue;
}
if ($features->canUseChannel($channel)) {
// Step 5: tier allows but daily limit exhausted
$this->log($channel, sent: false, missedReason: 'daily_limit');
} else {
// Step 6: tier does not allow the channel the user wanted
$this->log($channel, sent: false, missedReason: 'tier_restricted');
}
}
}
private function log(string $channel, bool $sent, ?string $missedReason = null): void
{
NotificationLog::create([
'user_id' => $this->user->id,
'channel' => $channel,
'trigger_type' => $this->triggerType,
'fuel_type' => $this->fuelType,
'price' => $this->price,
'sent' => $sent,
'missed_reason' => $missedReason,
'created_at' => now(),
]);
}
private function userHasEnabledPref(string $channel): bool
{
return UserNotificationPreference::where('user_id', $this->user->id)
->where('channel', $channel)
->where('enabled', true)
->exists();
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Jobs;
use App\Models\Plan;
use App\Models\User;
use App\Models\UserNotificationPreference;
use App\Services\PlanFeatures;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
/**
* Fan-out job for scheduled WhatsApp updates (morning / evening).
* Finds all eligible users and dispatches DispatchUserNotificationJob per user.
*
* Scheduled at 07:30 (morning) and 18:00 (evening) via routes/console.php.
*/
final class SendScheduledWhatsAppJob implements ShouldQueue
{
use Queueable;
public function __construct(public readonly string $period)
{
//
}
public function handle(): void
{
$triggerType = $this->period === 'morning' ? 'scheduled_morning' : 'scheduled_evening';
// Plans that allow scheduled WhatsApp updates
$eligiblePlanNames = Plan::where('active', true)
->get()
->filter(fn (Plan $plan): bool => ($plan->features['whatsapp']['scheduled_updates'] ?? 0) > 0)
->pluck('name')
->all();
if (empty($eligiblePlanNames)) {
return;
}
// Users who have whatsapp preference enabled
$userIds = UserNotificationPreference::where('channel', 'whatsapp')
->where('enabled', true)
->distinct()
->pluck('user_id');
User::whereIn('id', $userIds)
->each(function (User $user) use ($triggerType, $eligiblePlanNames): void {
$features = PlanFeatures::for($user);
// Skip if their tier isn't eligible or daily limit is hit
if (! in_array($features->tier(), $eligiblePlanNames, strict: true)) {
return;
}
if (! $features->canSendNow('whatsapp')) {
return;
}
DispatchUserNotificationJob::dispatch($user, $triggerType, fuelType: 'all');
});
}
}