feat: fold subscription deletion handling into HandleStripeWebhook
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
use App\Listeners\DowngradeUserOnSubscriptionDeleted;
|
||||
use App\Models\User;
|
||||
use App\Models\UserNotificationPreference;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('disables whatsapp and sms preferences when a stripe subscription is deleted', function (): void {
|
||||
$user = User::factory()->create(['stripe_id' => 'cus_test_123']);
|
||||
|
||||
UserNotificationPreference::query()->insert([
|
||||
['user_id' => $user->id, 'channel' => 'whatsapp', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
['user_id' => $user->id, 'channel' => 'sms', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
['user_id' => $user->id, 'channel' => 'email', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
]);
|
||||
|
||||
(new DowngradeUserOnSubscriptionDeleted)->handle(new WebhookReceived([
|
||||
'type' => 'customer.subscription.deleted',
|
||||
'data' => ['object' => ['customer' => 'cus_test_123']],
|
||||
]));
|
||||
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'whatsapp')->value('enabled'))->toBeFalse();
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'sms')->value('enabled'))->toBeFalse();
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'email')->value('enabled'))->toBeTrue();
|
||||
});
|
||||
|
||||
it('ignores non-deletion webhook events', function (): void {
|
||||
$user = User::factory()->create(['stripe_id' => 'cus_test_456']);
|
||||
UserNotificationPreference::query()->insert([
|
||||
['user_id' => $user->id, 'channel' => 'whatsapp', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
]);
|
||||
|
||||
(new DowngradeUserOnSubscriptionDeleted)->handle(new WebhookReceived([
|
||||
'type' => 'customer.subscription.updated',
|
||||
'data' => ['object' => ['customer' => 'cus_test_456']],
|
||||
]));
|
||||
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'whatsapp')->value('enabled'))->toBeTrue();
|
||||
});
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use App\Listeners\HandleStripeWebhook;
|
||||
use App\Models\User;
|
||||
use App\Models\UserNotificationPreference;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
@@ -40,3 +41,29 @@ it('busts the plan cache on customer.subscription.updated', function (): void {
|
||||
|
||||
expect(Cache::tags(['plans'])->get("plan_for_user_{$user->id}"))->toBeNull();
|
||||
});
|
||||
|
||||
it('on customer.subscription.deleted disables whatsapp+sms prefs, clears grace, busts cache', function (): void {
|
||||
$user = User::factory()->create([
|
||||
'stripe_id' => 'cus_deleted_1',
|
||||
'grace_period_until' => now()->addDays(5),
|
||||
]);
|
||||
|
||||
UserNotificationPreference::query()->insert([
|
||||
['user_id' => $user->id, 'channel' => 'whatsapp', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
['user_id' => $user->id, 'channel' => 'sms', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
['user_id' => $user->id, 'channel' => 'email', 'fuel_type' => 'E10', 'enabled' => true, 'created_at' => now(), 'updated_at' => now()],
|
||||
]);
|
||||
|
||||
Cache::tags(['plans'])->put("plan_for_user_{$user->id}", 'stale', 3600);
|
||||
|
||||
(new HandleStripeWebhook)->handle(new WebhookReceived([
|
||||
'type' => 'customer.subscription.deleted',
|
||||
'data' => ['object' => ['customer' => 'cus_deleted_1']],
|
||||
]));
|
||||
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'whatsapp')->value('enabled'))->toBeFalse();
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'sms')->value('enabled'))->toBeFalse();
|
||||
expect(UserNotificationPreference::where('user_id', $user->id)->where('channel', 'email')->value('enabled'))->toBeTrue();
|
||||
expect($user->fresh()->grace_period_until)->toBeNull();
|
||||
expect(Cache::tags(['plans'])->get("plan_for_user_{$user->id}"))->toBeNull();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user