Add subscription tiers, notification preferences, and logging infrastructure
- 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:
64
app/Jobs/SendScheduledWhatsAppJob.php
Normal file
64
app/Jobs/SendScheduledWhatsAppJob.php
Normal 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');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user