fix: model audit cleanups (primaryKey, fuel_type cast, cadence cache)

- StationPriceCurrent: $primaryKey was null; set to 'station_id' + keyType
  string so Eloquent has a sensible default for save() / find() paths.
- UserNotificationPreference: add FuelType enum cast on fuel_type so it
  hydrates as an enum like every other price model.
- Plan::resolveCadenceForUser: cache for 1h under the same plans tag as
  resolveForUser; HandleStripeWebhook busts both keys on subscription
  events.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ovidiu U
2026-04-29 18:32:55 +01:00
parent 775e076bb7
commit 783297694c
4 changed files with 36 additions and 22 deletions

View File

@@ -77,33 +77,41 @@ class Plan extends Model
*/
public static function resolveCadenceForUser(User $user): ?string
{
if (! method_exists($user, 'subscriptions')) {
return null;
}
$cache = Cache::supportsTags() ? Cache::tags(['plans']) : Cache::store();
$priceId = $user->subscriptions()->active()->value('stripe_price');
return $cache->remember(
"plan_cadence_for_user_{$user->id}",
3600,
function () use ($user): ?string {
if (! method_exists($user, 'subscriptions')) {
return null;
}
if ($priceId === null) {
return null;
}
$priceId = $user->subscriptions()->active()->value('stripe_price');
$plan = static::where('stripe_price_id_monthly', $priceId)
->orWhere('stripe_price_id_annual', $priceId)
->first();
if ($priceId === null) {
return null;
}
if ($plan === null) {
return null;
}
$plan = static::where('stripe_price_id_monthly', $priceId)
->orWhere('stripe_price_id_annual', $priceId)
->first();
if ($plan->stripe_price_id_monthly === $priceId) {
return 'monthly';
}
if ($plan === null) {
return null;
}
if ($plan->stripe_price_id_annual === $priceId) {
return 'annual';
}
if ($plan->stripe_price_id_monthly === $priceId) {
return 'monthly';
}
return null;
if ($plan->stripe_price_id_annual === $priceId) {
return 'annual';
}
return null;
}
);
}
protected static function booted(): void