fix(bot): publish wizard-created sessions
PR Checks / test-and-build (pull_request) Successful in 11m58s
PR Checks / test-and-build (pull_request) Successful in 11m58s
After the shared create handler persists sessions, create a Telegram topic when needed, send the schedule/signup message, and store thread_id/batch_message_id/topic_created_by_bot for the batch. Add a Testcontainers regression test for the wizard SubmitDraftAsync happy path. Bump version to 3.9.9.
This commit is contained in:
@@ -5,11 +5,13 @@ using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using Telegram.Bot.Types;
|
||||
using SharedCreateSessionHandler = GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler;
|
||||
|
||||
@@ -31,17 +33,23 @@ public sealed class CreateSessionHandler
|
||||
private readonly SharedCreateSessionHandler _shared;
|
||||
private readonly IWizardMessenger _messenger;
|
||||
private readonly ILogger<CreateSessionHandler> _log;
|
||||
private readonly IPlatformMessenger? _platformMessenger;
|
||||
private readonly NpgsqlDataSource? _dataSource;
|
||||
|
||||
public CreateSessionHandler(
|
||||
IWizardDraftRepository drafts,
|
||||
SharedCreateSessionHandler shared,
|
||||
IWizardMessenger messenger,
|
||||
ILogger<CreateSessionHandler> log)
|
||||
ILogger<CreateSessionHandler> log,
|
||||
IPlatformMessenger? platformMessenger = null,
|
||||
NpgsqlDataSource? dataSource = null)
|
||||
{
|
||||
_drafts = drafts;
|
||||
_shared = shared;
|
||||
_messenger = messenger;
|
||||
_log = log;
|
||||
_platformMessenger = platformMessenger;
|
||||
_dataSource = dataSource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -106,19 +114,24 @@ public sealed class CreateSessionHandler
|
||||
}
|
||||
|
||||
var commands = BuildCommands(draft, payload);
|
||||
var created = new List<(CreateSessionCommand Command, CreateSessionResult Result)>();
|
||||
try
|
||||
{
|
||||
foreach (var cmd in commands)
|
||||
{
|
||||
await _shared.HandleAsync(cmd, ct);
|
||||
var result = await _shared.HandleAsync(cmd, ct);
|
||||
if (!result.Success)
|
||||
{
|
||||
await _messenger.EditDraftMessageAsync(
|
||||
draft,
|
||||
result.ErrorMessage ?? "❌ Не удалось создать сессию.",
|
||||
Array.Empty<WizardAction>(),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
created.Add((cmd, result));
|
||||
}
|
||||
var totalSessions = commands.Sum(c => c.ScheduledTimes.Count);
|
||||
await _messenger.EditDraftMessageAsync(
|
||||
draft,
|
||||
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}",
|
||||
Array.Empty<WizardAction>(),
|
||||
ct);
|
||||
await _drafts.DeleteAsync(draft.Id, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -142,9 +155,89 @@ public sealed class CreateSessionHandler
|
||||
$"💥 Ошибка: {ex.Message}\nПопытка {payload.RetryCount}/{MaxRetries}.",
|
||||
RetryCancelActions(),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var totalSessions = created.Sum(c => c.Command.ScheduledTimes.Count);
|
||||
try
|
||||
{
|
||||
foreach (var item in created)
|
||||
{
|
||||
await PublishCreatedSessionAsync(item.Command, item.Result, ct);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex, "SubmitDraftAsync created draft {DraftId} but failed to publish schedule", draft.Id);
|
||||
await _messenger.EditDraftMessageAsync(
|
||||
draft,
|
||||
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}, но не удалось опубликовать сообщение для записи: {ex.Message}",
|
||||
Array.Empty<WizardAction>(),
|
||||
ct);
|
||||
await _drafts.DeleteAsync(draft.Id, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await _messenger.EditDraftMessageAsync(
|
||||
draft,
|
||||
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}",
|
||||
Array.Empty<WizardAction>(),
|
||||
ct);
|
||||
await _drafts.DeleteAsync(draft.Id, ct);
|
||||
}
|
||||
|
||||
private async Task PublishCreatedSessionAsync(CreateSessionCommand command, CreateSessionResult result, CancellationToken ct)
|
||||
{
|
||||
if (_platformMessenger is null || _dataSource is null)
|
||||
{
|
||||
throw new InvalidOperationException("Session publication dependencies are not configured.");
|
||||
}
|
||||
|
||||
if (result.View is null || result.BatchId is null)
|
||||
{
|
||||
throw new InvalidOperationException("Created session result does not contain publication data.");
|
||||
}
|
||||
|
||||
var group = command.Group;
|
||||
var topicCreatedByBot = false;
|
||||
if (string.IsNullOrWhiteSpace(group.ExternalThreadId))
|
||||
{
|
||||
var thread = await _platformMessenger.CreateThreadAsync(group, command.Title, ct);
|
||||
group = group with { ExternalThreadId = thread.ExternalThreadId };
|
||||
topicCreatedByBot = true;
|
||||
}
|
||||
|
||||
var scheduleMessage = await _platformMessenger.SendScheduleAsync(
|
||||
new PlatformScheduleMessage(group, result.View, ExistingMessage: null, command.ImageReference),
|
||||
ct);
|
||||
|
||||
await using var connection = await _dataSource.OpenConnectionAsync(ct);
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET thread_id = @ThreadId,
|
||||
batch_message_id = @BatchMessageId,
|
||||
topic_created_by_bot = @TopicCreatedByBot,
|
||||
updated_at = now()
|
||||
WHERE batch_id = @BatchId
|
||||
""",
|
||||
new
|
||||
{
|
||||
result.BatchId,
|
||||
ThreadId = ParseNullableInt(group.ExternalThreadId),
|
||||
BatchMessageId = ParseInt(scheduleMessage.ExternalMessageId),
|
||||
TopicCreatedByBot = topicCreatedByBot
|
||||
});
|
||||
}
|
||||
|
||||
private static int ParseInt(string value) =>
|
||||
int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
|
||||
private static int? ParseNullableInt(string? value) =>
|
||||
string.IsNullOrWhiteSpace(value)
|
||||
? null
|
||||
: int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
|
||||
// ── Build shared commands ────────────────────────────────────────
|
||||
// The shared handler creates one session per scheduled time in a
|
||||
// single transaction and assigns the same batch_id to all of them.
|
||||
|
||||
Reference in New Issue
Block a user