Skip to content

Commit 61ee65c

Browse files
authored
Fix admin newsletter trigger to run when scheduled sends are disabled (#453)
* Initial plan * Fix manual admin newsletter trigger when scheduling disabled --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 9789f72 commit 61ee65c

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

src/TechHub.Api/Services/NewsletterBackgroundService.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ private async Task RunManualAsync(CancellationToken ct)
104104
return;
105105
}
106106

107-
if (!_options.ScheduledSendEnabled)
108-
{
109-
_logger.LogInformation("Newsletter manual send skipped because ScheduledSendEnabled is false");
110-
return;
111-
}
112-
113107
await SendLatestRoundupsAsync(ct);
114108
}
115109

tests/TechHub.Api.Tests/Endpoints/NewsletterEndpointsTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,51 @@ await _client.PostAsJsonAsync(
9292
count.Should().Be(0);
9393
}
9494

95+
[Fact]
96+
public async Task AdminTriggerNewsletter_WhenScheduledSendingDisabled_StillProcessesManualSend()
97+
{
98+
await using var arrangeScope = _factory.Services.CreateAsyncScope();
99+
var arrangeConnection = arrangeScope.ServiceProvider.GetRequiredService<IDbConnection>();
100+
var roundupSlug = await arrangeConnection.ExecuteScalarAsync<string?>(
101+
"""
102+
SELECT slug
103+
FROM content_items
104+
WHERE collection_name = 'roundups'
105+
AND primary_section_name IS NOT NULL
106+
AND primary_section_name <> 'all'
107+
ORDER BY date_epoch DESC
108+
LIMIT 1
109+
""");
110+
111+
roundupSlug.Should().NotBeNullOrWhiteSpace();
112+
113+
var countBefore = await arrangeConnection.ExecuteScalarAsync<int>(
114+
"SELECT COUNT(*) FROM newsletter_send_log WHERE send_kind = 'weekly-roundup' AND target_key = @TargetKey",
115+
new { TargetKey = roundupSlug! });
116+
117+
var response = await _client.PostAsync("/api/admin/newsletter/trigger", null, TestContext.Current.CancellationToken);
118+
response.StatusCode.Should().Be(HttpStatusCode.Accepted);
119+
120+
var timedOut = true;
121+
for (var attempt = 0; attempt < 25; attempt++)
122+
{
123+
await Task.Delay(200, TestContext.Current.CancellationToken);
124+
await using var pollScope = _factory.Services.CreateAsyncScope();
125+
var pollConnection = pollScope.ServiceProvider.GetRequiredService<IDbConnection>();
126+
var countAfter = await pollConnection.ExecuteScalarAsync<int>(
127+
"SELECT COUNT(*) FROM newsletter_send_log WHERE send_kind = 'weekly-roundup' AND target_key = @TargetKey",
128+
new { TargetKey = roundupSlug! });
129+
130+
if (countAfter > countBefore)
131+
{
132+
timedOut = false;
133+
break;
134+
}
135+
}
136+
137+
timedOut.Should().BeFalse("manual admin trigger should execute even when scheduled sends are disabled");
138+
}
139+
95140
[Fact]
96141
public async Task Subscribe_WithOnlyInvalidSections_ReturnsBadRequest()
97142
{

0 commit comments

Comments
 (0)