Add proposal workflow and voting API

This commit is contained in:
GonnaHetzMe committed 2026-11-18 21:58:33 +01:00
1 parent 9ca23a2c15
commit 1ff4a0a814
17 files changed
+796 -83

No files matched your search

@@ -1,41 +0,0 @@
namespace Helpdesk.Domain.Issues;
public enum NotificationType { IssueCreated, IssueAssigned, IssueStatusChanged, IssueCommentAdded, IssueReopened, ProposalStatusChanged }
public enum DeliveryChannel { Email, Push }
public enum DeliveryStatus { Pending, Skipped }
public sealed class IssueNotification(
Issue issue, int userId, NotificationType type, string title, string message) : IHasCreatedAt
{
private IssueNotification() : this(null!, 0, default, null!, null!) { }
public int Id { get; private set; }
public int? IssueId { get; private set; }
public int? ProposalId { get; private set; }
public Issue Issue { get; private set; } = issue;
public int UserId { get; private set; } = userId;
public NotificationType Type { get; private set; } = type;
public string Title { get; private set; } = title;
public string Message { get; private set; } = message;
public DateTime CreatedAt { get; set; }
public DateTime? ReadAt { get; private set; }
}
public sealed class NotificationDelivery(
IssueNotification notification, DeliveryChannel channel, bool enabled, DateTime now)
{
private NotificationDelivery() : this(null!, default, false, default) { }
public int Id { get; private set; }
public int NotificationId { get; private set; }
public IssueNotification Notification { get; private set; } = notification;
public DeliveryChannel Channel { get; private set; } = channel;
public DeliveryStatus Status { get; private set; } = enabled ? DeliveryStatus.Pending : DeliveryStatus.Skipped;
public DateTime? NextAttemptAt { get; private set; } = enabled ? now : null;
}
public sealed class PushDevice
{
public int Id { get; private set; }
public int UserId { get; private set; }
}
@@ -0,0 +1,63 @@
using Helpdesk.Domain.Issues;
using Helpdesk.Domain.Proposals;
namespace Helpdesk.Domain.Notifications;
public enum NotificationType { IssueCreated, IssueAssigned, IssueStatusChanged, IssueCommentAdded, IssueReopened, ProposalStatusChanged }
public enum DeliveryChannel { Email, Push }
public enum DeliveryStatus { Pending, Skipped }
public sealed class Notification : IHasCreatedAt
{
private Notification() { }
public Notification(Issue issue, int userId, NotificationType type, string title, string message)
: this(userId, type, title, message)
{
Issue = issue;
}
public Notification(Proposal proposal, int userId, string title)
: this(userId, NotificationType.ProposalStatusChanged, title, proposal.Title)
{
ProposalId = proposal.Id;
}
private Notification(int userId, NotificationType type, string title, string message)
{
UserId = userId;
Type = type;
Title = title;
Message = message;
}
public int Id { get; private set; }
public int? IssueId { get; private set; }
public int? ProposalId { get; private set; }
public Issue? Issue { get; private set; }
public int UserId { get; private set; }
public NotificationType Type { get; private set; }
public string Title { get; private set; } = null!;
public string Message { get; private set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime? ReadAt { get; private set; }
}
public sealed class NotificationDelivery(
Notification notification, DeliveryChannel channel, bool enabled, DateTime now)
{
private NotificationDelivery() : this(null!, default, false, default) { }
public int Id { get; private set; }
public int NotificationId { get; private set; }
public Notification Notification { get; private set; } = notification;
public DeliveryChannel Channel { get; private set; } = channel;
public DeliveryStatus Status { get; private set; } = enabled ? DeliveryStatus.Pending : DeliveryStatus.Skipped;
public DateTime? NextAttemptAt { get; private set; } = enabled ? now : null;
}
public sealed class PushDevice
{
public int Id { get; private set; }
public int UserId { get; private set; }
}
@@ -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; }
}