-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[PM-13450] Admin: Display Multi-organization Enterprise attributes on provider details #4955
Open
jonashendrickx
wants to merge
22
commits into
main
Choose a base branch
from
PM-13450-Admin-Display-Multi-organization-Enterprise-attributes-on-provider-details
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+567
โ191
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
036e4b5
ok
jonashendrickx ce0ed1b
Merge branch 'main' into PM-13450-Admin-Display-Multi-organization-Enโฆ
jonashendrickx 541eede
feedback
jonashendrickx fd31ad2
Merge remote-tracking branch 'origin/PM-13450-Admin-Display-Multi-orgโฆ
jonashendrickx 57a1fbe
Update the billing cycle
jonashendrickx 9f88cca
rename
jonashendrickx 6c83b20
rename
jonashendrickx fb39357
Merge remote-tracking branch 'origin/PM-13450-Admin-Display-Multi-orgโฆ
jonashendrickx 054aa05
poc
jonashendrickx d1ec312
wip poc
jonashendrickx ca4dd32
remove
jonashendrickx c2101ab
Implement ability to change plans
jonashendrickx 7c6ffe7
r
jonashendrickx c8b007d
remove comment
jonashendrickx a7f5344
tests
jonashendrickx 394b873
good
jonashendrickx ce15d5d
feedback
jonashendrickx 69a7742
remove double call
jonashendrickx 204ef2d
refactor
jonashendrickx 079d899
remove unused code
jonashendrickx 534b88b
get provider subscription for stripe adapter
jonashendrickx 83cfbff
Stripe never returns null;
jonashendrickx 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 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
using Bit.Core.Billing.Extensions; | ||
using Bit.Core.Billing.Repositories; | ||
using Bit.Core.Billing.Services; | ||
using Bit.Core.Billing.Services.Contracts; | ||
using Bit.Core.Context; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Exceptions; | ||
|
@@ -437,144 +438,142 @@ | |
} | ||
} | ||
|
||
public async Task UpdateSeatMinimums( | ||
Provider provider, | ||
int enterpriseSeatMinimum, | ||
int teamsSeatMinimum) | ||
public async Task ChangePlan(ChangeProviderPlanCommand command) | ||
{ | ||
ArgumentNullException.ThrowIfNull(provider); | ||
var plan = await providerPlanRepository.GetByIdAsync(command.ProviderPlanId); | ||
|
||
if (enterpriseSeatMinimum < 0 || teamsSeatMinimum < 0) | ||
if (plan == null) | ||
{ | ||
throw new BadRequestException("Provider seat minimums must be at least 0."); | ||
throw new BadRequestException("Provider plan not found."); | ||
} | ||
|
||
var subscription = await stripeAdapter.SubscriptionGetAsync(provider.GatewaySubscriptionId); | ||
|
||
var subscriptionItemOptionsList = new List<SubscriptionItemOptions>(); | ||
if (plan.PlanType == command.NewPlan) | ||
{ | ||
return; | ||
} | ||
|
||
var providerPlans = await providerPlanRepository.GetByProviderId(provider.Id); | ||
var oldPlanConfiguration = StaticStore.GetPlan(plan.PlanType); | ||
|
||
var enterpriseProviderPlan = | ||
providerPlans.Single(providerPlan => providerPlan.PlanType == PlanType.EnterpriseMonthly); | ||
plan.PlanType = command.NewPlan; | ||
await providerPlanRepository.ReplaceAsync(plan); | ||
|
||
if (enterpriseProviderPlan.SeatMinimum != enterpriseSeatMinimum) | ||
Subscription subscription; | ||
try | ||
{ | ||
subscription = await stripeAdapter.ProviderSubscriptionGetAsync(command.GatewaySubscriptionId, plan.ProviderId); | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
var enterprisePriceId = StaticStore.GetPlan(PlanType.EnterpriseMonthly).PasswordManager | ||
.StripeProviderPortalSeatPlanId; | ||
throw new ConflictException("Subscription not found."); | ||
} | ||
|
||
var enterpriseSubscriptionItem = subscription.Items.First(item => item.Price.Id == enterprisePriceId); | ||
var oldSubscriptionItem = subscription.Items.SingleOrDefault(x => | ||
jonashendrickx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x.Price.Id == oldPlanConfiguration.PasswordManager.StripeProviderPortalSeatPlanId); | ||
|
||
if (enterpriseProviderPlan.PurchasedSeats == 0) | ||
{ | ||
if (enterpriseProviderPlan.AllocatedSeats > enterpriseSeatMinimum) | ||
var updateOptions = new SubscriptionUpdateOptions | ||
{ | ||
Items = | ||
[ | ||
new SubscriptionItemOptions | ||
{ | ||
enterpriseProviderPlan.PurchasedSeats = | ||
enterpriseProviderPlan.AllocatedSeats - enterpriseSeatMinimum; | ||
|
||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = enterpriseSubscriptionItem.Id, | ||
Price = enterprisePriceId, | ||
Quantity = enterpriseProviderPlan.AllocatedSeats | ||
}); | ||
} | ||
else | ||
Price = StaticStore.GetPlan(command.NewPlan).PasswordManager.StripeProviderPortalSeatPlanId, | ||
Quantity = oldSubscriptionItem!.Quantity | ||
jonashendrickx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
new SubscriptionItemOptions | ||
{ | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = enterpriseSubscriptionItem.Id, | ||
Price = enterprisePriceId, | ||
Quantity = enterpriseSeatMinimum | ||
}); | ||
Id = oldSubscriptionItem.Id, | ||
Deleted = true | ||
} | ||
} | ||
else | ||
{ | ||
var totalEnterpriseSeats = enterpriseProviderPlan.SeatMinimum + enterpriseProviderPlan.PurchasedSeats; | ||
] | ||
}; | ||
|
||
if (enterpriseSeatMinimum <= totalEnterpriseSeats) | ||
{ | ||
enterpriseProviderPlan.PurchasedSeats = totalEnterpriseSeats - enterpriseSeatMinimum; | ||
} | ||
else | ||
{ | ||
enterpriseProviderPlan.PurchasedSeats = 0; | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = enterpriseSubscriptionItem.Id, | ||
Price = enterprisePriceId, | ||
Quantity = enterpriseSeatMinimum | ||
}); | ||
} | ||
} | ||
await stripeAdapter.SubscriptionUpdateAsync(command.GatewaySubscriptionId, updateOptions); | ||
} | ||
|
||
enterpriseProviderPlan.SeatMinimum = enterpriseSeatMinimum; | ||
public async Task UpdateSeatMinimums(UpdateProviderSeatMinimumsCommand command) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. โ Same question as above with regard to this command. |
||
{ | ||
if (command.Configuration.Any(x => x.SeatsMinimum < 0)) | ||
{ | ||
throw new BadRequestException("Provider seat minimums must be at least 0."); | ||
} | ||
|
||
await providerPlanRepository.ReplaceAsync(enterpriseProviderPlan); | ||
Subscription subscription; | ||
try | ||
{ | ||
subscription = await stripeAdapter.ProviderSubscriptionGetAsync(command.GatewaySubscriptionId, command.Id); | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
throw new ConflictException("Subscription not found."); | ||
} | ||
|
||
var teamsProviderPlan = | ||
providerPlans.Single(providerPlan => providerPlan.PlanType == PlanType.TeamsMonthly); | ||
var subscriptionItemOptionsList = new List<SubscriptionItemOptions>(); | ||
|
||
if (teamsProviderPlan.SeatMinimum != teamsSeatMinimum) | ||
{ | ||
var teamsPriceId = StaticStore.GetPlan(PlanType.TeamsMonthly).PasswordManager | ||
.StripeProviderPortalSeatPlanId; | ||
var providerPlans = await providerPlanRepository.GetByProviderId(command.Id); | ||
|
||
var teamsSubscriptionItem = subscription.Items.First(item => item.Price.Id == teamsPriceId); | ||
foreach (var newPlanConfiguration in command.Configuration) | ||
{ | ||
jonashendrickx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var providerPlan = | ||
providerPlans.Single(providerPlan => providerPlan.PlanType == newPlanConfiguration.Plan); | ||
|
||
if (teamsProviderPlan.PurchasedSeats == 0) | ||
if (providerPlan.SeatMinimum != newPlanConfiguration.SeatsMinimum) | ||
{ | ||
if (teamsProviderPlan.AllocatedSeats > teamsSeatMinimum) | ||
{ | ||
teamsProviderPlan.PurchasedSeats = teamsProviderPlan.AllocatedSeats - teamsSeatMinimum; | ||
var priceId = StaticStore.GetPlan(newPlanConfiguration.Plan).PasswordManager | ||
.StripeProviderPortalSeatPlanId; | ||
var subscriptionItem = subscription.Items.First(item => item.Price.Id == priceId); | ||
|
||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = teamsSubscriptionItem.Id, | ||
Price = teamsPriceId, | ||
Quantity = teamsProviderPlan.AllocatedSeats | ||
}); | ||
} | ||
else | ||
if (providerPlan.PurchasedSeats == 0) | ||
{ | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
if (providerPlan.AllocatedSeats > newPlanConfiguration.SeatsMinimum) | ||
{ | ||
Id = teamsSubscriptionItem.Id, | ||
Price = teamsPriceId, | ||
Quantity = teamsSeatMinimum | ||
}); | ||
} | ||
} | ||
else | ||
{ | ||
var totalTeamsSeats = teamsProviderPlan.SeatMinimum + teamsProviderPlan.PurchasedSeats; | ||
|
||
if (teamsSeatMinimum <= totalTeamsSeats) | ||
{ | ||
teamsProviderPlan.PurchasedSeats = totalTeamsSeats - teamsSeatMinimum; | ||
providerPlan.PurchasedSeats = providerPlan.AllocatedSeats - newPlanConfiguration.SeatsMinimum; | ||
|
||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = subscriptionItem.Id, | ||
Price = priceId, | ||
Quantity = providerPlan.AllocatedSeats | ||
}); | ||
} | ||
else | ||
{ | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = subscriptionItem.Id, | ||
Price = priceId, | ||
Quantity = newPlanConfiguration.SeatsMinimum | ||
}); | ||
} | ||
} | ||
else | ||
{ | ||
teamsProviderPlan.PurchasedSeats = 0; | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
var totalSeats = providerPlan.SeatMinimum + providerPlan.PurchasedSeats; | ||
|
||
if (newPlanConfiguration.SeatsMinimum <= totalSeats) | ||
{ | ||
providerPlan.PurchasedSeats = totalSeats - newPlanConfiguration.SeatsMinimum; | ||
} | ||
else | ||
{ | ||
Id = teamsSubscriptionItem.Id, | ||
Price = teamsPriceId, | ||
Quantity = teamsSeatMinimum | ||
}); | ||
providerPlan.PurchasedSeats = 0; | ||
subscriptionItemOptionsList.Add(new SubscriptionItemOptions | ||
{ | ||
Id = subscriptionItem.Id, | ||
Price = priceId, | ||
Quantity = newPlanConfiguration.SeatsMinimum | ||
}); | ||
} | ||
} | ||
} | ||
|
||
teamsProviderPlan.SeatMinimum = teamsSeatMinimum; | ||
providerPlan.SeatMinimum = newPlanConfiguration.SeatsMinimum; | ||
|
||
await providerPlanRepository.ReplaceAsync(teamsProviderPlan); | ||
await providerPlanRepository.ReplaceAsync(providerPlan); | ||
} | ||
} | ||
|
||
if (subscriptionItemOptionsList.Count > 0) | ||
{ | ||
await stripeAdapter.SubscriptionUpdateAsync(provider.GatewaySubscriptionId, | ||
await stripeAdapter.SubscriptionUpdateAsync(command.GatewaySubscriptionId, | ||
new SubscriptionUpdateOptions { Items = subscriptionItemOptionsList }); | ||
} | ||
} | ||
|
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.
โ Looks like this is called in one place where we already have access to both the
Provider
and theProviderPlan
. Could we just include those in the command rather than double fetch them from the DB?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.
I just have to check if I can refactor the duplicate call to retrieve provider plans further at this point