Skip to content

Commit

Permalink
Fix error in remove org from provider
Browse files Browse the repository at this point in the history
  • Loading branch information
amorask-bitwarden committed May 13, 2024
1 parent 0018288 commit 734e77c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ public class RemoveOrganizationFromProviderCommand : IRemoveOrganizationFromProv
Provider provider,
IEnumerable<string> organizationOwnerEmails)
{
if (!organization.IsStripeEnabled())
{
return;
}

var isConsolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling);

if (isConsolidatedBillingEnabled &&
provider.Status == ProviderStatusType.Billable &&
organization.Status == OrganizationStatusType.Managed)
organization.Status == OrganizationStatusType.Managed &&
!string.IsNullOrEmpty(organization.GatewayCustomerId))
{
await _stripeAdapter.CustomerUpdateAsync(organization.GatewayCustomerId, new CustomerUpdateOptions
{
Description = string.Empty,
Email = organization.BillingEmail
});

var plan = StaticStore.GetPlan(organization.PlanType).PasswordManager;

var subscriptionCreateOptions = new SubscriptionCreateOptions
Expand All @@ -129,26 +131,23 @@ public class RemoveOrganizationFromProviderCommand : IRemoveOrganizationFromProv
var subscription = await _stripeAdapter.SubscriptionCreateAsync(subscriptionCreateOptions);

organization.GatewaySubscriptionId = subscription.Id;
organization.Status = OrganizationStatusType.Created;

await _providerBillingService.ScaleSeats(provider, organization.PlanType, -organization.Seats ?? 0);
}
else
else if (organization.IsStripeEnabled())
{
var customerUpdateOptions = new CustomerUpdateOptions
await _stripeAdapter.CustomerUpdateAsync(organization.GatewayCustomerId, new CustomerUpdateOptions
{
Coupon = string.Empty,
Email = organization.BillingEmail
};

await _stripeAdapter.CustomerUpdateAsync(organization.GatewayCustomerId, customerUpdateOptions);
});

var subscriptionUpdateOptions = new SubscriptionUpdateOptions
await _stripeAdapter.SubscriptionUpdateAsync(organization.GatewaySubscriptionId, new SubscriptionUpdateOptions
{
CollectionMethod = StripeConstants.CollectionMethod.SendInvoice,
DaysUntilDue = 30
};

await _stripeAdapter.SubscriptionUpdateAsync(organization.GatewaySubscriptionId, subscriptionUpdateOptions);
});

await _subscriberService.RemovePaymentMethod(organization);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,29 @@ public class RemoveOrganizationFromProviderCommandTests
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();

organizationRepository.GetOwnerEmailAddressesById(organization.Id).Returns([
"a@b.com",
"b@b.com"
"a@example.com",
"b@example.com"
]);

await sutProvider.Sut.RemoveOrganizationFromProvider(provider, providerOrganization, organization);

await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(org => org.BillingEmail == "a@b.com"));
await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(org => org.BillingEmail == "a@example.com"));

await sutProvider.GetDependency<IProviderOrganizationRepository>().Received(1)
.DeleteAsync(providerOrganization);

await sutProvider.GetDependency<IEventService>().Received(1)
.LogProviderOrganizationEventAsync(providerOrganization, EventType.ProviderOrganization_Removed);

sutProvider.GetDependency<IFeatureService>().DidNotReceiveWithAnyArgs().IsEnabled(Arg.Any<string>());
await sutProvider.GetDependency<IMailService>().Received(1)
.SendProviderUpdatePaymentMethod(
organization.Id,
organization.Name,
provider.Name,
Arg.Is<IEnumerable<string>>(emails => emails.FirstOrDefault() == "a@example.com"));

await sutProvider.GetDependency<IStripeAdapter>().DidNotReceiveWithAnyArgs()
.CustomerUpdateAsync(Arg.Any<string>(), Arg.Any<CustomerUpdateOptions>());
}

[Theory, BitAutoData]
Expand All @@ -141,8 +149,8 @@ await sutProvider.GetDependency<IEventService>().Received(1)
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();

organizationRepository.GetOwnerEmailAddressesById(organization.Id).Returns([
"a@b.com",
"b@b.com"
"a@example.com",
"b@example.com"
]);

sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
Expand All @@ -154,7 +162,7 @@ await sutProvider.GetDependency<IEventService>().Received(1)

await stripeAdapter.Received(1).CustomerUpdateAsync(organization.GatewayCustomerId,
Arg.Is<CustomerUpdateOptions>(options =>
options.Coupon == string.Empty && options.Email == "a@b.com"));
options.Coupon == string.Empty && options.Email == "a@example.com"));

await stripeAdapter.Received(1).SubscriptionUpdateAsync(organization.GatewaySubscriptionId,
Arg.Is<SubscriptionUpdateOptions>(options =>
Expand All @@ -163,13 +171,20 @@ await sutProvider.GetDependency<IEventService>().Received(1)

await sutProvider.GetDependency<ISubscriberService>().Received(1).RemovePaymentMethod(organization);

await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(org => org.BillingEmail == "a@b.com"));
await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(org => org.BillingEmail == "a@example.com"));

await sutProvider.GetDependency<IProviderOrganizationRepository>().Received(1)
.DeleteAsync(providerOrganization);

await sutProvider.GetDependency<IEventService>().Received(1)
.LogProviderOrganizationEventAsync(providerOrganization, EventType.ProviderOrganization_Removed);

await sutProvider.GetDependency<IMailService>().Received(1)
.SendProviderUpdatePaymentMethod(
organization.Id,
organization.Name,
provider.Name,
Arg.Is<IEnumerable<string>>(emails => emails.FirstOrDefault() == "a@example.com"));
}

[Theory, BitAutoData]
Expand Down Expand Up @@ -198,8 +213,8 @@ await sutProvider.GetDependency<IEventService>().Received(1)
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();

organizationRepository.GetOwnerEmailAddressesById(organization.Id).Returns([
"a@b.com",
"b@b.com"
"a@example.com",
"b@example.com"
]);

sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
Expand Down Expand Up @@ -228,12 +243,23 @@ await sutProvider.GetDependency<IEventService>().Received(1)
await sutProvider.GetDependency<IProviderBillingService>().Received(1)
.ScaleSeats(provider, organization.PlanType, -organization.Seats ?? 0);

await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(org => org.BillingEmail == "a@b.com" && organization.GatewaySubscriptionId == "subscription_id"));
await organizationRepository.Received(1).ReplaceAsync(Arg.Is<Organization>(
org =>
org.BillingEmail == "a@example.com" &&
org.GatewaySubscriptionId == "subscription_id" &&
org.Status == OrganizationStatusType.Created));

await sutProvider.GetDependency<IProviderOrganizationRepository>().Received(1)
.DeleteAsync(providerOrganization);

await sutProvider.GetDependency<IEventService>().Received(1)
.LogProviderOrganizationEventAsync(providerOrganization, EventType.ProviderOrganization_Removed);

await sutProvider.GetDependency<IMailService>().Received(1)
.SendProviderUpdatePaymentMethod(
organization.Id,
organization.Name,
provider.Name,
Arg.Is<IEnumerable<string>>(emails => emails.FirstOrDefault() == "a@example.com"));
}
}

0 comments on commit 734e77c

Please sign in to comment.