-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[PM 29610]Update Account Storage Endpoint #6750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amorask-bitwarden
merged 18 commits into
main
from
billing/pm-29610/update-account-storage-endpoint
Jan 5, 2026
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ca40b3f
update account storage endpoint
cyprain-okeke e0a1e26
Fix the failing test
cyprain-okeke a3e6679
Added flag and refactor base on pr comments
cyprain-okeke 56bf37d
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke d58c0da
fix the lint error
cyprain-okeke 505428e
Resolve the pr comments
cyprain-okeke c81faf1
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke cf6d7f7
Fix the failing test
cyprain-okeke 77420e7
Fix the failing test
cyprain-okeke d7d080d
Return none
cyprain-okeke 13117e1
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke 3e2c981
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke 89560de
Resolve the lint error
cyprain-okeke bcb625c
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke ceee636
Fix the failing test
cyprain-okeke 243a6c6
Merge branch 'main' into billing/pm-29610/update-account-storage-endpโฆ
cyprain-okeke 836fff6
Add the missing test
cyprain-okeke 6a5ccb1
Formatting issues fixed
cyprain-okeke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Api/Billing/Models/Requests/Storage/StorageUpdateRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| ๏ปฟusing System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace Bit.Api.Billing.Models.Requests.Storage; | ||
|
|
||
| /// <summary> | ||
| /// Request model for updating storage allocation on a user's premium subscription. | ||
| /// Allows for both increasing and decreasing storage in an idempotent manner. | ||
| /// </summary> | ||
| public class StorageUpdateRequest : IValidatableObject | ||
| { | ||
| /// <summary> | ||
| /// The additional storage in GB beyond the base storage. | ||
| /// Must be between 0 and the maximum allowed (minus base storage). | ||
| /// </summary> | ||
| [Required] | ||
| [Range(0, 99)] | ||
| public short AdditionalStorageGb { get; set; } | ||
|
|
||
| public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
| { | ||
| if (AdditionalStorageGb < 0) | ||
| { | ||
| yield return new ValidationResult( | ||
| "Additional storage cannot be negative.", | ||
| new[] { nameof(AdditionalStorageGb) }); | ||
| } | ||
|
|
||
| if (AdditionalStorageGb > 99) | ||
| { | ||
| yield return new ValidationResult( | ||
| "Maximum additional storage is 99 GB.", | ||
| new[] { nameof(AdditionalStorageGb) }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/Core/Billing/Premium/Commands/UpdatePremiumStorageCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| ๏ปฟusing Bit.Core.Billing.Commands; | ||
| using Bit.Core.Billing.Pricing; | ||
| using Bit.Core.Billing.Services; | ||
| using Bit.Core.Entities; | ||
| using Bit.Core.Services; | ||
| using Bit.Core.Utilities; | ||
| using Microsoft.Extensions.Logging; | ||
| using OneOf.Types; | ||
| using Stripe; | ||
|
|
||
| namespace Bit.Core.Billing.Premium.Commands; | ||
|
|
||
| /// <summary> | ||
| /// Updates the storage allocation for a premium user's subscription. | ||
| /// Handles both increases and decreases in storage in an idempotent manner. | ||
| /// </summary> | ||
| public interface IUpdatePremiumStorageCommand | ||
| { | ||
| /// <summary> | ||
| /// Updates the user's storage by the specified additional amount. | ||
| /// </summary> | ||
| /// <param name="user">The premium user whose storage should be updated.</param> | ||
| /// <param name="additionalStorageGb">The additional storage amount in GB beyond base storage.</param> | ||
| /// <returns>A billing command result indicating success or failure.</returns> | ||
| Task<BillingCommandResult<None>> Run(User user, short additionalStorageGb); | ||
| } | ||
|
|
||
| public class UpdatePremiumStorageCommand( | ||
| IStripeAdapter stripeAdapter, | ||
| IUserService userService, | ||
| IPricingClient pricingClient, | ||
| ILogger<UpdatePremiumStorageCommand> logger) | ||
| : BaseBillingCommand<UpdatePremiumStorageCommand>(logger), IUpdatePremiumStorageCommand | ||
| { | ||
| public Task<BillingCommandResult<None>> Run(User user, short additionalStorageGb) => HandleAsync<None>(async () => | ||
| { | ||
| if (!user.Premium) | ||
| { | ||
| return new BadRequest("User does not have a premium subscription."); | ||
| } | ||
|
|
||
| if (!user.MaxStorageGb.HasValue) | ||
| { | ||
| return new BadRequest("No access to storage."); | ||
| } | ||
|
|
||
| // Fetch all premium plans and the user's subscription to find which plan they're on | ||
| var premiumPlans = await pricingClient.ListPremiumPlans(); | ||
| var subscription = await stripeAdapter.GetSubscriptionAsync(user.GatewaySubscriptionId); | ||
|
|
||
| // Find the password manager subscription item (seat, not storage) and match it to a plan | ||
| var passwordManagerItem = subscription.Items.Data.FirstOrDefault(i => | ||
| premiumPlans.Any(p => p.Seat.StripePriceId == i.Price.Id)); | ||
|
|
||
| if (passwordManagerItem == null) | ||
| { | ||
| return new BadRequest("Premium subscription item not found."); | ||
| } | ||
|
|
||
| var premiumPlan = premiumPlans.First(p => p.Seat.StripePriceId == passwordManagerItem.Price.Id); | ||
|
|
||
| var baseStorageGb = (short)premiumPlan.Storage.Provided; | ||
|
|
||
| if (additionalStorageGb < 0) | ||
| { | ||
| return new BadRequest("Additional storage cannot be negative."); | ||
| } | ||
|
|
||
| var newTotalStorageGb = (short)(baseStorageGb + additionalStorageGb); | ||
|
|
||
| if (newTotalStorageGb > 100) | ||
| { | ||
| return new BadRequest("Maximum storage is 100 GB."); | ||
| } | ||
|
|
||
| // Idempotency check: if user already has the requested storage, return success | ||
| if (user.MaxStorageGb == newTotalStorageGb) | ||
| { | ||
| return new None(); | ||
| } | ||
|
|
||
| var remainingStorage = user.StorageBytesRemaining(newTotalStorageGb); | ||
| if (remainingStorage < 0) | ||
| { | ||
| return new BadRequest( | ||
| $"You are currently using {CoreHelpers.ReadableBytesSize(user.Storage.GetValueOrDefault(0))} of storage. " + | ||
| "Delete some stored data first."); | ||
| } | ||
|
|
||
| // Find the storage line item in the subscription | ||
| var storageItem = subscription.Items.Data.FirstOrDefault(i => i.Price.Id == premiumPlan.Storage.StripePriceId); | ||
|
|
||
| var subscriptionItemOptions = new List<SubscriptionItemOptions>(); | ||
|
|
||
| if (additionalStorageGb > 0) | ||
| { | ||
| if (storageItem != null) | ||
| { | ||
| // Update existing storage item | ||
| subscriptionItemOptions.Add(new SubscriptionItemOptions | ||
| { | ||
| Id = storageItem.Id, | ||
| Price = premiumPlan.Storage.StripePriceId, | ||
| Quantity = additionalStorageGb | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| // Add new storage item | ||
| subscriptionItemOptions.Add(new SubscriptionItemOptions | ||
| { | ||
| Price = premiumPlan.Storage.StripePriceId, | ||
| Quantity = additionalStorageGb | ||
| }); | ||
| } | ||
| } | ||
| else if (storageItem != null) | ||
| { | ||
| // Remove storage item if setting to 0 | ||
| subscriptionItemOptions.Add(new SubscriptionItemOptions | ||
| { | ||
| Id = storageItem.Id, | ||
| Deleted = true | ||
| }); | ||
| } | ||
|
|
||
| // Update subscription with prorations | ||
| // Storage is billed annually, so we create prorations and invoice immediately | ||
| var subscriptionUpdateOptions = new SubscriptionUpdateOptions | ||
| { | ||
| Items = subscriptionItemOptions, | ||
| ProrationBehavior = Core.Constants.CreateProrations | ||
| }; | ||
|
|
||
| await stripeAdapter.UpdateSubscriptionAsync(subscription.Id, subscriptionUpdateOptions); | ||
|
|
||
| // Update the user's max storage | ||
| user.MaxStorageGb = newTotalStorageGb; | ||
| await userService.SaveUserAsync(user); | ||
|
|
||
| // No payment intent needed - the subscription update will automatically create and finalize the invoice | ||
| return new None(); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
โ Is this a business rule we have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this 100 GB limit exists in the old BillingHelpers.AdjustStorageAsync code at /server/src/Core/Utilities/BillingHelpers.cs:38-41. The new UpdatePremiumStorageCommand copied this
validation.