Implement issue reporting and workflows
This commit is contained in:
1 parent
6ad3d5bbaa
commit
b80ec57589
23 files changed
+1407
-17
No files matched your search
@@ -0,0 +1,137 @@
|
||||
using Helpdesk.Domain.Users;
|
||||
|
||||
namespace Helpdesk.Domain.Issues;
|
||||
|
||||
public enum IssueStatus { Received, Assigned, InProgress, PendingVerification, Resolved, Rejected }
|
||||
public enum IssuePriority { Low, Normal, High }
|
||||
public enum IssueEventType { Created, Updated, Assigned, StatusChanged, Commented }
|
||||
public enum IssueAction { Edit, Assign, StartWork, SubmitForVerification, Confirm, Reopen, Reject, Comment, AddAttachments }
|
||||
public enum IssueActionAvailability { Allowed, Forbidden, InvalidState }
|
||||
|
||||
public sealed class Issue : IHasCreatedAt
|
||||
{
|
||||
private Issue() { }
|
||||
|
||||
public Issue(int reporterId, string title, string description, int categoryId, int? buildingId, string? locationDetail)
|
||||
{
|
||||
ReporterId = reporterId;
|
||||
SetDetails(title, description, categoryId, buildingId, locationDetail);
|
||||
}
|
||||
|
||||
public int Id { get; private set; }
|
||||
public string Title { get; private set; } = null!;
|
||||
public string Description { get; private set; } = null!;
|
||||
public int CategoryId { get; private set; }
|
||||
public int? BuildingId { get; private set; }
|
||||
public string? LocationDetail { get; private set; }
|
||||
public IssueStatus Status { get; private set; } = IssueStatus.Received;
|
||||
public IssuePriority Priority { get; private set; } = IssuePriority.Normal;
|
||||
public int ReporterId { get; private set; }
|
||||
public int? AssigneeId { get; private set; }
|
||||
public int RowVersion { get; private set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public static bool CanReadAll(UserRole role) => role is UserRole.FacilityManager or UserRole.Management;
|
||||
|
||||
public static bool CanBeAssigned(UserRole role) => role is UserRole.FacilityManager or UserRole.Management;
|
||||
|
||||
public bool CanRead(int userId, UserRole role) => CanReadAll(role) || ReporterId == userId;
|
||||
|
||||
public IssueActionAvailability Availability(IssueAction action, int actorId, UserRole role)
|
||||
{
|
||||
var reporter = ReporterId == actorId;
|
||||
var assignee = AssigneeId == actorId && CanBeAssigned(role);
|
||||
var management = role == UserRole.Management;
|
||||
|
||||
var authorized = action switch
|
||||
{
|
||||
IssueAction.Edit or IssueAction.Confirm or IssueAction.Reopen => reporter,
|
||||
IssueAction.Assign => management,
|
||||
IssueAction.StartWork or IssueAction.SubmitForVerification => assignee,
|
||||
IssueAction.Reject => assignee || (management && AssigneeId is null),
|
||||
IssueAction.Comment or IssueAction.AddAttachments => reporter || assignee,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if (!authorized)
|
||||
return IssueActionAvailability.Forbidden;
|
||||
|
||||
var permittedState = action switch
|
||||
{
|
||||
IssueAction.Edit => Status == IssueStatus.Received,
|
||||
IssueAction.Assign => Status is IssueStatus.Received or IssueStatus.Assigned or IssueStatus.InProgress,
|
||||
IssueAction.StartWork => Status == IssueStatus.Assigned,
|
||||
IssueAction.SubmitForVerification => Status == IssueStatus.InProgress,
|
||||
IssueAction.Confirm or IssueAction.Reopen => Status == IssueStatus.PendingVerification,
|
||||
IssueAction.Reject => Status == IssueStatus.Received
|
||||
? management
|
||||
: assignee && Status is (IssueStatus.Assigned or IssueStatus.InProgress),
|
||||
IssueAction.Comment or IssueAction.AddAttachments => Status is not (IssueStatus.Resolved or IssueStatus.Rejected),
|
||||
_ => false,
|
||||
};
|
||||
return permittedState ? IssueActionAvailability.Allowed : IssueActionAvailability.InvalidState;
|
||||
}
|
||||
|
||||
public void Edit(int actorId, UserRole role, string title, string description, int categoryId,
|
||||
int? buildingId, string? locationDetail)
|
||||
{
|
||||
Require(IssueAction.Edit, actorId, role);
|
||||
SetDetails(title, description, categoryId, buildingId, locationDetail);
|
||||
RowVersion++;
|
||||
}
|
||||
|
||||
public void Assign(int actorId, UserRole role, int assigneeId, UserRole assigneeRole, IssuePriority? priority)
|
||||
{
|
||||
Require(IssueAction.Assign, actorId, role);
|
||||
if (!CanBeAssigned(assigneeRole))
|
||||
throw new ArgumentException("Neplatná role řešitele.", nameof(assigneeRole));
|
||||
if (AssigneeId == assigneeId)
|
||||
throw new ArgumentException("Nový řešitel musí být jiný než stávající.", nameof(assigneeId));
|
||||
|
||||
AssigneeId = assigneeId;
|
||||
Priority = priority ?? Priority;
|
||||
Status = IssueStatus.Assigned;
|
||||
RowVersion++;
|
||||
}
|
||||
|
||||
public void Transition(IssueAction action, int actorId, UserRole role, string? reason)
|
||||
{
|
||||
Require(action, actorId, role);
|
||||
if (action is (IssueAction.Reject or IssueAction.Reopen) && string.IsNullOrWhiteSpace(reason))
|
||||
throw new ArgumentException("Důvod je povinný.", nameof(reason));
|
||||
|
||||
Status = action switch
|
||||
{
|
||||
IssueAction.StartWork or IssueAction.Reopen => IssueStatus.InProgress,
|
||||
IssueAction.SubmitForVerification => IssueStatus.PendingVerification,
|
||||
IssueAction.Confirm => IssueStatus.Resolved,
|
||||
IssueAction.Reject => IssueStatus.Rejected,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(action)),
|
||||
};
|
||||
RowVersion++;
|
||||
}
|
||||
|
||||
public void RecordParticipation(IssueAction action, int actorId, UserRole role)
|
||||
{
|
||||
if (action is not (IssueAction.Comment or IssueAction.AddAttachments))
|
||||
throw new ArgumentOutOfRangeException(nameof(action));
|
||||
|
||||
Require(action, actorId, role);
|
||||
RowVersion++;
|
||||
}
|
||||
|
||||
private void Require(IssueAction action, int actorId, UserRole role)
|
||||
{
|
||||
if (Availability(action, actorId, role) != IssueActionAvailability.Allowed)
|
||||
throw new InvalidOperationException("Akce není pro tuto závadu povolena.");
|
||||
}
|
||||
|
||||
private void SetDetails(string title, string description, int categoryId, int? buildingId, string? locationDetail)
|
||||
{
|
||||
Title = title.Trim();
|
||||
Description = description.Trim();
|
||||
CategoryId = categoryId;
|
||||
BuildingId = buildingId;
|
||||
LocationDetail = string.IsNullOrWhiteSpace(locationDetail) ? null : locationDetail.Trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
namespace Helpdesk.Domain.Issues;
|
||||
|
||||
public enum NotificationType { IssueCreated, IssueAssigned, IssueStatusChanged, IssueCommentAdded, IssueReopened }
|
||||
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 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 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,43 @@
|
||||
namespace Helpdesk.Domain.Issues;
|
||||
|
||||
public sealed class IssueEvent
|
||||
{
|
||||
private IssueEvent() { }
|
||||
|
||||
public IssueEvent(Issue issue, IssueEventType eventType, IssueStatus? fromStatus, int actorId, string? note,
|
||||
DateTime occurredAt)
|
||||
{
|
||||
Issue = issue;
|
||||
EventType = eventType;
|
||||
FromStatus = fromStatus;
|
||||
ToStatus = issue.Status;
|
||||
ActorId = actorId;
|
||||
Note = note;
|
||||
OccurredAt = occurredAt;
|
||||
}
|
||||
|
||||
public int Id { get; private set; }
|
||||
public int IssueId { get; private set; }
|
||||
public Issue Issue { get; private set; } = null!;
|
||||
public IssueEventType EventType { get; private set; }
|
||||
public IssueStatus? FromStatus { get; private set; }
|
||||
public IssueStatus 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 Attachment(
|
||||
Issue issue, string originalFileName, string storedFileName, string contentType, long sizeBytes) : IHasCreatedAt
|
||||
{
|
||||
private Attachment() : this(null!, null!, null!, null!, 0) { }
|
||||
|
||||
public int Id { get; private set; }
|
||||
public int IssueId { get; private set; }
|
||||
public Issue Issue { get; private set; } = issue;
|
||||
public string OriginalFileName { get; private set; } = originalFileName;
|
||||
public string StoredFileName { get; private set; } = storedFileName;
|
||||
public string ContentType { get; private set; } = contentType;
|
||||
public long SizeBytes { get; private set; } = sizeBytes;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
Reference in new issue
Block a user