Skip to content

Commit

Permalink
Fixed move to decline notification
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Sep 21, 2024
1 parent 9cf09e1 commit 68debc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
using Inc.TeamAssistant.Primitives;
using Inc.TeamAssistant.Primitives.Bots;
using Inc.TeamAssistant.Primitives.Commands;
using Inc.TeamAssistant.Primitives.Exceptions;
using Inc.TeamAssistant.Reviewer.Application.Contracts;
using Inc.TeamAssistant.Reviewer.Domain;
using Inc.TeamAssistant.Reviewer.Model.Commands.MoveToDecline;
using MediatR;

namespace Inc.TeamAssistant.Reviewer.Application.CommandHandlers.MoveToDecline;

internal sealed class MoveToDeclineCommandHandler : IRequestHandler<MoveToDeclineCommand, CommandResult>
{
private readonly ITaskForReviewRepository _taskForReviewRepository;
private readonly ITaskForReviewRepository _repository;
private readonly IReviewMessageBuilder _reviewMessageBuilder;
private readonly ITeamAccessor _teamAccessor;
private readonly IBotAccessor _botAccessor;

public MoveToDeclineCommandHandler(
ITaskForReviewRepository taskForReviewRepository,
ITaskForReviewRepository repository,
IReviewMessageBuilder reviewMessageBuilder,
ITeamAccessor teamAccessor)
ITeamAccessor teamAccessor,
IBotAccessor botAccessor)
{
_taskForReviewRepository =
taskForReviewRepository ?? throw new ArgumentNullException(nameof(taskForReviewRepository));
_reviewMessageBuilder =
reviewMessageBuilder ?? throw new ArgumentNullException(nameof(reviewMessageBuilder));
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
_reviewMessageBuilder = reviewMessageBuilder ?? throw new ArgumentNullException(nameof(reviewMessageBuilder));
_teamAccessor = teamAccessor ?? throw new ArgumentNullException(nameof(teamAccessor));
_botAccessor = botAccessor ?? throw new ArgumentNullException(nameof(botAccessor));
}

public async Task<CommandResult> Handle(MoveToDeclineCommand command, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(command);

var taskForReview = await _taskForReviewRepository.GetById(command.TaskId, token);
var taskForReview = await _repository.GetById(command.TaskId, token);
var botContext = await _botAccessor.GetBotContext(taskForReview.BotId, token);

if (!taskForReview.CanAccept())
return CommandResult.Empty;

Expand All @@ -41,7 +46,7 @@ public async Task<CommandResult> Handle(MoveToDeclineCommand command, Cancellati
if (owner is null)
throw new TeamAssistantUserException(Messages.Connector_PersonNotFound, taskForReview.OwnerId);

taskForReview.Decline(DateTimeOffset.UtcNow);
taskForReview.Decline(DateTimeOffset.UtcNow, botContext.GetNotificationIntervals());

var notifications = await _reviewMessageBuilder.Build(
command.MessageContext.ChatMessage.MessageId,
Expand All @@ -50,7 +55,7 @@ public async Task<CommandResult> Handle(MoveToDeclineCommand command, Cancellati
owner,
token);

await _taskForReviewRepository.Upsert(taskForReview, token);
await _repository.Upsert(taskForReview, token);

return CommandResult.Build(notifications.ToArray());
}
Expand Down
7 changes: 6 additions & 1 deletion src/Inc.TeamAssistant.Reviewer.Domain/TaskForReview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ public void Accept(DateTimeOffset now)

public void MoveToAccept() => State = TaskForReviewState.Accept;

public void Decline(DateTimeOffset now)
public void Decline(DateTimeOffset now, NotificationIntervals notificationIntervals)
{
ArgumentNullException.ThrowIfNull(notificationIntervals);

AddReviewInterval(ReviewerId, now);

State = TaskForReviewState.OnCorrection;
NextNotification = now;

if (ReviewIntervals.All(i => i.State != TaskForReviewState.OnCorrection))
SetNextNotificationTime(now, notificationIntervals);
}

public bool CanMoveToNextRound() => State == TaskForReviewState.OnCorrection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Add_IntervalsByTask_CorrectMetrics(
start,
moveToInProgressInterval,
acceptDurationInterval,
new[] { (declineDurationInterval, moveToNextRoundDurationInterval) });
[(declineDurationInterval, moveToNextRoundDurationInterval)]);
await _target.Add(taskForReview, CancellationToken.None);

var reviewTeamMetrics = _target.Get(teamId);
Expand Down Expand Up @@ -79,11 +79,10 @@ public async Task Add_IntervalsByTaskWithManyRounds_CorrectMetrics(
start,
moveToInProgressInterval,
acceptDurationInterval,
new[]
{
[
(declineDurationInterval * 2, moveToNextRoundDurationInterval * 2),
(declineDurationInterval, moveToNextRoundDurationInterval)
});
]);
await _target.Add(taskForReview, CancellationToken.None);

var reviewTeamMetrics = _target.Get(teamId);
Expand Down Expand Up @@ -116,19 +115,19 @@ public async Task Add_IntervalsByTasks_CorrectAverageMetrics(
start,
moveToInProgressInterval,
acceptDurationInterval,
new[] { (declineDurationInterval, moveToNextRoundDurationInterval) });
[(declineDurationInterval, moveToNextRoundDurationInterval)]);
var taskForReviewSecond = CreateAndSetupTaskForReview(
teamId,
start,
moveToInProgressInterval * 2,
acceptDurationInterval * 2,
new[] { (declineDurationInterval * 2, moveToNextRoundDurationInterval * 2) });
[(declineDurationInterval * 2, moveToNextRoundDurationInterval * 2)]);
var taskForReviewFromOtherTeam = CreateAndSetupTaskForReview(
_fixture.Create<Guid>(),
start,
moveToInProgressInterval,
acceptDurationInterval,
new[] { (declineDurationInterval, moveToNextRoundDurationInterval) });
[(declineDurationInterval, moveToNextRoundDurationInterval)]);
await _target.Add(taskForReviewFirst, CancellationToken.None);
await _target.Add(taskForReviewSecond, CancellationToken.None);
await _target.Add(taskForReviewFromOtherTeam, CancellationToken.None);
Expand Down Expand Up @@ -177,7 +176,7 @@ private TaskForReview CreateAndSetupTaskForReview(
foreach (var reviewDuration in reviewDurations)
{
operationStart = operationStart.Add(reviewDuration.DeclineDuration);
taskForReview.Decline(operationStart);
taskForReview.Decline(operationStart, _fixture.Create<NotificationIntervals>());

operationStart = operationStart.Add(reviewDuration.NextRoundDuration);
taskForReview.MoveToNextRound(operationStart);
Expand Down

0 comments on commit 68debc5

Please sign in to comment.