Add proposal workflow and voting API
This commit is contained in:
1 parent
9ca23a2c15
commit
1ff4a0a814
17 files changed
+796
-83
No files matched your search
@@ -0,0 +1,64 @@
|
||||
using Helpdesk.Domain.Users;
|
||||
|
||||
namespace Helpdesk.Domain.Proposals;
|
||||
|
||||
public enum ProposalStatus { Submitted, UnderReview, Accepted, Rejected, Implemented }
|
||||
public enum ProposalEventType { Created, Updated, StatusChanged }
|
||||
|
||||
public sealed class Proposal : IHasCreatedAt
|
||||
{
|
||||
private Proposal() { }
|
||||
|
||||
public Proposal(int authorId, string title, string description, int categoryId)
|
||||
{
|
||||
AuthorId = authorId;
|
||||
SetDetails(title, description, categoryId);
|
||||
}
|
||||
|
||||
public int Id { get; private set; }
|
||||
public int AuthorId { get; private set; }
|
||||
public string Title { get; private set; } = null!;
|
||||
public string Description { get; private set; } = null!;
|
||||
public int CategoryId { get; private set; }
|
||||
public ProposalStatus Status { get; private set; } = ProposalStatus.Submitted;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public bool IsAuthor(int userId) => AuthorId == userId;
|
||||
public bool IsEditable => Status == ProposalStatus.Submitted;
|
||||
public bool IsVotingOpen => Status is ProposalStatus.Submitted or ProposalStatus.UnderReview;
|
||||
public bool CanEdit(int userId) => IsAuthor(userId) && IsEditable;
|
||||
public bool CanVote(int userId) => !IsAuthor(userId) && IsVotingOpen;
|
||||
|
||||
public bool CanTransitionTo(ProposalStatus target) => Status switch
|
||||
{
|
||||
ProposalStatus.Submitted => target is ProposalStatus.UnderReview or ProposalStatus.Accepted or ProposalStatus.Rejected,
|
||||
ProposalStatus.UnderReview => target is ProposalStatus.Accepted or ProposalStatus.Rejected,
|
||||
ProposalStatus.Accepted => target == ProposalStatus.Implemented,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
public void Edit(int actorId, string title, string description, int categoryId)
|
||||
{
|
||||
if (!CanEdit(actorId))
|
||||
throw new InvalidOperationException("Návrh nelze upravit.");
|
||||
|
||||
SetDetails(title, description, categoryId);
|
||||
}
|
||||
|
||||
public void ChangeStatus(ProposalStatus target, UserRole role, string? note)
|
||||
{
|
||||
if (role != UserRole.Management || !CanTransitionTo(target))
|
||||
throw new InvalidOperationException("Stav návrhu nelze změnit.");
|
||||
if (target == ProposalStatus.Rejected && string.IsNullOrWhiteSpace(note))
|
||||
throw new ArgumentException("Zamítnutí vyžaduje vyjádření.", nameof(note));
|
||||
|
||||
Status = target;
|
||||
}
|
||||
|
||||
private void SetDetails(string title, string description, int categoryId)
|
||||
{
|
||||
Title = title;
|
||||
Description = description;
|
||||
CategoryId = categoryId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace Helpdesk.Domain.Proposals;
|
||||
|
||||
public sealed class ProposalEvent
|
||||
{
|
||||
private ProposalEvent() { }
|
||||
|
||||
public ProposalEvent(Proposal proposal, ProposalEventType eventType, ProposalStatus? fromStatus, int actorId,
|
||||
string? note, DateTime occurredAt)
|
||||
{
|
||||
Proposal = proposal;
|
||||
EventType = eventType;
|
||||
FromStatus = fromStatus;
|
||||
ToStatus = proposal.Status;
|
||||
ActorId = actorId;
|
||||
Note = note;
|
||||
OccurredAt = occurredAt;
|
||||
}
|
||||
|
||||
public int Id { get; private set; }
|
||||
public int ProposalId { get; private set; }
|
||||
public Proposal Proposal { get; private set; } = null!;
|
||||
public ProposalEventType EventType { get; private set; }
|
||||
public ProposalStatus? FromStatus { get; private set; }
|
||||
public ProposalStatus ToStatus { get; private set; }
|
||||
public int ActorId { get; private set; }
|
||||
public string? Note { get; private set; }
|
||||
public DateTime OccurredAt { get; private set; }
|
||||
}
|
||||
|
||||
public sealed class ProposalVote
|
||||
{
|
||||
private ProposalVote() { }
|
||||
|
||||
public ProposalVote(Proposal proposal, int userId, sbyte value, DateTime now)
|
||||
{
|
||||
ProposalId = proposal.Id;
|
||||
UserId = userId;
|
||||
SetValue(proposal, value, now);
|
||||
}
|
||||
|
||||
public int ProposalId { get; private set; }
|
||||
public int UserId { get; private set; }
|
||||
public sbyte Value { get; private set; }
|
||||
public DateTime VotedAt { get; private set; }
|
||||
|
||||
public void SetValue(Proposal proposal, sbyte value, DateTime now)
|
||||
{
|
||||
if (proposal.Id != ProposalId || !proposal.CanVote(UserId))
|
||||
throw new InvalidOperationException("O návrhu nelze hlasovat.");
|
||||
if (value is not (1 or -1))
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
if (Value == value)
|
||||
return;
|
||||
|
||||
Value = value;
|
||||
VotedAt = now;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ProposalScore
|
||||
{
|
||||
public int ProposalId { get; private set; }
|
||||
public decimal VotesFor { get; private set; }
|
||||
public decimal VotesAgainst { get; private set; }
|
||||
public decimal Score { get; private set; }
|
||||
}
|
||||
Reference in new issue
Block a user