-
-
Notifications
You must be signed in to change notification settings - Fork 508
Fix duplicate plan limit overage emails #2269
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
Open
niemyjski
wants to merge
1
commit into
main
Choose a base branch
from
niemyjski/fix-plan-limit-email-spam
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.
Open
Changes from all commits
Commits
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
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: 16 additions & 2 deletions
18
src/Exceptionless.Core/Models/WorkItems/OrganizationNotificationWorkItem.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,8 +1,22 @@ | ||
| namespace Exceptionless.Core.Models.WorkItems; | ||
| using Foundatio.Queues; | ||
|
|
||
| public record OrganizationNotificationWorkItem | ||
| namespace Exceptionless.Core.Models.WorkItems; | ||
|
|
||
| public record OrganizationNotificationWorkItem : IHaveUniqueIdentifier | ||
| { | ||
| public const string HourlyNotificationType = "hourly"; | ||
| public const string MonthlyNotificationType = "monthly"; | ||
|
|
||
| public required string OrganizationId { get; init; } | ||
| public required bool IsOverHourlyLimit { get; init; } | ||
| public required bool IsOverMonthlyLimit { get; init; } | ||
|
|
||
| public string NotificationType => IsOverMonthlyLimit ? MonthlyNotificationType : HourlyNotificationType; | ||
|
|
||
| public string? UniqueIdentifier => GetNotificationKey(OrganizationId, NotificationType); | ||
|
|
||
| public static string GetNotificationKey(string organizationId, string notificationType) | ||
| { | ||
| return $"Organization:{organizationId}:notification:{notificationType}"; | ||
| } | ||
| } |
185 changes: 185 additions & 0 deletions
185
...ss.Tests/Jobs/WorkItemHandlers/OrganizationNotificationWorkItemHandlerIntegrationTests.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,185 @@ | ||
| using Exceptionless.Core; | ||
| using Exceptionless.Core.Billing; | ||
| using Exceptionless.Core.Extensions; | ||
| using Exceptionless.Core.Jobs.WorkItemHandlers; | ||
| using Exceptionless.Core.Mail; | ||
| using Exceptionless.Core.Models; | ||
| using Exceptionless.Core.Models.WorkItems; | ||
| using Exceptionless.Core.Repositories; | ||
| using Exceptionless.Tests.Mail; | ||
| using Exceptionless.Tests.Utility; | ||
| using Foundatio.Caching; | ||
| using Foundatio.Jobs; | ||
| using Foundatio.Repositories; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Xunit; | ||
|
|
||
| namespace Exceptionless.Tests.Jobs.WorkItemHandlers; | ||
|
|
||
| public class OrganizationNotificationWorkItemHandlerIntegrationTests : IntegrationTestsBase | ||
| { | ||
| private const string PrimaryOrganizationId = "664ec4c1f12e4f2b7a0d1001"; | ||
| private const string SecondaryOrganizationId = "664ec4c1f12e4f2b7a0d1002"; | ||
| private const string PrimaryUserId = "664ec4c1f12e4f2b7a0d2001"; | ||
| private const string SecondaryUserId = "664ec4c1f12e4f2b7a0d2002"; | ||
|
|
||
| public OrganizationNotificationWorkItemHandlerIntegrationTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory) { } | ||
|
|
||
| private CountingMailer Mailer => GetService<CountingMailer>(); | ||
| private ICacheClient CacheClient => GetService<ICacheClient>(); | ||
| private OrganizationNotificationWorkItemHandler Handler => GetService<OrganizationNotificationWorkItemHandler>(); | ||
| private IOrganizationRepository OrganizationRepository => GetService<IOrganizationRepository>(); | ||
| private IUserRepository UserRepository => GetService<IUserRepository>(); | ||
| private BillingManager BillingManager => GetService<BillingManager>(); | ||
| private BillingPlans BillingPlans => GetService<BillingPlans>(); | ||
| private OrganizationData OrganizationData => GetService<OrganizationData>(); | ||
| private UserData UserData => GetService<UserData>(); | ||
|
|
||
| protected override void RegisterServices(IServiceCollection services) | ||
| { | ||
| base.RegisterServices(services); | ||
| services.AddSingleton<CountingMailer>(); | ||
| services.ReplaceSingleton<IMailer>(sp => sp.GetRequiredService<CountingMailer>()); | ||
| } | ||
|
|
||
| protected override async Task ResetDataAsync() | ||
| { | ||
| await base.ResetDataAsync(); | ||
| Mailer.Reset(); | ||
|
|
||
| var organizations = new[] | ||
| { | ||
| OrganizationData.GenerateOrganization(BillingManager, BillingPlans, id: PrimaryOrganizationId, name: "Primary Organization"), | ||
| OrganizationData.GenerateOrganization(BillingManager, BillingPlans, id: SecondaryOrganizationId, name: "Secondary Organization") | ||
| }; | ||
| await OrganizationRepository.AddAsync(organizations, options => options.ImmediateConsistency()); | ||
|
|
||
| var primaryUser = UserData.GenerateUser(id: PrimaryUserId, organizationId: PrimaryOrganizationId, emailAddress: "primary-owner@example.org"); | ||
| primaryUser.FullName = "Primary Owner"; | ||
| primaryUser.IsEmailAddressVerified = true; | ||
|
|
||
| var secondaryUser = UserData.GenerateUser(id: SecondaryUserId, organizationId: SecondaryOrganizationId, emailAddress: "secondary-owner@example.org"); | ||
| secondaryUser.FullName = "Secondary Owner"; | ||
| secondaryUser.IsEmailAddressVerified = true; | ||
|
|
||
| await UserRepository.AddAsync([primaryUser, secondaryUser], options => options.ImmediateConsistency()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenDuplicateMonthlyNotificationsAreProcessedAcrossHours_ShouldSendOneEmail() | ||
| { | ||
| // Arrange | ||
| // Act | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| TimeProvider.Advance(TimeSpan.FromHours(1).Add(TimeSpan.FromMinutes(1))); | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| TimeProvider.Advance(TimeSpan.FromHours(1).Add(TimeSpan.FromMinutes(1))); | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(1, Mailer.OrganizationNoticeCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenMonthlyNotificationsTargetDifferentOrganizations_ShouldSendOneEmailPerOrganization() | ||
| { | ||
| // Arrange | ||
| // Act | ||
| for (int i = 0; i < 3; i++) | ||
| { | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(SecondaryOrganizationId)); | ||
| } | ||
|
|
||
| // Assert | ||
| var callsByOrganization = Mailer.OrganizationNoticeCalls | ||
| .GroupBy(call => call.OrganizationId) | ||
| .ToDictionary(group => group.Key, group => group.Count()); | ||
|
|
||
| Assert.Equal(2, callsByOrganization.Count); | ||
| Assert.Equal(1, callsByOrganization[PrimaryOrganizationId]); | ||
| Assert.Equal(1, callsByOrganization[SecondaryOrganizationId]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenTwentyFourHoursPass_ShouldAllowANewMonthlyNotification() | ||
| { | ||
| // Arrange | ||
| // Act | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| TimeProvider.Advance(TimeSpan.FromHours(25)); | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(2, Mailer.OrganizationNoticeCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenOnlyAnHourlyOverageIsProcessed_ShouldNotSendEmail() | ||
| { | ||
| // Arrange | ||
| // Act | ||
| await HandleWorkItemAsync(CreateHourlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(0, Mailer.OrganizationNoticeCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenTheMonthlySentMarkerAlreadyExists_ShouldNotSendEmail() | ||
| { | ||
| // Arrange | ||
| await CacheClient.SetAsync( | ||
| OrganizationNotificationWorkItemHandler.GetNotificationSentKey(PrimaryOrganizationId, OrganizationNotificationWorkItem.MonthlyNotificationType), | ||
| true, | ||
| TimeSpan.FromHours(24)); | ||
|
|
||
| // Act | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(0, Mailer.OrganizationNoticeCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HandleItemAsync_WhenHourlyOveragePrecedesMonthlyOverage_ShouldSendTheMonthlyEmail() | ||
| { | ||
| // Arrange | ||
| // Act | ||
| await HandleWorkItemAsync(CreateHourlyNotificationWorkItem(PrimaryOrganizationId)); | ||
| await HandleWorkItemAsync(CreateMonthlyNotificationWorkItem(PrimaryOrganizationId)); | ||
|
|
||
| // Assert | ||
| Assert.Equal(1, Mailer.OrganizationNoticeCount); | ||
| } | ||
|
|
||
| private async Task HandleWorkItemAsync(OrganizationNotificationWorkItem workItem) | ||
| { | ||
| await using var workItemLock = await Handler.GetWorkItemLockAsync(workItem, TestContext.Current.CancellationToken); | ||
| var context = new WorkItemContext(workItem, "test-job", workItemLock, TestContext.Current.CancellationToken, static (_, _) => Task.CompletedTask); | ||
| await Handler.HandleItemAsync(context); | ||
| } | ||
|
|
||
| private static OrganizationNotificationWorkItem CreateMonthlyNotificationWorkItem(string organizationId) | ||
| { | ||
| return new OrganizationNotificationWorkItem | ||
| { | ||
| OrganizationId = organizationId, | ||
| IsOverHourlyLimit = false, | ||
| IsOverMonthlyLimit = true | ||
| }; | ||
| } | ||
|
|
||
| private static OrganizationNotificationWorkItem CreateHourlyNotificationWorkItem(string organizationId) | ||
| { | ||
| return new OrganizationNotificationWorkItem | ||
| { | ||
| OrganizationId = organizationId, | ||
| IsOverHourlyLimit = true, | ||
| IsOverMonthlyLimit = false | ||
| }; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.