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
@@ -3,6 +3,7 @@ using Helpdesk.Web.Api;
|
||||
using Helpdesk.Web.Infrastructure;
|
||||
using Helpdesk.Web.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Helpdesk.Web.Controllers;
|
||||
|
||||
@@ -49,21 +50,195 @@ public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
||||
{
|
||||
var issue = await api.CreateIssueAsync(
|
||||
new CreateIssueRequest(model.Title.Trim(), model.Description.Trim(), model.CategoryId!.Value, model.BuildingId, model.LocationDetail?.Trim()),
|
||||
model.Files.Select(f => new UploadFile(f.FileName, f.ContentType, f.OpenReadStream)).ToList(),
|
||||
ToUploads(model.Files),
|
||||
ct);
|
||||
TempData.SetSuccess($"Závada #{issue.Id} je nahlášená. Vedení školy ji přiřadí řešiteli.");
|
||||
return RedirectToAction(nameof(Index));
|
||||
return RedirectToAction(nameof(Detail), new { id = issue.Id });
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.RequestEntityTooLarge
|
||||
or HttpStatusCode.UnsupportedMediaType or HttpStatusCode.TooManyRequests)
|
||||
catch (ApiException ex) when (IsUserError(ex))
|
||||
{
|
||||
// 400 validace nebo nepovolený obsah souboru, 413 příliš velké přílohy, 429 limit zápisů (10/min).
|
||||
ModelState.AddApiErrors(ex, typeof(NewIssueViewModel));
|
||||
return View(await WithCatalogsAsync(model, ct));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<NewIssueViewModel> WithCatalogsAsync(NewIssueViewModel model, CancellationToken ct)
|
||||
/// <summary>Detail závady s časovou osou, přílohami a akcemi podle <c>allowedActions</c>.</summary>
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<IActionResult> Detail(int id, CancellationToken ct)
|
||||
{
|
||||
// Postupně, ne paralelně: všechna volání sdílejí HttpContext (token v cookie, jeho obnova).
|
||||
var issue = await api.GetIssueAsync(id, ct);
|
||||
var events = await api.GetIssueEventsAsync(id, ct);
|
||||
var categories = await api.GetIssueCategoriesAsync(ct);
|
||||
var buildings = await api.GetBuildingsAsync(ct);
|
||||
IReadOnlyList<AssignableUserDto> assignees = issue.Can(IssueActions.Assign) ? await api.GetAssignableUsersAsync(ct) : [];
|
||||
|
||||
return View(new IssueDetailViewModel
|
||||
{
|
||||
Issue = issue,
|
||||
Events = events,
|
||||
CategoryName = categories.FirstOrDefault(c => c.Id == issue.CategoryId)?.Name ?? "Neznámá kategorie",
|
||||
Location = IssueListViewModel.FormatLocation(buildings.FirstOrDefault(b => b.Id == issue.BuildingId)?.Name, issue.LocationDetail),
|
||||
Assignees = assignees,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Změna stavu: zahájit, předat k ověření, potvrdit, znovu otevřít, zamítnout (T4 – T9).</summary>
|
||||
[HttpPost("{id:int}/transition")]
|
||||
public async Task<IActionResult> Transition(int id, IssueActionForm form, CancellationToken ct)
|
||||
{
|
||||
var action = form.Action ?? "";
|
||||
if (IssueActions.TransitionPath(action) is null)
|
||||
return BadRequest();
|
||||
|
||||
var text = string.IsNullOrWhiteSpace(form.Text) || !IssueActions.AcceptsText(action) ? null : form.Text.Trim();
|
||||
if (IssueActions.RequiresReason(action) && text is null)
|
||||
return BackToDetail(id, error: "Napište prosím důvod.");
|
||||
if (text?.Length > IssueActionForm.MaxTextLength)
|
||||
return BackToDetail(id, error: $"Text může mít nejvýše {IssueActionForm.MaxTextLength} znaků.");
|
||||
|
||||
return await RunAsync(id, () => api.TransitionIssueAsync(id, action, form.RowVersion, text, ct), action switch
|
||||
{
|
||||
IssueActions.StartWork => "Řešení závady je zahájené.",
|
||||
IssueActions.SubmitForVerification => "Závada je předaná k ověření. Hlásič potvrdí, jestli je vyřešená.",
|
||||
IssueActions.Confirm => "Děkujeme, závada je vyřešená.",
|
||||
IssueActions.Reopen => "Závada je znovu otevřená a vrací se řešiteli.",
|
||||
_ => "Závada je zamítnutá.",
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Přiřazení nebo přeřazení řešiteli (T2, T3) – jen vedení.</summary>
|
||||
[HttpPost("{id:int}/assign")]
|
||||
public async Task<IActionResult> Assign(int id, IssueActionForm form, CancellationToken ct)
|
||||
{
|
||||
if (form.AssigneeId is not { } assigneeId)
|
||||
return BackToDetail(id, error: "Vyberte řešitele.");
|
||||
var priority = IssuePriorities.All.Contains(form.Priority) ? form.Priority : null;
|
||||
|
||||
return await RunAsync(id, () => api.AssignIssueAsync(id, new AssignIssueRequest(assigneeId, priority, form.RowVersion), ct),
|
||||
"Závada je přiřazená řešiteli.");
|
||||
}
|
||||
|
||||
/// <summary>Komentář hlásiče nebo řešitele (FR-16).</summary>
|
||||
[HttpPost("{id:int}/comments")]
|
||||
public async Task<IActionResult> Comment(int id, IssueActionForm form, CancellationToken ct)
|
||||
{
|
||||
var text = form.Text?.Trim();
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return BackToDetail(id, error: "Napište text komentáře.", fragment: "komentar");
|
||||
if (text.Length > IssueActionForm.MaxTextLength)
|
||||
return BackToDetail(id, error: $"Komentář může mít nejvýše {IssueActionForm.MaxTextLength} znaků.", fragment: "komentar");
|
||||
|
||||
return await RunAsync(id, () => api.AddCommentAsync(id, text, ct), "Komentář je odeslaný.", fragment: "komentar");
|
||||
}
|
||||
|
||||
/// <summary>Doplnění příloh k závadě v průběhu řešení (FR-11).</summary>
|
||||
[HttpPost("{id:int}/attachments")]
|
||||
[RequestSizeLimit(AttachmentRules.MaxRequestBytes)]
|
||||
[RequestFormLimits(MultipartBodyLengthLimit = AttachmentRules.MaxRequestBytes)]
|
||||
public async Task<IActionResult> Attachments(int id, IssueActionForm form, CancellationToken ct)
|
||||
{
|
||||
var error = form.Files.Count == 0 ? "Vyberte alespoň jeden soubor." : AttachmentRules.Validate(form.Files).FirstOrDefault();
|
||||
if (error is not null)
|
||||
return BackToDetail(id, error: error, fragment: "prilohy");
|
||||
|
||||
return await RunAsync(id, () => api.AddAttachmentsAsync(id, ToUploads(form.Files), ct),
|
||||
form.Files.Count == 1 ? "Příloha je nahraná." : "Přílohy jsou nahrané.", fragment: "prilohy");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Příloha přes web: prohlížeč nemá access token, proto ji web stáhne z API a pošle dál jako proud.
|
||||
/// Oprávnění kontroluje API (jen ten, kdo vidí závadu).
|
||||
/// </summary>
|
||||
[HttpGet("attachments/{attachmentId:int}")]
|
||||
public async Task<IActionResult> Attachment(int attachmentId, CancellationToken ct)
|
||||
{
|
||||
var file = await api.DownloadAttachmentAsync(attachmentId, ct);
|
||||
HttpContext.Response.RegisterForDispose(file);
|
||||
// filename i filename* (RFC 6266), aby prohlížeč zachoval diakritiku v názvu souboru.
|
||||
var disposition = new ContentDispositionHeaderValue(IssueDetailViewModel.OpensInBrowser(file.ContentType) ? "inline" : "attachment");
|
||||
disposition.SetHttpFileName(file.FileName);
|
||||
Response.Headers.ContentDisposition = disposition.ToString();
|
||||
return File(file.Content, file.ContentType);
|
||||
}
|
||||
|
||||
/// <summary>Úprava hlášení, dokud je ve stavu Přijata (jen hlásič).</summary>
|
||||
[HttpGet("{id:int}/edit")]
|
||||
public async Task<IActionResult> Edit(int id, CancellationToken ct)
|
||||
{
|
||||
var issue = await api.GetIssueAsync(id, ct);
|
||||
if (!issue.Can(IssueActions.Edit))
|
||||
return BackToDetail(id, error: "Hlášení už nejde upravit – vedení ho mezitím převzalo. Doplňující informace napište do komentáře.");
|
||||
|
||||
return View(await WithCatalogsAsync(EditIssueViewModel.From(issue), ct));
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/edit")]
|
||||
public async Task<IActionResult> Edit(int id, EditIssueViewModel model, CancellationToken ct)
|
||||
{
|
||||
model.Id = id;
|
||||
if (!ModelState.IsValid)
|
||||
return View(await WithCatalogsAsync(model, ct));
|
||||
|
||||
try
|
||||
{
|
||||
await api.EditIssueAsync(id, new EditIssueRequest(model.Title.Trim(), model.Description.Trim(), model.CategoryId!.Value,
|
||||
model.BuildingId, model.LocationDetail?.Trim(), model.RowVersion), ct);
|
||||
return BackToDetail(id, success: "Změny hlášení jsou uložené.");
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.Conflict)
|
||||
{
|
||||
return BackToDetail(id, error: ConflictMessage);
|
||||
}
|
||||
catch (ApiException ex) when (IsUserError(ex))
|
||||
{
|
||||
ModelState.AddApiErrors(ex, typeof(EditIssueViewModel));
|
||||
return View(await WithCatalogsAsync(model, ct));
|
||||
}
|
||||
}
|
||||
|
||||
private const string ConflictMessage =
|
||||
"Závadu mezitím někdo změnil. Níže je její aktuální stav – zkontrolujte ho a akci případně zopakujte.";
|
||||
|
||||
/// <summary>
|
||||
/// Provede akci a vrátí uživatele na detail se zprávou (Post/Redirect/Get). Chyby, které může
|
||||
/// uživatel napravit, ukáže nad detailem; 401/403/404 zpracuje <see cref="ApiExceptionFilter"/>.
|
||||
/// </summary>
|
||||
private async Task<IActionResult> RunAsync(int id, Func<Task> action, string success, string? fragment = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await action();
|
||||
return BackToDetail(id, success: success, fragment: fragment);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.Conflict)
|
||||
{
|
||||
// 409: jiná rowVersion (někdo závadu mezitím změnil), nebo akce v aktuálním stavu už nejde.
|
||||
return BackToDetail(id, error: ConflictMessage);
|
||||
}
|
||||
catch (ApiException ex) when (IsUserError(ex))
|
||||
{
|
||||
return BackToDetail(id, error: ex.AllMessages, fragment: fragment);
|
||||
}
|
||||
}
|
||||
|
||||
private RedirectToActionResult BackToDetail(int id, string? success = null, string? error = null, string? fragment = null)
|
||||
{
|
||||
if (success is not null)
|
||||
TempData.SetSuccess(success);
|
||||
if (error is not null)
|
||||
TempData.SetError(error);
|
||||
return RedirectToAction(nameof(Detail), null, new { id }, fragment);
|
||||
}
|
||||
|
||||
/// <summary>400 validace nebo nepovolený obsah souboru, 413 příliš velké přílohy, 415, 429 limit zápisů (10/min).</summary>
|
||||
private static bool IsUserError(ApiException ex) => ex.StatusCode is HttpStatusCode.BadRequest
|
||||
or HttpStatusCode.RequestEntityTooLarge or HttpStatusCode.UnsupportedMediaType or HttpStatusCode.TooManyRequests;
|
||||
|
||||
private static List<UploadFile> ToUploads(IEnumerable<IFormFile> files) =>
|
||||
files.Select(f => new UploadFile(f.FileName, f.ContentType, f.OpenReadStream)).ToList();
|
||||
|
||||
private async Task<T> WithCatalogsAsync<T>(T model, CancellationToken ct) where T : IssueFormViewModel
|
||||
{
|
||||
model.SetCatalogs(await api.GetIssueCategoriesAsync(ct), await api.GetBuildingsAsync(ct));
|
||||
return model;
|
||||
|
||||
Reference in new issue
Block a user