Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random coffee refuse flag added #84

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Inc.TeamAssistant.Appraiser.Domain/Messages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Inc.TeamAssistant.Primitives.Languages;

namespace Inc.TeamAssistant.Appraiser.Domain;

internal static class Messages
{
public static readonly MessageId Connector_HasNoRights = new(nameof(Connector_HasNoRights));
}
6 changes: 3 additions & 3 deletions src/Inc.TeamAssistant.Appraiser.Domain/Story.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Reset(long participantId, bool hasManagerAccess)
return;

if (ModeratorId != participantId && !hasManagerAccess)
throw new TeamAssistantException("User has not rights for action.");
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, participantId);

foreach (var storyForEstimate in _storyForEstimates)
storyForEstimate.Reset();
Expand All @@ -99,7 +99,7 @@ public void Finish(long participantId, bool hasManagerAccess)
return;

if (ModeratorId != participantId && !hasManagerAccess)
throw new TeamAssistantException("User has not rights for action.");
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, participantId);

foreach (var storyForEstimate in _storyForEstimates)
if (storyForEstimate.Value == Estimation.None.Value)
Expand All @@ -112,7 +112,7 @@ public void Finish(long participantId, bool hasManagerAccess)
public Estimation Accept(long participantId, bool hasManagerAccess, int value)
{
if (ModeratorId != participantId && !hasManagerAccess)
throw new TeamAssistantException("User has not rights for action.");
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, participantId);

var totalEstimation = ToEstimation(value);

Expand Down
5 changes: 4 additions & 1 deletion src/Inc.TeamAssistant.Gateway/wwwroot/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
"RandomCoffee_MeetingDescription": "Please arrange a meeting via private messaging",
"RandomCoffee_NotEnoughParticipants": "There are not enough participants to form pairs",
"RandomCoffee_InviteHelp": "Start random coffee meetings",
"RandomCoffee_RefuseHelp": "Refuse random coffee meetings",
"RandomCoffee_RefusedForCoffee": "Random coffee meetings are refused",

"Connector_EnterTeamName": "Reply to this message with the team name",
"Connector_SelectTeam": "Select the team:",
Expand All @@ -112,5 +114,6 @@
"Connector_RemoveTeamHelp": "Remove the team",
"Connector_HasNotRightsForRemoveTeam": "You do not have permission to remove the command \"{0}\"",
"Connector_RemoveTeamSuccess": "The \"{0}\" team has been removed",
"Connector_Help": "Display the list of available commands"
"Connector_Help": "Display the list of available commands",
"Connector_HasNoRights": "User has no rights to perform this action"
}
5 changes: 4 additions & 1 deletion src/Inc.TeamAssistant.Gateway/wwwroot/langs/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
"RandomCoffee_MeetingDescription": "Договоритесь в ЛС о проведении встречи",
"RandomCoffee_NotEnoughParticipants": "Недостаточно участников для составления пар",
"RandomCoffee_InviteHelp": "Начать проведение встреч",
"RandomCoffee_RefuseHelp": "Приостановить проведение встреч",
"RandomCoffee_RefusedForCoffee": "Проведение встреч приостановлено",

"Connector_EnterTeamName": "В ответ на это сообщение необходимо ввести наименование команды",
"Connector_SelectTeam": "Выберите команду:",
Expand All @@ -112,5 +114,6 @@
"Connector_RemoveTeamHelp": "Удалить команду",
"Connector_HasNotRightsForRemoveTeam": "У вас нет прав на удаление команды «{0}»",
"Connector_RemoveTeamSuccess": "Команда «{0}» удалена",
"Connector_Help": "Отобразить список команд"
"Connector_Help": "Отобразить список команд",
"Connector_HasNoRights": "У пользователя недостаточно прав для выполнения операции"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using FluentMigrator;

namespace Inc.TeamAssistant.Migrations;

[Migration(2024_10_25_0)]

