Skip to content

Commit

Permalink
Refactoring ReviewStatsBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 22, 2024
1 parent 480ae6c commit f131fff
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public Estimation GetValue(int value)
if (Estimations.TryGetValue(value, out var estimation))
return estimation;

throw new ArgumentOutOfRangeException(
nameof(value),
value,
$"Value is not valid for {nameof(FibonacciEstimationStrategy)}.");
throw new ArgumentOutOfRangeException(nameof(value), value, "State out of range.");
}

public int GetWeight(Story story)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public Estimation GetValue(int value)
if (Estimations.TryGetValue(value, out var estimation))
return estimation;

throw new ArgumentOutOfRangeException(
nameof(value),
value,
$"Value is not valid for {nameof(FibonacciEstimationStrategy)}.");
throw new ArgumentOutOfRangeException(nameof(value), value, "State out of range.");
}

public int GetWeight(Story story)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public Estimation GetValue(int value)
if (Estimations.TryGetValue(value, out var estimation))
return estimation;

throw new ArgumentOutOfRangeException(
nameof(value),
value,
$"Value is not valid for {nameof(TShirtEstimationStrategy)}.");
throw new ArgumentOutOfRangeException(nameof(value), value, "State out of range.");
}

public int GetWeight(Story story)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public static IEstimationStrategy Create(StoryType storyType)
{
return Strategies.TryGetValue(storyType, out var strategy)
? strategy
: throw new ArgumentOutOfRangeException(nameof(storyType),
storyType,
$"StoryType is not valid for {nameof(EstimationStrategyFactory)}.");
: throw new ArgumentOutOfRangeException(nameof(storyType), storyType, "State out of range.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ private static BotCommandScope ToBotCommandScope(ContextScope contextScope)
{
ContextScope.Chats => BotCommandScope.AllGroupChats(),
ContextScope.Private => BotCommandScope.Default(),
_ => throw new ArgumentOutOfRangeException(
nameof(contextScope),
contextScope,
"ContextScope value was out of range.")
_ => throw new ArgumentOutOfRangeException(nameof(contextScope), contextScope, "State out of range.")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ protected override async Task ExecuteAsync(CancellationToken token)
private async Task Execute(CancellationToken token)
{
var now = DateTimeOffset.UtcNow;
var randomCoffeeEntries = await _reader.GetByDate(now, token);
var entries = await _reader.GetByDate(now, token);

foreach (var randomCoffeeEntry in randomCoffeeEntries)
foreach (var entry in entries)
{
if (!await _holidayService.IsWorkTime(randomCoffeeEntry.BotId, now, token))
if (!await _holidayService.IsWorkTime(entry.BotId, now, token))
continue;

var messageContext = MessageContext.CreateFromBackground(randomCoffeeEntry.BotId, randomCoffeeEntry.ChatId);
var messageContext = MessageContext.CreateFromBackground(entry.BotId, entry.ChatId);

IDialogCommand command = randomCoffeeEntry.State switch
IDialogCommand command = entry.State switch
{
RandomCoffeeState.Waiting => new SelectPairsCommand(messageContext, randomCoffeeEntry.Id),
RandomCoffeeState.Waiting => new SelectPairsCommand(messageContext, entry.Id),
RandomCoffeeState.Idle => new InviteForCoffeeCommand(messageContext, OnDemand: false),
_ => throw new ArgumentOutOfRangeException()
_ => throw new ArgumentOutOfRangeException(nameof(entry.State), entry.State, "State out of range.")
};

await _commandExecutor.Execute(command, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static IServiceCollection AddReviewerApplication(this IServiceCollection

services
.AddSingleton<DraftTaskForReviewService>()
.AddScoped<ReviewStatsBuilder>()
.AddScoped<IReviewMessageBuilder, ReviewMessageBuilder>()
.AddScoped<ReassignReviewService>()
.AddHostedService<PushService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ internal sealed class ReviewMessageBuilder : IReviewMessageBuilder
private readonly IReviewMetricsProvider _metricsProvider;
private readonly ReviewTeamMetricsFactory _metricsFactory;
private readonly DraftTaskForReviewService _service;
private readonly ReviewStatsBuilder _reviewStatsBuilder;

public ReviewMessageBuilder(
IMessageBuilder messageBuilder,
ITeamAccessor teamAccessor,
ITaskForReviewReader taskForReviewReader,
IReviewMetricsProvider metricsProvider,
ReviewTeamMetricsFactory metricsFactory,
DraftTaskForReviewService service,
ReviewStatsBuilder reviewStatsBuilder)
DraftTaskForReviewService service)
{
_messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder));
_teamAccessor = teamAccessor ?? throw new ArgumentNullException(nameof(teamAccessor));
_taskForReviewReader = taskForReviewReader ?? throw new ArgumentNullException(nameof(taskForReviewReader));
_metricsProvider = metricsProvider ?? throw new ArgumentNullException(nameof(metricsProvider));
_metricsFactory = metricsFactory ?? throw new ArgumentNullException(nameof(metricsFactory));
_service = service ?? throw new ArgumentNullException(nameof(service));
_reviewStatsBuilder = reviewStatsBuilder ?? throw new ArgumentNullException(nameof(reviewStatsBuilder));
}

public async Task<IReadOnlyCollection<NotificationMessage>> Build(
Expand Down Expand Up @@ -215,7 +212,7 @@ private async Task<NotificationMessage> MessageForTeam(
TaskForReviewState.InProgress => Icons.InProgress,
TaskForReviewState.OnCorrection => Icons.OnCorrection,
TaskForReviewState.Accept => Icons.Accept,
_ => throw new ArgumentOutOfRangeException(nameof(TaskForReviewState), task.State, "State out of range.")
_ => throw new ArgumentOutOfRangeException(nameof(task.State), task.State, "State out of range.")
};
var reviewerTargetMessageKey = task.HasConcreteReviewer
? Messages.Reviewer_TargetManually
Expand All @@ -232,14 +229,12 @@ private async Task<NotificationMessage> MessageForTeam(
messageBuilder.AppendLine();
messageBuilder.AppendLine(task.Description);
messageBuilder.AppendLine(state);

var stats = await _reviewStatsBuilder.Build(
task,
metricsByTeam,
metricsByTask,
languageId,
hasReviewMetrics: true,
hasCorrectionMetrics: true);

var stats = await ReviewStatsBuilder
.Create(_messageBuilder)
.WithReviewMetrics()
.WithCorrectionMetrics()
.Build(task, metricsByTeam, metricsByTask, languageId);
messageBuilder.Append(stats);

var message = messageBuilder.ToString();
Expand Down Expand Up @@ -275,112 +270,106 @@ private async Task<NotificationMessage> HideControlsForReviewer(
}

private async Task<NotificationMessage> MessageForReviewer(
TaskForReview taskForReview,
TaskForReview task,
ReviewTeamMetrics metricsByTeam,
ReviewTeamMetrics metricsByTask,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(taskForReview);
ArgumentNullException.ThrowIfNull(task);
ArgumentNullException.ThrowIfNull(metricsByTeam);
ArgumentNullException.ThrowIfNull(metricsByTask);

var languageId = await _teamAccessor.GetClientLanguage(taskForReview.BotId, taskForReview.ReviewerId, token);
var languageId = await _teamAccessor.GetClientLanguage(task.BotId, task.ReviewerId, token);
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine(await _messageBuilder.Build(Messages.Reviewer_NeedReview, languageId));
messageBuilder.AppendLine();
messageBuilder.AppendLine(taskForReview.Description);
messageBuilder.AppendLine(task.Description);

var stats = await _reviewStatsBuilder.Build(
taskForReview,
metricsByTeam,
metricsByTask,
languageId,
hasReviewMetrics: true,
hasCorrectionMetrics: false);
var stats = await ReviewStatsBuilder
.Create(_messageBuilder)
.WithReviewMetrics()
.Build(task, metricsByTeam, metricsByTask, languageId);
messageBuilder.Append(stats);

var message = messageBuilder.ToString();
var notification = taskForReview.ReviewerMessageId.HasValue
? NotificationMessage.Edit(new(taskForReview.ReviewerId, taskForReview.ReviewerMessageId.Value), message)
var notification = task.ReviewerMessageId.HasValue
? NotificationMessage.Edit(new(task.ReviewerId, task.ReviewerMessageId.Value), message)
: NotificationMessage
.Create(taskForReview.ReviewerId, message)
.Create(task.ReviewerId, message)
.AddHandler((c, p) => new AttachMessageCommand(
c,
taskForReview.Id,
task.Id,
int.Parse(p),
MessageType.Reviewer.ToString()));

if (taskForReview.State == TaskForReviewState.New)
if (task.State == TaskForReviewState.New)
{
var inProgressButton = await _messageBuilder.Build(Messages.Reviewer_MoveToInProgress, languageId);
notification.WithButton(new Button(
inProgressButton,
$"{CommandList.MoveToInProgress}{taskForReview.Id:N}"));
$"{CommandList.MoveToInProgress}{task.Id:N}"));

var fromDate = DateTimeOffset.UtcNow.GetLastDayOfWeek(DayOfWeek.Monday);
var hasReassign = await _taskForReviewReader.HasReassignFromDate(taskForReview.ReviewerId, fromDate, token);
if (!taskForReview.OriginalReviewerId.HasValue && !hasReassign)
var hasReassign = await _taskForReviewReader.HasReassignFromDate(task.ReviewerId, fromDate, token);
if (!task.OriginalReviewerId.HasValue && !hasReassign)
{
var reassignReviewButton = await _messageBuilder.Build(Messages.Reviewer_Reassign, languageId);
notification.WithButton(new Button(
reassignReviewButton,
$"{CommandList.ReassignReview}{taskForReview.Id:N}"));
$"{CommandList.ReassignReview}{task.Id:N}"));
}
}

if (taskForReview.State == TaskForReviewState.InProgress)
if (task.State == TaskForReviewState.InProgress)
{
var moveToAcceptButton = await _messageBuilder.Build(Messages.Reviewer_MoveToAccept, languageId);
notification.WithButton(new Button(moveToAcceptButton, $"{CommandList.Accept}{taskForReview.Id:N}"));
notification.WithButton(new Button(moveToAcceptButton, $"{CommandList.Accept}{task.Id:N}"));

var moveToDeclineButton = await _messageBuilder.Build(Messages.Reviewer_MoveToDecline, languageId);
notification.WithButton(new Button(moveToDeclineButton, $"{CommandList.Decline}{taskForReview.Id:N}"));
notification.WithButton(new Button(moveToDeclineButton, $"{CommandList.Decline}{task.Id:N}"));
}

return notification;
}

private async Task<NotificationMessage> MessageForOwner(
TaskForReview taskForReview,
TaskForReview task,
ReviewTeamMetrics metricsByTeam,
ReviewTeamMetrics metricsByTask,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(taskForReview);
ArgumentNullException.ThrowIfNull(task);
ArgumentNullException.ThrowIfNull(metricsByTeam);
ArgumentNullException.ThrowIfNull(metricsByTask);

var languageId = await _teamAccessor.GetClientLanguage(taskForReview.BotId, taskForReview.OwnerId, token);
var languageId = await _teamAccessor.GetClientLanguage(task.BotId, task.OwnerId, token);
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine(await _messageBuilder.Build(Messages.Reviewer_ReviewDeclined, languageId));
messageBuilder.AppendLine();
messageBuilder.AppendLine(taskForReview.Description);
messageBuilder.AppendLine(task.Description);

var stats = await _reviewStatsBuilder.Build(
taskForReview,
metricsByTeam,
metricsByTask,
languageId,
hasReviewMetrics: false,
hasCorrectionMetrics: true);
var stats = await ReviewStatsBuilder
.Create(_messageBuilder)
.WithCorrectionMetrics()
.Build(task, metricsByTeam, metricsByTask, languageId);
messageBuilder.Append(stats);

var message = messageBuilder.ToString();
var notification = taskForReview.OwnerMessageId.HasValue
? NotificationMessage.Edit(new(taskForReview.OwnerId, taskForReview.OwnerMessageId.Value), message)
var notification = task.OwnerMessageId.HasValue
? NotificationMessage.Edit(new(task.OwnerId, task.OwnerMessageId.Value), message)
: NotificationMessage
.Create(taskForReview.OwnerId, message)
.Create(task.OwnerId, message)
.AddHandler((c, p) => new AttachMessageCommand(
c,
taskForReview.Id,
task.Id,
int.Parse(p),
MessageType.Owner.ToString()));

if (taskForReview.State == TaskForReviewState.OnCorrection)
if (task.State == TaskForReviewState.OnCorrection)
{
var moveToNextRoundButton = await _messageBuilder.Build(Messages.Reviewer_MoveToNextRound, languageId);
notification.WithButton(new Button(moveToNextRoundButton,
$"{CommandList.MoveToNextRound}{taskForReview.Id:N}"));
$"{CommandList.MoveToNextRound}{task.Id:N}"));
}

return notification;
Expand Down
Loading

0 comments on commit f131fff

Please sign in to comment.