-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-25379] Refactor org metadata #6418
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
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
63a6541
ignore serena
kdenney f2ad3aa
removing unused properties from org metadata
kdenney 0c1e6bd
removing further properties that can already be fetched on the client…
kdenney 0d8a5ac
new vnext endpoint for org metadata plus caching metadata first pass
kdenney 5f9faca
[PM-25379] decided against cache and new query shouldn't use the service
kdenney 6d24d5d
Merge branch 'main' into billing/pm-25379/clean-up-org-metadata-usage
kdenney ddbf084
pr feedback
kdenney d557580
run dotnet format
kdenney 7d16f03
Merge branch 'main' into billing/pm-25379/clean-up-org-metadata-usage
kdenney 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,3 +231,4 @@ bitwarden_license/src/Sso/Sso.zip | |
| /identity.json | ||
| /api.json | ||
| /api.public.json | ||
| .serena/ | ||
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
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
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
18 changes: 0 additions & 18 deletions
18
src/Core/Billing/Organizations/Models/OrganizationMetadata.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 |
|---|---|---|
| @@ -1,28 +1,10 @@ | ||
| namespace Bit.Core.Billing.Organizations.Models; | ||
|
|
||
| public record OrganizationMetadata( | ||
| bool IsEligibleForSelfHost, | ||
| bool IsManaged, | ||
| bool IsOnSecretsManagerStandalone, | ||
| bool IsSubscriptionUnpaid, | ||
| bool HasSubscription, | ||
| bool HasOpenInvoice, | ||
| bool IsSubscriptionCanceled, | ||
| DateTime? InvoiceDueDate, | ||
| DateTime? InvoiceCreatedDate, | ||
| DateTime? SubPeriodEndDate, | ||
| int OrganizationOccupiedSeats) | ||
| { | ||
| public static OrganizationMetadata Default => new OrganizationMetadata( | ||
| false, | ||
| false, | ||
| false, | ||
| false, | ||
| false, | ||
| false, | ||
| false, | ||
| null, | ||
| null, | ||
| null, | ||
| 0); | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/Core/Billing/Organizations/Queries/GetOrganizationMetadataQuery.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,95 @@ | ||
| using Bit.Core.AdminConsole.Entities; | ||
| using Bit.Core.Billing.Constants; | ||
| using Bit.Core.Billing.Organizations.Models; | ||
| using Bit.Core.Billing.Pricing; | ||
| using Bit.Core.Billing.Services; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Core.Settings; | ||
| using Stripe; | ||
|
|
||
| namespace Bit.Core.Billing.Organizations.Queries; | ||
|
|
||
| public interface IGetOrganizationMetadataQuery | ||
| { | ||
| Task<OrganizationMetadata?> Run(Organization organization); | ||
| } | ||
|
|
||
| public class GetOrganizationMetadataQuery( | ||
| IGlobalSettings globalSettings, | ||
| IOrganizationRepository organizationRepository, | ||
| IPricingClient pricingClient, | ||
| ISubscriberService subscriberService) : IGetOrganizationMetadataQuery | ||
| { | ||
| public async Task<OrganizationMetadata?> Run(Organization organization) | ||
| { | ||
| if (organization == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (globalSettings.SelfHosted) | ||
| { | ||
| return OrganizationMetadata.Default; | ||
| } | ||
|
|
||
| var orgOccupiedSeats = await organizationRepository.GetOccupiedSeatCountByOrganizationIdAsync(organization.Id); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(organization.GatewaySubscriptionId)) | ||
| { | ||
| return OrganizationMetadata.Default with | ||
| { | ||
| OrganizationOccupiedSeats = orgOccupiedSeats.Total | ||
| }; | ||
| } | ||
|
|
||
| var customer = await subscriberService.GetCustomer(organization, | ||
| new CustomerGetOptions { Expand = ["discount.coupon.applies_to"] }); | ||
|
|
||
| var subscription = await subscriberService.GetSubscription(organization); | ||
|
|
||
| if (customer == null || subscription == null) | ||
| { | ||
| return OrganizationMetadata.Default with | ||
| { | ||
| OrganizationOccupiedSeats = orgOccupiedSeats.Total | ||
| }; | ||
| } | ||
|
|
||
| var isOnSecretsManagerStandalone = await IsOnSecretsManagerStandalone(organization, customer, subscription); | ||
|
|
||
| return new OrganizationMetadata( | ||
| isOnSecretsManagerStandalone, | ||
| orgOccupiedSeats.Total); | ||
| } | ||
|
|
||
| private async Task<bool> IsOnSecretsManagerStandalone( | ||
| Organization organization, | ||
| Customer? customer, | ||
| Subscription? subscription) | ||
| { | ||
| if (customer == null || subscription == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var plan = await pricingClient.GetPlanOrThrow(organization.PlanType); | ||
|
|
||
| if (!plan.SupportsSecretsManager) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var hasCoupon = customer.Discount?.Coupon?.Id == StripeConstants.CouponIDs.SecretsManagerStandalone; | ||
|
|
||
| if (!hasCoupon) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var subscriptionProductIds = subscription.Items.Data.Select(item => item.Plan.ProductId); | ||
|
|
||
| var couponAppliesTo = customer.Discount?.Coupon?.AppliesTo?.Products; | ||
|
|
||
| return subscriptionProductIds.Intersect(couponAppliesTo ?? []).Any(); | ||
| } | ||
| } |
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
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
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.
⛏️ I'm fine with removing this layer of response models when they do nothing but copy the Core models if you want to. Your call.
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.
Great suggestion; thanks! Especially now that it's only 2 properties, this class seems very superfluous now 😆 Removed in ddbf084