Skip to content

Commit

Permalink
Refactoring dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Jan 14, 2024
1 parent f9704b9 commit 195b440
Show file tree
Hide file tree
Showing 77 changed files with 653 additions and 873 deletions.
7 changes: 0 additions & 7 deletions Inc.TeamAssistant.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inc.TeamAssistant.Holidays"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inc.TeamAssistant.HolidaysTests", "tests\Inc.TeamAssistant.HolidaysTests\Inc.TeamAssistant.HolidaysTests.csproj", "{39417B68-C33C-411E-8478-DD9C081A640A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inc.TeamAssistant.DialogContinuations", "src\Inc.TeamAssistant.DialogContinuations\Inc.TeamAssistant.DialogContinuations.csproj", "{D0D129F3-79DF-4DDC-B54C-F272582D78D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inc.TeamAssistant.Reviewer.Domain", "src\Inc.TeamAssistant.Reviewer.Domain\Inc.TeamAssistant.Reviewer.Domain.csproj", "{529CC802-3552-4381-8D22-872890CC6A9E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inc.TeamAssistant.Reviewer.DataAccess", "src\Inc.TeamAssistant.Reviewer.DataAccess\Inc.TeamAssistant.Reviewer.DataAccess.csproj", "{0BCBE76B-F740-44AE-8D47-5DD735EA6F3D}"
Expand Down Expand Up @@ -160,10 +158,6 @@ Global
{39417B68-C33C-411E-8478-DD9C081A640A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39417B68-C33C-411E-8478-DD9C081A640A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39417B68-C33C-411E-8478-DD9C081A640A}.Release|Any CPU.Build.0 = Release|Any CPU
{D0D129F3-79DF-4DDC-B54C-F272582D78D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0D129F3-79DF-4DDC-B54C-F272582D78D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0D129F3-79DF-4DDC-B54C-F272582D78D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0D129F3-79DF-4DDC-B54C-F272582D78D6}.Release|Any CPU.Build.0 = Release|Any CPU
{529CC802-3552-4381-8D22-872890CC6A9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{529CC802-3552-4381-8D22-872890CC6A9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{529CC802-3552-4381-8D22-872890CC6A9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -219,7 +213,6 @@ Global
{CA295F9A-5F4D-4616-992A-340A8C0A0D9A} = {2819C5EE-D36B-4BE5-95D3-80BF8869051D}
{2A6D584C-D381-4405-9DDD-51D5AEF6AC2E} = {2819C5EE-D36B-4BE5-95D3-80BF8869051D}
{39417B68-C33C-411E-8478-DD9C081A640A} = {1943C11E-7A4A-4300-BDC1-DA333BD3EBED}
{D0D129F3-79DF-4DDC-B54C-F272582D78D6} = {2819C5EE-D36B-4BE5-95D3-80BF8869051D}
{529CC802-3552-4381-8D22-872890CC6A9E} = {70E979AF-B6E2-4044-9161-91FA02A2644F}
{0BCBE76B-F740-44AE-8D47-5DD735EA6F3D} = {70E979AF-B6E2-4044-9161-91FA02A2644F}
{CC45D751-DEF6-47A9-BBB3-9F46AA1F2A18} = {70E979AF-B6E2-4044-9161-91FA02A2644F}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ namespace Inc.TeamAssistant.Appraiser.Application.CommandHandlers.AcceptEstimate

internal sealed class AcceptEstimateCommandCreator : ICommandCreator
{
private readonly string _command = "/accept?storyId=";
public string Command => "/accept?storyId=";

public int Priority => 3;

public Task<IRequest<CommandResult>?> Create(MessageContext messageContext, CancellationToken token)
public Task<IRequest<CommandResult>> Create(
MessageContext messageContext,
Guid? selectedTeamId,
CancellationToken token)
{
if (messageContext is null)
throw new ArgumentNullException(nameof(messageContext));

if (messageContext.Text.StartsWith(_command, StringComparison.InvariantCultureIgnoreCase))
{
var storyId = Guid.Parse(messageContext.Text.Replace(_command, string.Empty));
return Task.FromResult<IRequest<CommandResult>?>(new AcceptEstimateCommand(messageContext, storyId));
}

return Task.FromResult<IRequest<CommandResult>?>(null);
var storyId = Guid.Parse(messageContext.Text.Replace(Command, string.Empty));
return Task.FromResult<IRequest<CommandResult>>(new AcceptEstimateCommand(messageContext, storyId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@
using MediatR;
using Inc.TeamAssistant.Appraiser.Application.Services;
using Inc.TeamAssistant.Appraiser.Domain;
using Inc.TeamAssistant.DialogContinuations;
using Inc.TeamAssistant.Primitives;

namespace Inc.TeamAssistant.Appraiser.Application.CommandHandlers.AddStory;

internal sealed class AddStoryCommandHandler : IRequestHandler<AddStoryCommand, CommandResult>
{
private readonly IStoryRepository _storyRepository;
private readonly IDialogContinuation<BotCommandStage> _dialogContinuation;
private readonly SummaryByStoryBuilder _summaryByStoryBuilder;
private readonly IMessagesSender _messagesSender;

public AddStoryCommandHandler(
IStoryRepository storyRepository,
IDialogContinuation<BotCommandStage> dialogContinuation,
SummaryByStoryBuilder summaryByStoryBuilder,
IMessagesSender messagesSender)
{
_storyRepository = storyRepository ?? throw new ArgumentNullException(nameof(storyRepository));
_dialogContinuation = dialogContinuation ?? throw new ArgumentNullException(nameof(dialogContinuation));
_summaryByStoryBuilder = summaryByStoryBuilder ?? throw new ArgumentNullException(nameof(summaryByStoryBuilder));
_messagesSender = messagesSender ?? throw new ArgumentNullException(nameof(messagesSender));
}
Expand All @@ -48,25 +44,8 @@ public async Task<CommandResult> Handle(AddStoryCommand command, CancellationTok

await _storyRepository.Upsert(story, token);

var dialogState = _dialogContinuation.TryEnd(command.MessageContext.PersonId, BotCommandStage.EnterText);
var notifications = new List<NotificationMessage>
{
await _summaryByStoryBuilder.Build(SummaryByStoryConverter.ConvertTo(story))
};

if (dialogState is not null)
{
if (command.MessageContext.Shared)
dialogState.TryAttachMessage(new ChatMessage(
command.MessageContext.ChatId,
command.MessageContext.MessageId));

if (dialogState.ChatMessages.Any())
notifications.Add(NotificationMessage.Delete(dialogState.ChatMessages));
}

await _messagesSender.StoryChanged(story.TeamId);

return CommandResult.Build(notifications.ToArray());
return CommandResult.Build(await _summaryByStoryBuilder.Build(SummaryByStoryConverter.ConvertTo(story)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using FluentValidation;
using FluentValidation.Results;
using Inc.TeamAssistant.Appraiser.Model.Commands.AddStory;
using Inc.TeamAssistant.DialogContinuations;
using Inc.TeamAssistant.Primitives;
using MediatR;

Expand All @@ -12,61 +11,56 @@ internal sealed class AddStoryCommandCreator : ICommandCreator
{
private readonly IMessageBuilder _messageBuilder;
private readonly ITeamAccessor _teamAccessor;
private readonly IDialogContinuation<BotCommandStage> _dialogContinuation;
private readonly AddStoryToAssessmentSessionOptions _options;

private readonly string _command = "/add";
private readonly BotCommandStage _commandStage = BotCommandStage.EnterText;

public int Priority => 3;
public string Command => "/add";

public AddStoryCommandCreator(
IMessageBuilder messageBuilder,
ITeamAccessor teamAccessor,
IDialogContinuation<BotCommandStage> dialogContinuation,
AddStoryToAssessmentSessionOptions options)
{
_messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder));
_teamAccessor = teamAccessor ?? throw new ArgumentNullException(nameof(teamAccessor));
_dialogContinuation = dialogContinuation ?? throw new ArgumentNullException(nameof(dialogContinuation));
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public async Task<IRequest<CommandResult>?> Create(MessageContext messageContext, CancellationToken token)
public async Task<IRequest<CommandResult>> Create(
MessageContext messageContext,
Guid? selectedTeamId,
CancellationToken token)
{
if (messageContext is null)
throw new ArgumentNullException(nameof(messageContext));

var dialogState = _dialogContinuation.Find(messageContext.PersonId);
if (messageContext.Cmd.Equals(_command, StringComparison.InvariantCultureIgnoreCase) &&
messageContext.CurrentCommandStage == _commandStage &&
dialogState is not null)
{
if (messageContext.Text.StartsWith("/"))
throw new ValidationException(new[]
{
new ValidationFailure(
"Command",
await _messageBuilder.Build(Messages.Appraiser_StoryTitleIsEmpty, messageContext.LanguageId))
});

var teamId = Guid.Parse(dialogState.Data[1]);
var teammates = await _teamAccessor.GetTeammates(teamId, token);
var storyItems = messageContext.Text.Split(' ');
var storyTitleBuilder = new StringBuilder();
var links = new List<string>();

foreach (var storyItem in storyItems)
if (messageContext.Text.StartsWith("/"))
throw new ValidationException(new[]
{
if (_options.LinksPrefix.Any(l => storyItem.StartsWith(l, StringComparison.InvariantCultureIgnoreCase)))
links.Add(storyItem.ToLower().Trim());
else
storyTitleBuilder.Append($"{storyItem} ");
}

return new AddStoryCommand(messageContext, teamId, storyTitleBuilder.ToString(), links, teammates);
}
new ValidationFailure(
"Command",
await _messageBuilder.Build(Messages.Appraiser_StoryTitleIsEmpty, messageContext.LanguageId))
});
if (!selectedTeamId.HasValue)
throw new ApplicationException("Team was not selected.");

var teammates = await _teamAccessor.GetTeammates(selectedTeamId.Value, token);
var storyItems = messageContext.Text.Split(' ');
var storyTitleBuilder = new StringBuilder();
var links = new List<string>();

return null;
foreach (var storyItem in storyItems)
{
if (_options.LinksPrefix.Any(l => storyItem.StartsWith(l, StringComparison.InvariantCultureIgnoreCase)))
links.Add(storyItem.ToLower().Trim());
else
storyTitleBuilder.Append($"{storyItem} ");
}

return new AddStoryCommand(
messageContext,
selectedTeamId.Value,
storyTitleBuilder.ToString(),
links,
teammates);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ namespace Inc.TeamAssistant.Appraiser.Application.CommandHandlers.ReVoteEstimate

internal sealed class ReVoteEstimateCommandCreator : ICommandCreator
{
private readonly string _command = "/revote?storyId=";
public string Command => "/revote?storyId=";

public int Priority => 3;

public Task<IRequest<CommandResult>?> Create(MessageContext messageContext, CancellationToken token)
public Task<IRequest<CommandResult>> Create(
MessageContext messageContext,
Guid? selectedTeamId,
CancellationToken token)
{
if (messageContext is null)
throw new ArgumentNullException(nameof(messageContext));

if (messageContext is null)
throw new ArgumentNullException(nameof(messageContext));

if (messageContext.Text.StartsWith(_command, StringComparison.InvariantCultureIgnoreCase))
{
var storyId = Guid.Parse(messageContext.Text.Replace(_command, string.Empty, StringComparison.InvariantCultureIgnoreCase));
return Task.FromResult<IRequest<CommandResult>?>(new ReVoteEstimateCommand(messageContext, storyId));
}

return Task.FromResult<IRequest<CommandResult>?>(null);
var storyId = Guid.Parse(messageContext.Text.Replace(Command, string.Empty, StringComparison.InvariantCultureIgnoreCase));
return Task.FromResult<IRequest<CommandResult>>(new ReVoteEstimateCommand(messageContext, storyId));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Inc.TeamAssistant.Appraiser.Domain;
using Inc.TeamAssistant.Appraiser.Model.Commands.SetEstimateForStory;
using Inc.TeamAssistant.Primitives;
using MediatR;
Expand All @@ -7,33 +6,32 @@ namespace Inc.TeamAssistant.Appraiser.Application.CommandHandlers.SetEstimateFor

internal sealed class SetEstimateForStoryCommandCreator : ICommandCreator
{
private static readonly IReadOnlyCollection<string> Commands = AssessmentValue.GetAssessments
.Select(a => a.ToString())
.ToArray();

public int Priority => 3;
public string Command { get; }

public SetEstimateForStoryCommandCreator(string command)
{
if (string.IsNullOrWhiteSpace(command))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(command));

Command = command;
}

public Task<IRequest<CommandResult>?> Create(MessageContext messageContext, CancellationToken token)
public Task<IRequest<CommandResult>> Create(
MessageContext messageContext,
Guid? selectedTeamId,
CancellationToken token)
{
if (messageContext is null)
throw new ArgumentNullException(nameof(messageContext));

foreach (var command in Commands)
{
if (messageContext.Cmd.StartsWith($"/{command}", StringComparison.InvariantCultureIgnoreCase))
{
var storyId = Guid.Parse(messageContext.Cmd.Replace(
$"/{command}?storyId=",
string.Empty,
StringComparison.InvariantCultureIgnoreCase));
var storyId = Guid.Parse(messageContext.Text.Replace(
Command,
string.Empty,
StringComparison.InvariantCultureIgnoreCase));

return Task.FromResult<IRequest<CommandResult>?>(new SetEstimateForStoryCommand(
messageContext,
storyId,
command));
}
}

return Task.FromResult<IRequest<CommandResult>?>(null);
return Task.FromResult<IRequest<CommandResult>>(new SetEstimateForStoryCommand(
messageContext,
storyId,
Command));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<ItemGroup>
<ProjectReference Include="..\Inc.TeamAssistant.Appraiser.Domain\Inc.TeamAssistant.Appraiser.Domain.csproj" />
<ProjectReference Include="..\Inc.TeamAssistant.Appraiser.Model\Inc.TeamAssistant.Appraiser.Model.csproj" />
<ProjectReference Include="..\Inc.TeamAssistant.DialogContinuations\Inc.TeamAssistant.DialogContinuations.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
Expand Down
Loading

0 comments on commit 195b440

Please sign in to comment.