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
{
/// <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);
void SetCreateOptions(SubscriptionCreateOptions options, Customer customer);
void SetUpdateOptions(SubscriptionUpdateOptions options, Subscription subscription);

View File

@ -5,6 +5,14 @@ namespace Bit.Core.Billing.Services;
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 SetCreateOptionsAsync(SubscriptionCreateOptions options, Customer customer);
Task SetUpdateOptionsAsync(SubscriptionUpdateOptions options, Subscription subscription);

View File

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

View File

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