feat: invites
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.AcceptCollectionInvite;
|
||||
|
||||
/// <param name="Token">Токен из deep link (t.me/Bot?start=inv_TOKEN).</param>
|
||||
/// <param name="AcceptingTelegramId">Telegram ID принимающего приглашение.</param>
|
||||
/// <returns>ID коллекции при успехе, null если инвайт не найден/отозван.</returns>
|
||||
public sealed record AcceptCollectionInviteCommand(
|
||||
string Token,
|
||||
long AcceptingTelegramId
|
||||
) : IRequest<Guid?>;
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.AcceptCollectionInvite;
|
||||
|
||||
public sealed class AcceptCollectionInviteCommandHandler
|
||||
: IRequestHandler<AcceptCollectionInviteCommand, Guid?>
|
||||
{
|
||||
private readonly ICollectionInviteRepository _invites;
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public AcceptCollectionInviteCommandHandler(
|
||||
ICollectionInviteRepository invites,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_invites = invites;
|
||||
_collections = collections;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task<Guid?> Handle(
|
||||
AcceptCollectionInviteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var invite = await _invites.GetByTokenAsync(request.Token, cancellationToken);
|
||||
if (invite is null || invite.IsRevoked)
|
||||
return null;
|
||||
|
||||
var collection = await _collections.GetByIdAsync(invite.CollectionId, cancellationToken)
|
||||
?? throw new DomainException("Collection no longer exists.");
|
||||
|
||||
// Владелец не может принять инвайт в собственную коллекцию
|
||||
if (collection.OwnerTelegramId == request.AcceptingTelegramId)
|
||||
return collection.Id;
|
||||
|
||||
// Если уже участник — просто возвращаем коллекцию (идемпотентность)
|
||||
if (collection.GetRole(request.AcceptingTelegramId) is not null)
|
||||
return collection.Id;
|
||||
|
||||
// Owner создаёт инвайт с правами requestingTelegramId, добавляем участника
|
||||
collection.AddMember(request.AcceptingTelegramId, invite.Role, collection.OwnerTelegramId);
|
||||
|
||||
await _collections.UpdateAsync(collection, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return collection.Id;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using MediatR;
|
||||
using MemeVault.Domain.Enums;
|
||||
|
||||
namespace MemeVault.Application.Commands.CreateCollectionInvite;
|
||||
|
||||
/// <param name="CollectionId">Коллекция, для которой создаётся инвайт.</param>
|
||||
/// <param name="RequestingTelegramId">Telegram ID запрашивающего (должен быть Owner).</param>
|
||||
/// <param name="Role">Роль, которую получит принявший (Viewer или Manager).</param>
|
||||
/// <returns>Токен инвайта, или null если коллекция не найдена / нет прав.</returns>
|
||||
public sealed record CreateCollectionInviteCommand(
|
||||
Guid CollectionId,
|
||||
long RequestingTelegramId,
|
||||
CollectionRole Role
|
||||
) : IRequest<string?>;
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.CollectionInvite;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.CreateCollectionInvite;
|
||||
|
||||
public sealed class CreateCollectionInviteCommandHandler
|
||||
: IRequestHandler<CreateCollectionInviteCommand, string?>
|
||||
{
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly ICollectionInviteRepository _invites;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public CreateCollectionInviteCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
ICollectionInviteRepository invites,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_collections = collections;
|
||||
_invites = invites;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task<string?> Handle(
|
||||
CreateCollectionInviteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var collection = await _collections.GetByIdAsync(request.CollectionId, cancellationToken);
|
||||
if (collection is null)
|
||||
return null;
|
||||
|
||||
// Только Owner может создавать инвайты
|
||||
if (collection.GetRole(request.RequestingTelegramId) != CollectionRole.Owner)
|
||||
return null;
|
||||
|
||||
var invite = CollectionInvite.Create(
|
||||
collection.Id,
|
||||
request.RequestingTelegramId,
|
||||
request.Role);
|
||||
|
||||
await _invites.AddAsync(invite, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return invite.Token;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.RevokeCollectionInvite;
|
||||
|
||||
/// <returns>true если отозван, false если не найден или нет прав.</returns>
|
||||
public sealed record RevokeCollectionInviteCommand(
|
||||
Guid InviteId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<bool>;
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.RevokeCollectionInvite;
|
||||
|
||||
public sealed class RevokeCollectionInviteCommandHandler
|
||||
: IRequestHandler<RevokeCollectionInviteCommand, bool>
|
||||
{
|
||||
private readonly ICollectionInviteRepository _invites;
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public RevokeCollectionInviteCommandHandler(
|
||||
ICollectionInviteRepository invites,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_invites = invites;
|
||||
_collections = collections;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(
|
||||
RevokeCollectionInviteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var invite = await _invites.GetByIdAsync(request.InviteId, cancellationToken);
|
||||
if (invite is null)
|
||||
return false;
|
||||
|
||||
var collection = await _collections.GetByIdAsync(invite.CollectionId, cancellationToken);
|
||||
if (collection is null)
|
||||
return false;
|
||||
|
||||
// Только Owner коллекции может отзывать инвайты
|
||||
if (collection.GetRole(request.RequestingTelegramId) != CollectionRole.Owner)
|
||||
return false;
|
||||
|
||||
invite.Revoke();
|
||||
|
||||
await _invites.UpdateAsync(invite, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user