fix(subscription): clamp dynamic quantity to MIN_SERVER_LIMIT on update

This commit is contained in:
Andras Bacsai 2026-06-19 08:27:41 +02:00
parent 02ab4b39ef
commit 00c5a630cf
2 changed files with 49 additions and 2 deletions

View file

@ -260,7 +260,10 @@ public function handle(): void
$comment = data_get($data, 'cancellation_details.comment');
$lookup_key = data_get($data, 'items.data.0.price.lookup_key');
if (str($lookup_key)->contains('dynamic')) {
$quantity = min((int) data_get($data, 'items.data.0.quantity', 2), UpdateSubscriptionQuantity::MAX_SERVER_LIMIT);
$quantity = max(
UpdateSubscriptionQuantity::MIN_SERVER_LIMIT,
min((int) data_get($data, 'items.data.0.quantity', 2), UpdateSubscriptionQuantity::MAX_SERVER_LIMIT)
);
$team = data_get($subscription, 'team');
if ($team) {
$team->update([

View file

@ -143,7 +143,51 @@
});
});
describe('customer.subscription.updated clamps quantity to MAX_SERVER_LIMIT', function () {
describe('customer.subscription.updated clamps quantity to subscription bounds', function () {
test('quantity below MIN is clamped to 2', function () {
Queue::fake();
Subscription::create([
'team_id' => $this->team->id,
'stripe_subscription_id' => 'sub_existing',
'stripe_customer_id' => 'cus_min_clamp_test',
'stripe_invoice_paid' => true,
]);
$event = [
'type' => 'customer.subscription.updated',
'data' => [
'object' => [
'customer' => 'cus_min_clamp_test',
'id' => 'sub_existing',
'status' => 'active',
'metadata' => [
'team_id' => $this->team->id,
'user_id' => $this->user->id,
],
'items' => [
'data' => [[
'subscription' => 'sub_existing',
'plan' => ['id' => 'price_dynamic_monthly'],
'price' => ['lookup_key' => 'dynamic_monthly'],
'quantity' => 1,
]],
],
'cancel_at_period_end' => false,
'cancellation_details' => ['feedback' => null, 'comment' => null],
],
],
];
$job = new StripeProcessJob($event);
$job->handle();
$this->team->refresh();
expect($this->team->custom_server_limit)->toBe(2);
Queue::assertPushed(ServerLimitCheckJob::class);
});
test('quantity exceeding MAX is clamped to 100', function () {
Queue::fake();