1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00
This commit is contained in:
Jonas Hendrickx 2025-03-18 14:03:08 +01:00
parent 46ff82b84a
commit 39d344f742
4 changed files with 30 additions and 29 deletions

View File

@ -5,6 +5,14 @@ namespace Bit.Core.Billing.Services;
public interface IIndividualAutomaticTaxStrategy public interface IIndividualAutomaticTaxStrategy
{ {
/// <summary>
///
/// </summary>
/// <param name="subscription"></param>
/// <returns>
/// Returns <see cref="SubscriptionUpdateOptions" /> if changes are to be applied to the subscription, returns null
/// otherwise.
/// </returns>
SubscriptionUpdateOptions? GetUpdateOptions(Subscription subscription); SubscriptionUpdateOptions? GetUpdateOptions(Subscription subscription);
void SetCreateOptions(SubscriptionCreateOptions options, Customer customer); void SetCreateOptions(SubscriptionCreateOptions options, Customer customer);
void SetUpdateOptions(SubscriptionUpdateOptions options, Subscription subscription); void SetUpdateOptions(SubscriptionUpdateOptions options, Subscription subscription);

View File

@ -5,6 +5,14 @@ namespace Bit.Core.Billing.Services;
public interface IOrganizationAutomaticTaxStrategy public interface IOrganizationAutomaticTaxStrategy
{ {
/// <summary>
///
/// </summary>
/// <param name="subscription"></param>
/// <returns>
/// Returns <see cref="SubscriptionUpdateOptions" /> if changes are to be applied to the subscription, returns null
/// otherwise.
/// </returns>
Task<SubscriptionUpdateOptions?> GetUpdateOptionsAsync(Subscription subscription); Task<SubscriptionUpdateOptions?> GetUpdateOptionsAsync(Subscription subscription);
Task SetCreateOptionsAsync(SubscriptionCreateOptions options, Customer customer); Task SetCreateOptionsAsync(SubscriptionCreateOptions options, Customer customer);
Task SetUpdateOptionsAsync(SubscriptionUpdateOptions options, Subscription subscription); Task SetUpdateOptionsAsync(SubscriptionUpdateOptions options, Subscription subscription);

View File

@ -10,7 +10,7 @@ public class IndividualAutomaticTaxStrategy : IIndividualAutomaticTaxStrategy
{ {
options.AutomaticTax = new SubscriptionAutomaticTaxOptions options.AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = ShouldEnable(customer) Enabled = ShouldBeEnabled(customer)
}; };
} }
@ -18,14 +18,14 @@ public class IndividualAutomaticTaxStrategy : IIndividualAutomaticTaxStrategy
{ {
options.AutomaticTax = new SubscriptionAutomaticTaxOptions options.AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = ShouldEnable(subscription.Customer) Enabled = ShouldBeEnabled(subscription.Customer)
}; };
options.DefaultTaxRates = []; options.DefaultTaxRates = [];
} }
public SubscriptionUpdateOptions? GetUpdateOptions(Subscription subscription) public SubscriptionUpdateOptions? GetUpdateOptions(Subscription subscription)
{ {
if (subscription.AutomaticTax.Enabled == ShouldEnable(subscription.Customer)) if (subscription.AutomaticTax.Enabled == ShouldBeEnabled(subscription.Customer))
{ {
return null; return null;
} }
@ -34,7 +34,7 @@ public class IndividualAutomaticTaxStrategy : IIndividualAutomaticTaxStrategy
{ {
AutomaticTax = new SubscriptionAutomaticTaxOptions AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = ShouldEnable(subscription.Customer), Enabled = ShouldBeEnabled(subscription.Customer),
}, },
DefaultTaxRates = [] DefaultTaxRates = []
}; };
@ -42,7 +42,7 @@ public class IndividualAutomaticTaxStrategy : IIndividualAutomaticTaxStrategy
return options; return options;
} }
private static bool ShouldEnable(Customer customer) private static bool ShouldBeEnabled(Customer customer)
{ {
return customer.HasTaxLocationVerified(); return customer.HasTaxLocationVerified();
} }

View File

@ -20,19 +20,13 @@ public class OrganizationAutomaticTaxStrategy(
public async Task<SubscriptionUpdateOptions?> GetUpdateOptionsAsync(Subscription subscription) public async Task<SubscriptionUpdateOptions?> GetUpdateOptionsAsync(Subscription subscription)
{ {
ArgumentNullException.ThrowIfNull(subscription); var shouldBeEnabled = await ShouldBeEnabledAsync(subscription);
var isEnabled = await IsEnabledAsync(subscription);
if (!isEnabled.HasValue)
{
return null;
}
var options = new SubscriptionUpdateOptions var options = new SubscriptionUpdateOptions
{ {
AutomaticTax = new SubscriptionAutomaticTaxOptions AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = isEnabled.Value Enabled = shouldBeEnabled
}, },
DefaultTaxRates = [] DefaultTaxRates = []
}; };
@ -44,31 +38,27 @@ public class OrganizationAutomaticTaxStrategy(
{ {
options.AutomaticTax = new SubscriptionAutomaticTaxOptions options.AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = await IsEnabledAsync(options, customer) Enabled = await ShouldBeEnabledAsync(options, customer)
}; };
} }
public async Task SetUpdateOptionsAsync(SubscriptionUpdateOptions options, Subscription subscription) public async Task SetUpdateOptionsAsync(SubscriptionUpdateOptions options, Subscription subscription)
{ {
if (subscription.AutomaticTax.Enabled == options.AutomaticTax?.Enabled) var shouldBeEnabled = await ShouldBeEnabledAsync(subscription);
{
return;
}
var isEnabled = await IsEnabledAsync(subscription); if (subscription.AutomaticTax.Enabled == shouldBeEnabled)
if (!isEnabled.HasValue)
{ {
return; return;
} }
options.AutomaticTax = new SubscriptionAutomaticTaxOptions options.AutomaticTax = new SubscriptionAutomaticTaxOptions
{ {
Enabled = isEnabled.Value Enabled = shouldBeEnabled
}; };
options.DefaultTaxRates = []; options.DefaultTaxRates = [];
} }
private async Task<bool?> IsEnabledAsync(Subscription subscription) private async Task<bool> ShouldBeEnabledAsync(Subscription subscription)
{ {
if (!subscription.Customer.HasTaxLocationVerified()) if (!subscription.Customer.HasTaxLocationVerified())
{ {
@ -86,15 +76,10 @@ public class OrganizationAutomaticTaxStrategy(
shouldBeEnabled = subscription.Items.Select(item => item.Price.Id).Intersect(familyPriceIds).Any(); shouldBeEnabled = subscription.Items.Select(item => item.Price.Id).Intersect(familyPriceIds).Any();
} }
if (subscription.AutomaticTax.Enabled != shouldBeEnabled) return shouldBeEnabled;
{
return shouldBeEnabled;
}
return null;
} }
private async Task<bool?> IsEnabledAsync(SubscriptionCreateOptions options, Customer customer) private async Task<bool> ShouldBeEnabledAsync(SubscriptionCreateOptions options, Customer customer)
{ {
if (!customer.HasTaxLocationVerified()) if (!customer.HasTaxLocationVerified())
{ {