Skip to content

Commit

Permalink
Random coffee refuse flag added
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-tourist committed Oct 26, 2024
1 parent 3aad282 commit 037b52e
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Inc.TeamAssistant.Gateway/wwwroot/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,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",

"Constructor_NewBot": "New bot",
"Constructor_AddBot": "Add",
Expand Down
2 changes: 2 additions & 0 deletions src/Inc.TeamAssistant.Gateway/wwwroot/langs/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
"RandomCoffee_MeetingDescription": "Договоритесь в ЛС о проведении встречи",
"RandomCoffee_NotEnoughParticipants": "Недостаточно участников для составления пар",
"RandomCoffee_InviteHelp": "Начать проведение встреч",
"RandomCoffee_RefuseHelp": "Приостановить проведение встреч",
"RandomCoffee_RefusedForCoffee": "Проведение встреч приостановлено",

"Constructor_NewBot": "Создать",
"Constructor_AddBot": "Добавить",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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().Nullable();
}

public override void Down()
{
Delete
.Column("refused")
.FromTable("entries")
.InSchema("random_coffee");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentMigrator;

namespace Inc.TeamAssistant.Migrations;

[Migration(2024_10_25_1)]

public class AddRandomCoffeeRefuseHelpToCommand : Migration
{
public override void Up()
{
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()
{
Execute.Sql(
"""
DELETE FROM connector.bot_commands
WHERE id = 'd6097f8b-de33-412c-a064-68069d458776';
""",
"Clear new random coffee command");
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");
}
}
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)
{
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,7 +36,10 @@ 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)
return CommandResult.Empty;

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

var randomCoffeeEntry = existsRandomCoffeeEntry ?? new RandomCoffeeEntry(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 is true)
return CommandResult.Empty;

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

existsRandomCoffeeEntry.MoveToRefused();

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 = command.MessageContext.ChatMessage.OnlyChat
? [notification]
: 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";
}
1 change: 1 addition & 0 deletions src/Inc.TeamAssistant.RandomCoffee.Application/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ 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));
}
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;

0 comments on commit 037b52e

Please sign in to comment.