public sealed class AddRandomCoffeeRefusedFlag : Migration
{
public override void Up()
{
Create
.Column("refused")
.OnTable("entries")
.InSchema("random_coffee")
.AsBoolean()
dyatlov-a marked this conversation as resolved.
Show resolved Hide resolved
.NotNullable()
.SetExistingRowsTo(false);

Execute.Sql(
"""
INSERT INTO connector.bot_commands(id, value, help_message_id, scopes)
VALUES
('d6097f8b-de33-412c-a064-68069d458776', '/refuse', 'RandomCoffee_RefuseHelp', '[2]'::jsonb)
""",
"Insert new random coffee command");
Execute.Sql(
"""
INSERT INTO connector.command_packs(feature_id, bot_command_id)
VALUES
('39195e70-b83a-42b3-88e5-dbbf6789a3c8', 'd6097f8b-de33-412c-a064-68069d458776')
""",
"Insert new random coffee command");
}

public override void Down()
{
Delete
.Column("refused")
.FromTable("entries")
.InSchema("random_coffee");

Execute.Sql(
"""
DELETE FROM connector.command_packs
WHERE feature_id = '39195e70-b83a-42b3-88e5-dbbf6789a3c8'
AND bot_command_id = 'd6097f8b-de33-412c-a064-68069d458776';
""",
"Clear new random coffee command");

Execute.Sql(
"""
DELETE FROM connector.bot_commands
WHERE id = 'd6097f8b-de33-412c-a064-68069d458776';
""",
"Clear new random coffee command");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<CommandResult> Handle(AddPollAnswerCommand command, Cancellati
ArgumentNullException.ThrowIfNull(command);

var randomCoffeeEntry = await _repository.Find(command.PollId, token);
if (randomCoffeeEntry is not null)
if (randomCoffeeEntry?.Refused is false)
dyatlov-a marked this conversation as resolved.
Show resolved Hide resolved
{
const string optionYes = "0";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<CommandResult> Handle(AttachPollCommand command, CancellationT
ArgumentNullException.ThrowIfNull(command);

var randomCoffeeEntry = await _repository.Find(command.RandomCoffeeEntryId, token);
if (randomCoffeeEntry is null)
if (randomCoffeeEntry is null || randomCoffeeEntry.Refused is true)
throw new TeamAssistantException($"RandomCoffeeEntry {command.RandomCoffeeEntryId} was not found.");

randomCoffeeEntry.AttachPoll(command.PollId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public async Task<CommandResult> Handle(InviteForCoffeeCommand command, Cancella
ArgumentNullException.ThrowIfNull(command);

var existsRandomCoffeeEntry = await _repository.Find(command.MessageContext.ChatMessage.ChatId, token);
if (existsRandomCoffeeEntry is not null && command.OnDemand)
if (existsRandomCoffeeEntry?.Refused is false && command.OnDemand)
dyatlov-a marked this conversation as resolved.
Show resolved Hide resolved
return CommandResult.Empty;

if (existsRandomCoffeeEntry?.Refused is true && !command.OnDemand)
return CommandResult.Empty;

if (existsRandomCoffeeEntry is not null && existsRandomCoffeeEntry.OwnerId != command.MessageContext.Person.Id)
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, command.MessageContext.Person.Id);

var randomCoffeeEntry = existsRandomCoffeeEntry ?? new RandomCoffeeEntry(
Guid.NewGuid(),
command.MessageContext.Bot.Id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Inc.TeamAssistant.Primitives;
using Inc.TeamAssistant.Primitives.Commands;
using Inc.TeamAssistant.Primitives.Exceptions;
using Inc.TeamAssistant.Primitives.Languages;
using Inc.TeamAssistant.Primitives.Notifications;
using Inc.TeamAssistant.RandomCoffee.Application.Contracts;
using Inc.TeamAssistant.RandomCoffee.Model.Commands.RefuseForCoffee;
using MediatR;

namespace Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.RefuseForCoffee;

public sealed class RefuseForCoffeeCommandHandler : IRequestHandler<RefuseForCoffeeCommand, CommandResult>
{
private readonly IRandomCoffeeRepository _repository;
private readonly ITeamAccessor _teamAccessor;
private readonly IMessageBuilder _messageBuilder;

public RefuseForCoffeeCommandHandler(
IRandomCoffeeRepository repository,
ITeamAccessor teamAccessor,
IMessageBuilder messageBuilder)
{
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
_teamAccessor = teamAccessor ?? throw new ArgumentNullException(nameof(teamAccessor));
_messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder));
}

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

var existsRandomCoffeeEntry = await _repository.Find(command.MessageContext.ChatMessage.ChatId, token);

if (existsRandomCoffeeEntry is null || existsRandomCoffeeEntry.Refused)
return CommandResult.Empty;

if (command.MessageContext.Person.Id != existsRandomCoffeeEntry.OwnerId)
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, command.MessageContext.Person.Id);

var owner = await _teamAccessor.FindPerson(existsRandomCoffeeEntry.OwnerId, token);
if (owner is null)
throw new TeamAssistantException($"Owner {existsRandomCoffeeEntry.OwnerId} was not found.");

existsRandomCoffeeEntry.MoveToRefused();
dyatlov-a marked this conversation as resolved.
Show resolved Hide resolved

await _repository.Upsert(existsRandomCoffeeEntry, token);

var languageId = await _teamAccessor.GetClientLanguage(command.MessageContext.Bot.Id, owner.Id, token);
var notification = NotificationMessage
.Create(
existsRandomCoffeeEntry.ChatId,
await _messageBuilder.Build(Messages.RandomCoffee_RefusedForCoffee, languageId));
var notifications = new[]
{
notification,
NotificationMessage.Delete(command.MessageContext.ChatMessage)
};

return CommandResult.Build(notifications);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Inc.TeamAssistant.Primitives.Commands;
using Inc.TeamAssistant.RandomCoffee.Model.Commands.RefuseForCoffee;

namespace Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.RefuseForCoffee.Services;

internal sealed class RefuseForCoffeeCommandCreator : ICommandCreator
{
public string Command => CommandList.RefuseForCoffee;

public Task<IDialogCommand> Create(
MessageContext messageContext,
CurrentTeamContext teamContext,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(messageContext);
ArgumentNullException.ThrowIfNull(teamContext);

return Task.FromResult<IDialogCommand>(new RefuseForCoffeeCommand(messageContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public async Task<CommandResult> Handle(SelectPairsCommand command, Cancellation
if (randomCoffeeEntry is null)
throw new TeamAssistantException($"RandomCoffeeEntry {command.RandomCoffeeEntryId} was not found.");

if (randomCoffeeEntry.Refused is true)
return CommandResult.Empty;

var botContext = await _botAccessor.GetBotContext(randomCoffeeEntry.BotId, token);
var owner = await _teamAccessor.FindPerson(randomCoffeeEntry.OwnerId, token);
if (owner is null)
Expand Down
2 changes: 2 additions & 0 deletions src/Inc.TeamAssistant.RandomCoffee.Application/CommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ internal static class CommandList
public const string InviteForCoffee = "/invite";

public const string AddPollAnswer = "/poll_answer?pollId=";

public const string RefuseForCoffee = "/refuse";
}
2 changes: 2 additions & 0 deletions src/Inc.TeamAssistant.RandomCoffee.Application/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ internal static class Messages
public static readonly MessageId RandomCoffee_NotSelectedPair = new(nameof(RandomCoffee_NotSelectedPair));
public static readonly MessageId RandomCoffee_MeetingDescription = new(nameof(RandomCoffee_MeetingDescription));
public static readonly MessageId RandomCoffee_NotEnoughParticipants = new(nameof(RandomCoffee_NotEnoughParticipants));
public static readonly MessageId RandomCoffee_RefusedForCoffee = new(nameof(RandomCoffee_RefusedForCoffee));
public static readonly MessageId Connector_HasNoRights = new(nameof(Connector_HasNoRights));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Inc.TeamAssistant.Primitives.FeatureProperties;
using Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.AddPollAnswer.Services;
using Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.InviteForCoffee.Services;
using Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.RefuseForCoffee.Services;
using Inc.TeamAssistant.RandomCoffee.Application.CommandHandlers.SelectPairs.Services;
using Inc.TeamAssistant.RandomCoffee.Application.Services;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -21,6 +22,7 @@ public static IServiceCollection AddRandomCoffeeApplication(this IServiceCollect

.AddSingleton<ICommandCreator, InviteForCoffeeCommandCreator>()
.AddSingleton<ICommandCreator, AddPollAnswerCommandCreator>()
.AddSingleton<ICommandCreator, RefuseForCoffeeCommandCreator>()

.AddHostedService<ScheduleService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ INSERT INTO random_coffee.entries (
state,
poll_id,
participant_ids,
name)
name,
refused)
VALUES (
@id,
@created,
Expand All @@ -102,7 +103,8 @@ INSERT INTO random_coffee.entries (
@state,
@poll_id,
@participant_ids::jsonb,
@name)
@name,
@refused)
ON CONFLICT (id) DO UPDATE SET
created = excluded.created,
bot_id = excluded.bot_id,
Expand All @@ -112,7 +114,8 @@ ON CONFLICT (id) DO UPDATE SET
state = excluded.state,
poll_id = excluded.poll_id,
participant_ids = excluded.participant_ids,
name = excluded.name;",
name = excluded.name,
refused = excluded.refused;",
new
{
id = randomCoffeeEntry.Id,
Expand All @@ -124,7 +127,8 @@ ON CONFLICT (id) DO UPDATE SET
state = randomCoffeeEntry.State,
poll_id = randomCoffeeEntry.PollId,
participant_ids = JsonSerializer.Serialize(randomCoffeeEntry.ParticipantIds),
name = randomCoffeeEntry.Name
name = randomCoffeeEntry.Name,
refused = randomCoffeeEntry.Refused
},
flags: CommandFlags.None,
cancellationToken: token);
Expand Down Expand Up @@ -185,7 +189,8 @@ ON CONFLICT (id) DO UPDATE SET
e.state AS state,
e.poll_id AS pollid,
e.participant_ids AS participantids,
e.name AS name
e.name AS name,
e.refused AS refused
FROM random_coffee.entries AS e
WHERE e.id = @id;

Expand Down
11 changes: 11 additions & 0 deletions src/Inc.TeamAssistant.RandomCoffee.Domain/RandomCoffeeEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public sealed class RandomCoffeeEntry

private readonly List<RandomCoffeeHistory> _history = new();
public IReadOnlyCollection<RandomCoffeeHistory> History => _history;

public bool Refused { get; private set; }

private RandomCoffeeEntry()
{
Expand Down Expand Up @@ -51,6 +53,8 @@ public RandomCoffeeEntry MoveToWaiting(DateTimeOffset now, TimeSpan waitingInter
ParticipantIds.Clear();
PollId = null;

Refused = false;

return this;
}

Expand Down Expand Up @@ -104,4 +108,11 @@ public RandomCoffeeHistory SelectPairs()

return randomCoffeeHistory;
}

public RandomCoffeeEntry MoveToRefused()
{
Refused = true;

return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Inc.TeamAssistant.Primitives.Commands;

namespace Inc.TeamAssistant.RandomCoffee.Model.Commands.RefuseForCoffee;

public sealed record RefuseForCoffeeCommand(MessageContext MessageContext)
: IDialogCommand;
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public DraftTaskForReview WithPreviewMessage(int messageId)
public DraftTaskForReview CheckRights(long personId)
{
if (OwnerId != personId)
throw new TeamAssistantException("User has not rights for action.");
throw new TeamAssistantUserException(Messages.Connector_HasNoRights, personId);

return this;
}
Expand Down
Loading