Files
NTBug/backend/src/Helpdesk.Domain/Proposals/Proposal.cs
T

65 lines
2.5 KiB
C#

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;
}
}