2026-12-02 17:34:58 +01:00
|
|
|
|
using System.Net;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
using Helpdesk.Web.Api;
|
2026-12-02 17:34:58 +01:00
|
|
|
|
using Helpdesk.Web.Infrastructure;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
using Helpdesk.Web.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-12-03 00:14:53 +01:00
|
|
|
|
using Microsoft.Net.Http.Headers;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Hlášení závad (FR-10 – FR-17).</summary>
|
|
|
|
|
|
[Route("issues")]
|
|
|
|
|
|
public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>Moje hlášení – závady, které uživatel nahlásil, od nejnovějších.</summary>
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<IActionResult> Index(string? status, int page = 1, CancellationToken ct = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (status is not null && !IssueListViewModel.Statuses.Contains(status))
|
|
|
|
|
|
status = null;
|
|
|
|
|
|
|
|
|
|
|
|
var issues = await api.GetIssuesAsync(new IssueListQuery(status, Mine: true, Page: Math.Max(page, 1)), ct);
|
|
|
|
|
|
// API vrací u závady jen ID kategorie a budovy – názvy doplníme z číselníků.
|
|
|
|
|
|
var categories = await api.GetIssueCategoriesAsync(ct);
|
|
|
|
|
|
var buildings = await api.GetBuildingsAsync(ct);
|
|
|
|
|
|
|
|
|
|
|
|
return View(new IssueListViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Issues = issues,
|
|
|
|
|
|
CategoryNames = categories.ToDictionary(c => c.Id, c => c.Name),
|
|
|
|
|
|
BuildingNames = buildings.ToDictionary(b => b.Id, b => b.Name),
|
|
|
|
|
|
Status = status,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-12-02 17:34:58 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Formulář nového hlášení (T1 – závada vznikne ve stavu Přijata).</summary>
|
|
|
|
|
|
[HttpGet("new")]
|
|
|
|
|
|
public async Task<IActionResult> New(CancellationToken ct) => View(await WithCatalogsAsync(new NewIssueViewModel(), ct));
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("new")]
|
|
|
|
|
|
[RequestSizeLimit(AttachmentRules.MaxRequestBytes)]
|
|
|
|
|
|
[RequestFormLimits(MultipartBodyLengthLimit = AttachmentRules.MaxRequestBytes)]
|
|
|
|
|
|
public async Task<IActionResult> New(NewIssueViewModel model, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var error in AttachmentRules.Validate(model.Files))
|
|
|
|
|
|
ModelState.AddModelError(nameof(NewIssueViewModel.Files), error);
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
|
return View(await WithCatalogsAsync(model, ct));
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var issue = await api.CreateIssueAsync(
|
|
|
|
|
|
new CreateIssueRequest(model.Title.Trim(), model.Description.Trim(), model.CategoryId!.Value, model.BuildingId, model.LocationDetail?.Trim()),
|
2026-12-03 00:14:53 +01:00
|
|
|
|
ToUploads(model.Files),
|
2026-12-02 17:34:58 +01:00
|
|
|
|
ct);
|
|
|
|
|
|
TempData.SetSuccess($"Závada #{issue.Id} je nahlášená. Vedení školy ji přiřadí řešiteli.");
|
2026-12-03 00:14:53 +01:00
|
|
|
|
return RedirectToAction(nameof(Detail), new { id = issue.Id });
|
2026-12-02 17:34:58 +01:00
|
|
|
|
}
|
2026-12-03 00:14:53 +01:00
|
|
|
|
catch (ApiException ex) when (IsUserError(ex))
|
2026-12-02 17:34:58 +01:00
|
|
|
|
{
|
|
|
|
|
|
ModelState.AddApiErrors(ex, typeof(NewIssueViewModel));
|
|
|
|
|
|
return View(await WithCatalogsAsync(model, ct));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-03 00:14:53 +01:00
|
|
|
|
/// <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")]
|
2026-12-05 01:34:00 +01:00
|
|
|
|
public async Task<IActionResult> Transition(int id, [FromForm] IssueActionForm form, CancellationToken ct)
|
2026-12-03 00:14:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
var action = form.Action ?? "";
|
|
|
|
|
|
if (IssueActions.TransitionPath(action) is null)
|
|
|
|
|
|
return BadRequest();
|
2026-12-05 01:34:00 +01:00
|
|
|
|
if (!ModelState.IsValid || form.RowVersion is not int rowVersion || rowVersion < 0)
|
|
|
|
|
|
return BackToDetail(id, error: InvalidActionFormMessage);
|
2026-12-03 00:14:53 +01:00
|
|
|
|
|
|
|
|
|
|
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ů.");
|
|
|
|
|
|
|
2026-12-05 01:34:00 +01:00
|
|
|
|
return await RunAsync(id, () => api.TransitionIssueAsync(id, action, rowVersion, text, ct), action switch
|
2026-12-03 00:14:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
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")]
|
2026-12-05 01:34:00 +01:00
|
|
|
|
public async Task<IActionResult> Assign(int id, [FromForm] IssueActionForm form, CancellationToken ct)
|
2026-12-03 00:14:53 +01:00
|
|
|
|
{
|
2026-12-05 01:34:00 +01:00
|
|
|
|
if (!ModelState.IsValid || form.RowVersion is not int rowVersion || rowVersion < 0)
|
|
|
|
|
|
return BackToDetail(id, error: InvalidActionFormMessage);
|
2026-12-03 00:14:53 +01:00
|
|
|
|
if (form.AssigneeId is not { } assigneeId)
|
|
|
|
|
|
return BackToDetail(id, error: "Vyberte řešitele.");
|
2026-12-05 01:34:00 +01:00
|
|
|
|
if (form.Priority is not null && !IssuePriorities.All.Contains(form.Priority))
|
|
|
|
|
|
return BackToDetail(id, error: "Vyberte platnou prioritu.");
|
2026-12-03 00:14:53 +01:00
|
|
|
|
|
2026-12-05 01:34:00 +01:00
|
|
|
|
return await RunAsync(id, () => api.AssignIssueAsync(id, new AssignIssueRequest(assigneeId, form.Priority, rowVersion), ct),
|
2026-12-03 00:14:53 +01:00
|
|
|
|
"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")]
|
2026-12-05 01:34:00 +01:00
|
|
|
|
public async Task<IActionResult> Edit(int id, [FromForm] EditIssueViewModel model, CancellationToken ct)
|
2026-12-03 00:14:53 +01:00
|
|
|
|
{
|
|
|
|
|
|
model.Id = id;
|
2026-12-05 01:34:00 +01:00
|
|
|
|
if (model.RowVersion is not int rowVersion || rowVersion < 0)
|
|
|
|
|
|
return BackToDetail(id, error: InvalidActionFormMessage);
|
2026-12-03 00:14:53 +01:00
|
|
|
|
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,
|
2026-12-05 01:34:00 +01:00
|
|
|
|
model.BuildingId, model.LocationDetail?.Trim(), rowVersion), ct);
|
2026-12-03 00:14:53 +01:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-05 01:34:00 +01:00
|
|
|
|
private const string InvalidActionFormMessage =
|
|
|
|
|
|
"Formulář obsahuje chybějící nebo neplatné údaje. Zkontrolujte aktuální detail závady a akci zopakujte.";
|
|
|
|
|
|
|
2026-12-03 00:14:53 +01:00
|
|
|
|
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
|
2026-12-02 17:34:58 +01:00
|
|
|
|
{
|
|
|
|
|
|
model.SetCatalogs(await api.GetIssueCategoriesAsync(ct), await api.GetBuildingsAsync(ct));
|
|
|
|
|
|
return model;
|
|
|
|
|
|
}
|
2026-12-02 13:50:01 +01:00
|
|
|
|
}
|