refactor: flatten plans.features JSON to typed columns
The features JSON column required defensive fallback stubs in three places (Plan::resolveForUser, PlanFeatures::__construct, PlanSeeder) and silently swallowed misspelled keys. Typed columns give Eloquent type-safe reads, simplify the Filament form (no more dotted JSON paths), and let resolveForUser fail loud when the free row is missing. PlanFeatures public API is unchanged so consumers (jobs, middleware) need no rewrites — one missed JSON read in SendScheduledWhatsAppJob was caught and converted to a typed where() query. tests/Pest.php seeds PlanSeeder in beforeEach so any feature test that resolves a plan finds the free row, mirroring production where plans always exist. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -28,20 +28,19 @@ it('canUseChannel returns false for sms on free tier', function (): void {
|
||||
it('canUseChannel returns false for sms on basic tier', function (): void {
|
||||
$plan = Plan::where('name', 'basic')->first();
|
||||
|
||||
// basic has sms.enabled = false in features
|
||||
expect($plan->features['sms']['enabled'])->toBeFalse();
|
||||
expect($plan->sms_enabled)->toBeFalse();
|
||||
});
|
||||
|
||||
it('canUseChannel returns true for sms on plus tier', function (): void {
|
||||
$plan = Plan::where('name', 'plus')->first();
|
||||
|
||||
expect($plan->features['sms']['enabled'])->toBeTrue();
|
||||
expect($plan->sms_enabled)->toBeTrue();
|
||||
});
|
||||
|
||||
it('canUseChannel returns true for sms on pro tier', function (): void {
|
||||
$plan = Plan::where('name', 'pro')->first();
|
||||
|
||||
expect($plan->features['sms']['enabled'])->toBeTrue();
|
||||
expect($plan->sms_enabled)->toBeTrue();
|
||||
});
|
||||
|
||||
// ─── canSendNow ───────────────────────────────────────────────────────────────
|
||||
@@ -54,10 +53,9 @@ it('canSendNow returns false when tier does not allow the channel', function ():
|
||||
});
|
||||
|
||||
it('canSendNow returns false when daily limit is reached', function (): void {
|
||||
$plan = Plan::where('name', 'plus')->first(); // sms daily_limit = 1
|
||||
$plan = Plan::where('name', 'plus')->first(); // sms_daily_limit = 1
|
||||
$user = User::factory()->create();
|
||||
|
||||
// Give user a preference so channelsFor works, and log one sent SMS today
|
||||
UserNotificationPreference::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'channel' => 'sms',
|
||||
@@ -72,10 +70,8 @@ it('canSendNow returns false when daily limit is reached', function (): void {
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
// Manually bypass resolveForUser by using the plus plan features directly
|
||||
expect($plan->features['sms']['daily_limit'])->toBe(1);
|
||||
expect($plan->sms_daily_limit)->toBe(1);
|
||||
|
||||
// Confirm log count matches limit
|
||||
$sentCount = NotificationLog::where('user_id', $user->id)
|
||||
->where('channel', 'sms')
|
||||
->where('sent', true)
|
||||
@@ -88,7 +84,7 @@ it('canSendNow returns false when daily limit is reached', function (): void {
|
||||
// ─── canTrackFuelType ─────────────────────────────────────────────────────────
|
||||
|
||||
it('canTrackFuelType respects max limit for non-pro tiers', function (): void {
|
||||
$plan = Plan::where('name', 'basic')->first(); // max = 1
|
||||
$plan = Plan::where('name', 'basic')->first(); // max_fuel_types = 1
|
||||
$user = User::factory()->create();
|
||||
|
||||
UserNotificationPreference::factory()->create([
|
||||
@@ -98,7 +94,7 @@ it('canTrackFuelType respects max limit for non-pro tiers', function (): void {
|
||||
'enabled' => true,
|
||||
]);
|
||||
|
||||
expect($plan->features['fuel_types']['max'])->toBe(1);
|
||||
expect($plan->max_fuel_types)->toBe(1);
|
||||
|
||||
$count = UserNotificationPreference::where('user_id', $user->id)
|
||||
->distinct('fuel_type')
|
||||
@@ -110,7 +106,7 @@ it('canTrackFuelType respects max limit for non-pro tiers', function (): void {
|
||||
it('pro tier has null fuel type limit meaning unlimited', function (): void {
|
||||
$plan = Plan::where('name', 'pro')->first();
|
||||
|
||||
expect($plan->features['fuel_types']['max'])->toBeNull();
|
||||
expect($plan->max_fuel_types)->toBeNull();
|
||||
});
|
||||
|
||||
// ─── can() feature flags ──────────────────────────────────────────────────────
|
||||
@@ -118,19 +114,18 @@ it('pro tier has null fuel type limit meaning unlimited', function (): void {
|
||||
it('can returns false for ai_predictions on free tier', function (): void {
|
||||
$plan = Plan::where('name', 'free')->first();
|
||||
|
||||
expect($plan->features['ai_predictions'])->toBeFalse();
|
||||
expect($plan->ai_predictions)->toBeFalse();
|
||||
});
|
||||
|
||||
it('can returns true for ai_predictions on plus tier', function (): void {
|
||||
$plan = Plan::where('name', 'plus')->first();
|
||||
|
||||
expect($plan->features['ai_predictions'])->toBeTrue();
|
||||
expect($plan->ai_predictions)->toBeTrue();
|
||||
});
|
||||
|
||||
// ─── PlanSeeder idempotency ───────────────────────────────────────────────────
|
||||
|
||||
it('PlanSeeder is idempotent', function (): void {
|
||||
// Run seeder a second time
|
||||
$this->artisan('db:seed', ['--class' => 'PlanSeeder']);
|
||||
|
||||
expect(Plan::count())->toBe(4);
|
||||
@@ -211,15 +206,15 @@ it('scopeForFuelType filters by fuel type', function (): void {
|
||||
|
||||
// ─── push frequency ───────────────────────────────────────────────────────────
|
||||
|
||||
it('seeds push.frequency for every tier', function (): void {
|
||||
expect(Plan::where('name', 'free')->first()->features['push'])
|
||||
->toBe(['enabled' => false, 'frequency' => 'none'])
|
||||
->and(Plan::where('name', 'basic')->first()->features['push'])
|
||||
->toBe(['enabled' => true, 'frequency' => 'daily'])
|
||||
->and(Plan::where('name', 'plus')->first()->features['push'])
|
||||
->toBe(['enabled' => true, 'frequency' => 'triggered'])
|
||||
->and(Plan::where('name', 'pro')->first()->features['push'])
|
||||
->toBe(['enabled' => true, 'frequency' => 'triggered']);
|
||||
it('seeds push frequency for every tier', function (): void {
|
||||
expect(Plan::where('name', 'free')->first())
|
||||
->push_enabled->toBeFalse()->push_frequency->toBe('none')
|
||||
->and(Plan::where('name', 'basic')->first())
|
||||
->push_enabled->toBeTrue()->push_frequency->toBe('daily')
|
||||
->and(Plan::where('name', 'plus')->first())
|
||||
->push_enabled->toBeTrue()->push_frequency->toBe('triggered')
|
||||
->and(Plan::where('name', 'pro')->first())
|
||||
->push_enabled->toBeTrue()->push_frequency->toBe('triggered');
|
||||
});
|
||||
|
||||
// ─── display name ─────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user