using System; using System.Collections.Generic; using System.Web.Mvc; using Neo.Afx.Common; using Neo.LegitimateLicences.Common.Helpers; using Neo.LegitimateLicences.LicenceIssueRequest.Models.Database; namespace Neo.LegitimateLicences.LicenceIssueRequest { public partial class LicenceIssueRequestController { #region SaveDateOfExpiry (autosave) // The Licence Issuing Details form renders on every workflow step (Data Capture, // Printing, etc.) but the Save button is gated to Data Capture only. The clerk // captures DateOfExpiry on this form — at any step before the licence is printed // — so we autosave the picker via this lightweight endpoint instead of requiring // a Save click. Once DateOfIssue is stamped at first print, this endpoint becomes // a no-op (UpdateLicenceDateOfExpiry has the `WHERE DateOfIssue IS NULL` guard). [HttpPost] public JsonNetResult SaveDateOfExpiry(long licenceIssueDetailID, DateTime? dateOfExpiry) { try { if (!dateOfExpiry.HasValue) { return new JsonNetResult { Data = new JsonBaseResult { Success = false, ErrorCode = "ERROR", Error = "Date of Expiry is required." } }; } if (dateOfExpiry.Value.Date < DateTime.Today) { return new JsonNetResult { Data = new JsonBaseResult { Success = false, ErrorCode = "ERROR", Error = "Date of Expiry cannot be in the past." } }; } // Lightweight lookup — autosave fires on every picker change and must be fast. // GetLicenceIssueForm pulls the whole vehicle/member/routes/services graph and // was causing ~20s hangs on the autosave roundtrip. var liDb = new LicenceIssueDatabase(); var state = liDb.GetLicenceIdAndIssueState(licenceIssueDetailID); if (state == null) { return new JsonNetResult { Data = new JsonBaseResult { Success = false, ErrorCode = "ERROR", Error = "Licence not found." } }; } // DateOfExpiry remains editable for the life of the licence, including // after first print, so the clerk can correct mistakes / extend validity. liDb.UpdateLicenceDateOfExpiry(state.LicenceID, dateOfExpiry); return new JsonNetResult { Data = new JsonBaseResult { Success = true, Error = "", ErrorCode = "" } }; } catch (Exception ex) { var additionalData = new Dictionary { { "licenceIssueDetailID", licenceIssueDetailID }, { "dateOfExpiry", dateOfExpiry } }; var errorMessage = ErrorHelper.ProcessError(ex, "LicenceIssueRequest", "SaveDateOfExpiry", additionalData); return new JsonNetResult { Data = new JsonBaseResult { Success = false, ErrorCode = "ERROR", Error = errorMessage } }; } } #endregion } }