web: detail závady s průběhem řešení, komentáři, přílohami a akcemi
This commit is contained in:
1 parent
5d8b71c435
commit
ac3ec6aff7
14 files changed
+1006
-56
No files matched your search
@@ -25,6 +25,11 @@ public sealed class ApiException(HttpStatusCode statusCode, string? title, IRead
|
||||
_ => "Požadavek se nepodařilo zpracovat. Zkuste to prosím znovu.",
|
||||
};
|
||||
|
||||
/// <summary>Chyby polí, pokud je API poslalo, jinak <see cref="UserMessage"/> – pro zprávu mimo formulář.</summary>
|
||||
public string AllMessages => Errors.Count == 0
|
||||
? UserMessage
|
||||
: string.Join(" ", Errors.Values.SelectMany(messages => messages).Distinct());
|
||||
|
||||
public static async Task<ApiException> FromResponseAsync(HttpResponseMessage response, CancellationToken ct)
|
||||
{
|
||||
string? title = null;
|
||||
|
||||
@@ -67,8 +67,105 @@ public sealed record CreateIssueRequest(string Title, string Description, int Ca
|
||||
/// <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 (zatím jen pole, která web používá).</summary>
|
||||
public sealed record IssueDetail(int Id, string Title, string Status, int RowVersion, IReadOnlyList<string> AllowedActions);
|
||||
/// <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);
|
||||
@@ -51,6 +51,111 @@ public sealed class HelpdeskApiClient(HttpClient http)
|
||||
if (!string.IsNullOrWhiteSpace(request.LocationDetail))
|
||||
content.Add(new StringContent(request.LocationDetail), "locationDetail");
|
||||
|
||||
AddFiles(content, files);
|
||||
|
||||
using var response = await http.PostAsync("issues", content, ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>Detail včetně <c>allowedActions</c> a aktuální <c>rowVersion</c>.</summary>
|
||||
public async Task<IssueDetail> GetIssueAsync(int id, CancellationToken ct)
|
||||
{
|
||||
using var response = await http.GetAsync(IssuePath(id), ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>Časová osa – změny stavu i komentáře od nejstarších.</summary>
|
||||
public async Task<IReadOnlyList<IssueEventDto>> GetIssueEventsAsync(int id, CancellationToken ct)
|
||||
{
|
||||
using var response = await http.GetAsync(IssuePath(id, "events"), ct);
|
||||
return await ApiResponse.ReadAsync<List<IssueEventDto>>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>Úprava hlášení hlásičem, dokud je ve stavu Přijata.</summary>
|
||||
public async Task<IssueDetail> EditIssueAsync(int id, EditIssueRequest request, CancellationToken ct)
|
||||
{
|
||||
using var response = await http.PutAsJsonAsync(IssuePath(id), request, ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>Přiřazení nebo přeřazení řešiteli (T2, T3), jen vedení.</summary>
|
||||
public async Task<IssueDetail> AssignIssueAsync(int id, AssignIssueRequest request, CancellationToken ct)
|
||||
{
|
||||
using var response = await http.PostAsJsonAsync(IssuePath(id, "assign"), request, ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Změna stavu přes akční endpoint (T4 – T9). <paramref name="text"/> je důvod u zamítnutí
|
||||
/// a znovuotevření, nebo nepovinná poznámka u předání k ověření.
|
||||
/// </summary>
|
||||
public async Task<IssueDetail> TransitionIssueAsync(int id, string action, int rowVersion, string? text, CancellationToken ct)
|
||||
{
|
||||
var path = IssueActions.TransitionPath(action) ?? throw new ArgumentException($"Neznámá akce {action}.", nameof(action));
|
||||
object body = action switch
|
||||
{
|
||||
IssueActions.SubmitForVerification => new { rowVersion, note = text },
|
||||
IssueActions.Reopen or IssueActions.Reject => new { rowVersion, reason = text },
|
||||
_ => new { rowVersion },
|
||||
};
|
||||
using var response = await http.PostAsJsonAsync(IssuePath(id, path), body, ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
public async Task<IssueEventDto> AddCommentAsync(int id, string text, CancellationToken ct)
|
||||
{
|
||||
using var response = await http.PostAsJsonAsync(IssuePath(id, "comments"), new CommentRequest(text), ct);
|
||||
return await ApiResponse.ReadAsync<IssueEventDto>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>Doplnění příloh k existující závadě (FR-11), 1–5 souborů.</summary>
|
||||
public async Task<IReadOnlyList<AttachmentDto>> AddAttachmentsAsync(int id, IReadOnlyCollection<UploadFile> files, CancellationToken ct)
|
||||
{
|
||||
using var content = new MultipartFormDataContent();
|
||||
AddFiles(content, files);
|
||||
using var response = await http.PostAsync(IssuePath(id, "attachments"), content, ct);
|
||||
return await ApiResponse.ReadAsync<List<AttachmentDto>>(response, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stažení přílohy. Odpověď se nečte do paměti – web ji jako proud předá prohlížeči
|
||||
/// a po odeslání ji uvolní (<see cref="ApiFile.Dispose"/>).
|
||||
/// </summary>
|
||||
public async Task<ApiFile> DownloadAttachmentAsync(int attachmentId, CancellationToken ct)
|
||||
{
|
||||
var response = await http.GetAsync($"attachments/{attachmentId.ToString(CultureInfo.InvariantCulture)}",
|
||||
HttpCompletionOption.ResponseHeadersRead, ct);
|
||||
try
|
||||
{
|
||||
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||
var disposition = response.Content.Headers.ContentDisposition;
|
||||
var fileName = (disposition?.FileNameStar ?? disposition?.FileName)?.Trim('"');
|
||||
return new ApiFile(
|
||||
await response.Content.ReadAsStreamAsync(ct),
|
||||
response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream",
|
||||
string.IsNullOrWhiteSpace(fileName) ? $"priloha-{attachmentId}" : fileName,
|
||||
response);
|
||||
}
|
||||
catch
|
||||
{
|
||||
response.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Řešitelé s počtem otevřených závad – jen pro vedení.</summary>
|
||||
public async Task<IReadOnlyList<AssignableUserDto>> GetAssignableUsersAsync(CancellationToken ct)
|
||||
{
|
||||
using var response = await http.GetAsync("users/assignable", ct);
|
||||
return await ApiResponse.ReadAsync<List<AssignableUserDto>>(response, ct);
|
||||
}
|
||||
|
||||
private static string IssuePath(int id, string? action = null) =>
|
||||
action is null ? $"issues/{id.ToString(CultureInfo.InvariantCulture)}" : $"issues/{id.ToString(CultureInfo.InvariantCulture)}/{action}";
|
||||
|
||||
/// <summary>Soubory z formuláře jdou do API jako proud v poli <c>files</c>, web je nenačítá do paměti.</summary>
|
||||
private static void AddFiles(MultipartFormDataContent content, IEnumerable<UploadFile> files)
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
var part = new StreamContent(file.OpenRead());
|
||||
@@ -59,9 +164,6 @@ public sealed class HelpdeskApiClient(HttpClient http)
|
||||
: new MediaTypeHeaderValue("application/octet-stream");
|
||||
content.Add(part, "files", file.FileName);
|
||||
}
|
||||
|
||||
using var response = await http.PostAsync("issues", content, ct);
|
||||
return await ApiResponse.ReadAsync<IssueDetail>(response, ct);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<CategoryDto>> GetIssueCategoriesAsync(CancellationToken ct)
|
||||
|
||||
Reference in new issue
Block a user