Refactor code structure for improved readability and maintainability

This commit is contained in:
Stefan
2026-07-10 14:29:51 +02:00
parent af445c015e
commit 42997c4f49
18 changed files with 1237 additions and 615 deletions
@@ -17,32 +17,26 @@ public partial class IntranetController
/// <summary>Standard 410 when a session token is unknown/expired — the client re-opens the draft.</summary>
private IActionResult DraftGone() => StatusCode(410, new { error = "expired" });
// POST inv/dopen — { id? | payload? } → { token, version }
// POST inv/dopen — { payload } → { token, version }
// The editor assembles the initial draft (from a service request or a reloaded DB draft
// via the existing render paths) and seeds the authoritative session here. Reload/discard
// is the client re-fetching + re-seeding, so there is no server-side DB reshaping.
private async Task<IActionResult> HandleDraftOpen(string fn, string id, string code)
{
InvoiceDraftSession session;
if (HasForm("id") && !string.IsNullOrEmpty(Form("id")))
if (!HasForm("payload"))
{
_logger.LogInformation("Draft dopen: from DB draft {InvId} user={User}", Form("id"), UserAccountID);
session = await _invoiceDrafts.OpenFromDraftAsync(Form("id"), UserAccountID, DbSec);
}
else if (HasForm("payload"))
{
_logger.LogInformation("Draft dopen: from payload user={User}", UserAccountID);
JObject payload;
try { payload = JObject.Parse(Form("payload")); }
catch (JsonException ex)
{
_logger.LogWarning(ex, "Draft dopen: invalid payload JSON user={User}", UserAccountID);
return BadRequest400();
}
session = _invoiceDrafts.OpenFromPayload(payload, UserAccountID);
}
else
{
_logger.LogWarning("Draft dopen: neither 'id' nor 'payload' supplied user={User}", UserAccountID);
_logger.LogWarning("Draft dopen: 'payload' missing user={User}", UserAccountID);
return BadRequest400();
}
JObject payload;
try { payload = JObject.Parse(Form("payload")); }
catch (JsonException ex)
{
_logger.LogWarning(ex, "Draft dopen: invalid payload JSON user={User}", UserAccountID);
return BadRequest400();
}
var session = _invoiceDrafts.OpenFromPayload(payload, UserAccountID);
_logger.LogInformation("Draft dopen: session {Token} (invId={InvId}) user={User}", session.Token, session.InvId, UserAccountID);
// The browser holds the token from this response and fetches dstate directly; there is
// no server 'draftReady' on open (it would race the client's group-join). Signals drive
// only subsequent server-side changes.
@@ -125,16 +119,6 @@ public partial class IntranetController
return await JSONAsync(new { history });
}
// POST inv/ddiscard — { token } → { ok, version }; reload from DB + draftReady
private async Task<IActionResult> HandleDraftDiscard(string fn, string id, string code)
{
if (!HasForm("token")) return BadRequest400();
var session = await _invoiceDrafts.DiscardAsync(Form("token"), UserAccountID, DbSec);
if (session == null) return DraftGone();
await _draftNotifier.SignalDraftReadyAsync(session.Token, session.Version);
return await JSONAsync(new { ok = true, version = session.Version });
}
// POST inv/dclose — { token } → { ok }
private async Task<IActionResult> HandleDraftClose(string fn, string id, string code)
{
@@ -165,7 +165,6 @@ public partial class IntranetController
case "dpreview": return await HandleDraftPreview(fn, id, code);
case "dsave": return await HandleDraftSave(fn, id, code);
case "dhistory": return await HandleDraftHistory(fn, id, code);
case "ddiscard": return await HandleDraftDiscard(fn, id, code);
case "dclose": return await HandleDraftClose(fn, id, code);
default: