185 lines
7.9 KiB
C#
185 lines
7.9 KiB
C#
namespace Helpdesk.Web.Api;
|
||
|
||
// Kontrakt REST API /api/v1 tak, jak ho web používá. Web je tenký klient a na projekty
|
||
// backendu nemá referenci – DTO jsou zde zrcadlem dokumentace ve Swaggeru.
|
||
|
||
public sealed record UserDto(
|
||
int Id,
|
||
string Email,
|
||
string FirstName,
|
||
string LastName,
|
||
string? Phone,
|
||
string Role,
|
||
bool EmailNotificationsEnabled)
|
||
{
|
||
public string FullName => $"{FirstName} {LastName}";
|
||
}
|
||
|
||
public sealed record AuthResponse(
|
||
string AccessToken,
|
||
DateTimeOffset AccessTokenExpiresAt,
|
||
string RefreshToken,
|
||
DateTimeOffset RefreshTokenExpiresAt,
|
||
UserDto User)
|
||
{
|
||
public TokenSet Tokens => new(AccessToken, AccessTokenExpiresAt, RefreshToken, RefreshTokenExpiresAt);
|
||
}
|
||
|
||
/// <summary>Pár tokenů uložený v autentizační cookie webu.</summary>
|
||
public sealed record TokenSet(
|
||
string AccessToken,
|
||
DateTimeOffset AccessTokenExpiresAt,
|
||
string RefreshToken,
|
||
DateTimeOffset RefreshTokenExpiresAt);
|
||
|
||
public sealed record LoginRequest(string Email, string Password);
|
||
|
||
public sealed record RegisterRequest(string Email, string FirstName, string LastName);
|
||
|
||
public sealed record RefreshTokenRequest(string RefreshToken);
|
||
|
||
public sealed record ConfirmEmailRequest(string Token, string NewPassword);
|
||
|
||
public sealed record ForgotPasswordRequest(string Email);
|
||
|
||
public sealed record ResetPasswordRequest(string Token, string NewPassword);
|
||
|
||
public sealed record UpdateProfileRequest(string FirstName, string LastName, string? Phone, bool EmailNotificationsEnabled);
|
||
|
||
public sealed record AdminUserDto(
|
||
int Id, string Email, string FirstName, string LastName, string Role, bool IsActive, bool EmailConfirmed, DateTimeOffset CreatedAt)
|
||
{
|
||
public string FullName => $"{FirstName} {LastName}";
|
||
}
|
||
|
||
public sealed record AdminUserDetailsDto(int Id, string Email, string FirstName, string LastName, string? Phone,
|
||
string Role, bool IsActive, bool EmailConfirmed, bool EmailNotificationsEnabled, int RowVersion,
|
||
bool? VerificationEmailSent = null);
|
||
|
||
public sealed record SaveAdminUserRequest(string Email, string FirstName, string LastName, string? Phone,
|
||
string Role, bool IsActive, bool EmailConfirmed, bool EmailNotificationsEnabled, string? Password, int? RowVersion);
|
||
|
||
/// <summary>Stránkovaný seznam z API: <c>{ items, totalCount, page, pageSize }</c>.</summary>
|
||
public sealed record PagedResult<T>(IReadOnlyList<T> Items, int TotalCount, int Page, int PageSize)
|
||
{
|
||
public int PageCount => PageSize <= 0 ? 0 : (int)Math.Ceiling(TotalCount / (double)PageSize);
|
||
}
|
||
|
||
public sealed record CategoryDto(int Id, string Name, string? Description, bool IsActive);
|
||
|
||
public sealed record BuildingDto(int Id, string Code, string Name, string? Address, bool IsActive);
|
||
|
||
/// <summary>Závada v seznamu. Kategorie a budova jsou jen ID – názvy doplňuje web z číselníků.</summary>
|
||
public sealed record IssueSummary(
|
||
int Id, string Title, int CategoryId, int? BuildingId, string? LocationDetail, string Status,
|
||
string Priority, int ReporterId, int? AssigneeId, DateTimeOffset CreatedAt);
|
||
|
||
/// <summary>Nové hlášení. Přílohy jdou ve stejném požadavku (multipart, pole <c>files</c>).</summary>
|
||
public sealed record CreateIssueRequest(string Title, string Description, int CategoryId, int? BuildingId, string? LocationDetail);
|
||
|
||
/// <summary>Soubor přeposílaný z formuláře webu do API.</summary>
|
||
public sealed record UploadFile(string FileName, string ContentType, Func<Stream> OpenRead);
|
||
|
||
/// <summary>
|
||
/// Detail závady. <c>AllowedActions</c> říká, co smí aktuální uživatel udělat (kódy z <see cref="IssueActions"/>),
|
||
/// <c>RowVersion</c> se posílá zpět s každou úpravou a změnou stavu (optimistické zamykání, jinak 409).
|
||
/// </summary>
|
||
public sealed record IssueDetail(
|
||
int Id, string Title, string Description, int CategoryId, int? BuildingId, string? LocationDetail, string Status,
|
||
string Priority, int ReporterId, int? AssigneeId, DateTimeOffset CreatedAt, int RowVersion,
|
||
IReadOnlyList<string> AllowedActions, IReadOnlyList<AttachmentDto> Attachments)
|
||
{
|
||
public bool Can(string action) => AllowedActions?.Contains(action) == true;
|
||
}
|
||
|
||
public sealed record AttachmentDto(int Id, string FileName, string ContentType, long SizeBytes, DateTimeOffset CreatedAt);
|
||
|
||
/// <summary>Událost na časové ose závady – vytvoření, přiřazení, změna stavu i komentář (FR-17).</summary>
|
||
public sealed record IssueEventDto(
|
||
int Id, string EventType, string? FromStatus, string ToStatus, int ActorId, string ActorName, string? Note,
|
||
DateTimeOffset OccurredAt);
|
||
|
||
/// <summary>Možný řešitel (jen pro vedení) s počtem jeho otevřených závad (FR-13).</summary>
|
||
public sealed record AssignableUserDto(int Id, string FirstName, string LastName, string Role, int OpenIssues)
|
||
{
|
||
public string FullName => $"{FirstName} {LastName}";
|
||
}
|
||
|
||
public sealed record EditIssueRequest(
|
||
string Title, string Description, int CategoryId, int? BuildingId, string? LocationDetail, int RowVersion);
|
||
|
||
public sealed record AssignIssueRequest(int AssigneeId, string? Priority, int RowVersion);
|
||
|
||
public sealed record CommentRequest(string Text);
|
||
|
||
/// <summary>Stažená příloha. Proud čte přímo odpověď API a uvolní se spolu s ní.</summary>
|
||
public sealed class ApiFile(Stream content, string contentType, string fileName, IDisposable response) : IDisposable
|
||
{
|
||
public Stream Content { get; } = content;
|
||
public string ContentType { get; } = contentType;
|
||
public string FileName { get; } = fileName;
|
||
|
||
public void Dispose() => response.Dispose();
|
||
}
|
||
|
||
/// <summary>Kódy akcí z <c>allowedActions</c> v detailu závady.</summary>
|
||
public static class IssueActions
|
||
{
|
||
public const string Edit = "EDIT";
|
||
public const string Assign = "ASSIGN";
|
||
public const string StartWork = "START_WORK";
|
||
public const string SubmitForVerification = "SUBMIT_FOR_VERIFICATION";
|
||
public const string Confirm = "CONFIRM";
|
||
public const string Reopen = "REOPEN";
|
||
public const string Reject = "REJECT";
|
||
public const string Comment = "COMMENT";
|
||
public const string AddAttachments = "ADD_ATTACHMENTS";
|
||
|
||
/// <summary>Změny stavu v pořadí, v jakém je web nabízí (T4 – T9).</summary>
|
||
public static readonly IReadOnlyList<string> Transitions = [StartWork, SubmitForVerification, Confirm, Reopen, Reject];
|
||
|
||
/// <summary>Akční endpoint <c>POST /issues/{id}/…</c> pro změnu stavu.</summary>
|
||
public static string? TransitionPath(string? action) => action switch
|
||
{
|
||
StartWork => "start",
|
||
SubmitForVerification => "submit-for-verification",
|
||
Confirm => "confirm",
|
||
Reopen => "reopen",
|
||
Reject => "reject",
|
||
_ => null,
|
||
};
|
||
|
||
/// <summary>Zamítnutí a znovuotevření vyžadují důvod (T7 – T9).</summary>
|
||
public static bool RequiresReason(string action) => action is Reopen or Reject;
|
||
|
||
/// <summary>Předání k ověření má nepovinnou poznámku (T5).</summary>
|
||
public static bool AcceptsText(string action) => action is SubmitForVerification || RequiresReason(action);
|
||
|
||
/// <summary>Texty tlačítek – stejné jako v mobilní aplikaci.</summary>
|
||
public static string Label(string action) => action switch
|
||
{
|
||
StartWork => "Zahájit řešení",
|
||
SubmitForVerification => "Předat k ověření",
|
||
Confirm => "Potvrdit vyřešení",
|
||
Reopen => "Znovu otevřít závadu",
|
||
Reject => "Zamítnout závadu",
|
||
_ => action,
|
||
};
|
||
}
|
||
|
||
/// <summary>Priorita závady; názvy jako v mobilní aplikaci.</summary>
|
||
public static class IssuePriorities
|
||
{
|
||
public static readonly IReadOnlyList<string> All = ["LOW", "NORMAL", "HIGH"];
|
||
|
||
public static string Label(string? priority) => priority switch
|
||
{
|
||
"LOW" => "Nízká",
|
||
"HIGH" => "Vysoká",
|
||
_ => "Běžná",
|
||
};
|
||
}
|
||
|
||
/// <summary>Filtry <c>GET /api/v1/issues</c>; prázdné hodnoty se do dotazu neposílají.</summary>
|
||
public sealed record IssueListQuery(string? Status = null, bool Mine = false, string Sort = "newest", int Page = 1, int PageSize = 20);
|