/// <Summary>Gets directly from the database, not session. Stores in session.</Summary> /// <Summary>Saves changes to this election</Summary> public JsonResult SaveElection(Election electionFromBrowser) { var electionCacher = new ElectionCacher(Db); var election = UserSession.CurrentElection; Db.Election.Attach(election); var beforeChanges = election.GetAllProperties(); var currentType = election.ElectionType; var currentMode = election.ElectionMode; var currentNum = election.NumberToElect; // var currentCan = election.CanVote; // var currentReceive = election.CanReceive; var currentListed = election.ListForPublic; // List of fields to allow edit from setup page var editableFields = new { election.Name, election.DateOfElection, election.Convenor, election.ElectionType, election.ElectionMode, election.NumberToElect, election.NumberExtra, // election.CanVote, // election.CanReceive, election.ListForPublic, election.ShowAsTest, election.ElectionPasscode, //election.UseCallInButton, - use VotingMethods now election.HidePreBallotPages, election.MaskVotingMethod, election.BallotProcessRaw, election.EnvNumModeRaw, election.T24, election.OnlineWhenOpen, election.OnlineWhenClose, election.OnlineCloseIsEstimate, election.OnlineSelectionProcess, election.EmailFromAddress, election.EmailFromName, election.VotingMethods, election.CustomMethods, }.GetAllPropertyInfos().Select(pi => pi.Name).ToList(); // if (election.ElectionType == ElectionTypeEnum.Oth) // { // editableFields.Add(election.CanVote); // editableFields.Add(election.CanReceive); // } if (!currentListed.AsBoolean() && election.ListForPublic.AsBoolean()) { // just turned on election.ListedForPublicAsOf = DateTime.Now; } var changed = electionFromBrowser.CopyPropertyValuesTo(election, editableFields); var coreSettingsChanged = currentMode != election.ElectionMode || currentType != election.ElectionType || currentNum != election.NumberToElect; // || currentCan != election.CanVote // || currentReceive != election.CanReceive; if (coreSettingsChanged) { var setupModel = new SetupModel(); if (setupModel.HasBallots || setupModel.HasOnlineBallots) { return(new { success = false, Status = "Cannot change type of election after ballots are entered." }.AsJsonResult()); } } var locationModel = new LocationModel(); var onlineLocation = locationModel.GetOnlineLocation(); if (changed) { // check if trying to turn off online elections if (!election.OnlineEnabled) { if (onlineLocation != null) { if (locationModel.IsLocationInUse(onlineLocation.LocationGuid)) { beforeChanges.CopyPropertyValuesTo(election); // we have ballots from Online... can't remove Open date! return(new { success = false, Status = "Online ballots received. Cannot disable online voting.", Election = election, displayName = UserSession.CurrentElectionDisplayNameAndInfo }.AsJsonResult()); } } } Db.SaveChanges(); electionCacher.UpdateItemAndSaveCache(election); new PublicHub() .TellPublicAboutVisibleElections(); // in case the name, or ListForPublic, etc. has changed } if (coreSettingsChanged) { // reset flags // new PeopleModel().SetInvolvementFlagsToDefault(); --> must re-import list to apply settings to everyone // update analysis new ResultsModel().GenerateResults(); } // adjust for Online if (election.OnlineEnabled) { if (onlineLocation == null) { // need a new location for online! locationModel.EditLocation(0, LocationModel.OnlineLocationName, true); } } else { // not enabled if (onlineLocation != null) { // remove it (already checked that it is not in use) locationModel.EditLocation(onlineLocation.C_RowId, "", true); } } new AllVotersHub() .UpdateVoters(new { changed = true, election.OnlineWhenClose, election.OnlineWhenOpen, election.OnlineCloseIsEstimate, election.OnlineSelectionProcess }); new FrontDeskHub() .UpdateOnlineElection(new { election.OnlineWhenClose, election.OnlineWhenOpen, election.OnlineCloseIsEstimate, }); // alert will go out when the scheduled job runs return(new { success = true, Status = "Saved", defaultFromAddress = UserSession.CurrentElection.EmailFromAddressWithDefault, Election = election, displayName = UserSession.CurrentElectionDisplayNameAndInfo }.AsJsonResult()); }
// public Election GetFreshFromDb(Guid electionGuid) // { // return Election.ThisElectionCached;// Db.Election.FirstOrDefault(e => e.ElectionGuid == electionGuid); // } /// <Summary>Gets directly from the database, not session. Stores in session.</Summary> /// <Summary>Saves changes to this election</Summary> public JsonResult SaveElection(Election electionFromBrowser) { var Db = GetNewDbContext(); // var election = Db.Election.SingleOrDefault(e => e.Id == electionFromBrowser.Id); var electionCacher = new ElectionCacher(Db); var election = UserSession.CurrentElection; Db.Election.Attach(election); var currentType = election.ElectionType; var currentMode = election.ElectionMode; var currentCan = election.CanVote; var currentReceive = election.CanReceive; var currentListed = election.ListForPublic; // List of fields to allow edit from setup page var editableFields = new { election.Name, election.DateOfElection, election.Convenor, election.ElectionType, election.ElectionMode, election.NumberToElect, election.NumberExtra, election.CanVote, election.CanReceive, election.ListForPublic, election.ShowAsTest, election.ElectionPasscode, election.UseCallInButton, election.HidePreBallotPages, election.MaskVotingMethod, election.BallotProcess, election.EnvNumMode, election.T24, }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray(); if (!currentListed.AsBoolean() && election.ListForPublic.AsBoolean()) { // just turned on election.ListedForPublicAsOf = DateTime.Now; } var changed = electionFromBrowser.CopyPropertyValuesTo(election, editableFields); if (changed) { Db.SaveChanges(); electionCacher.UpdateItemAndSaveCache(election); Startup.GetService <IPublicHubHelper>().TellPublicAboutVisibleElections(); // in case the name, or ListForPublic, etc. has changed } if (currentMode != election.ElectionMode || currentType != election.ElectionType || currentCan != election.CanVote || currentReceive != election.CanReceive ) { // reset flags new PeopleModel().SetInvolvementFlagsToDefault(); // update analysis new ResultsModel().GenerateResults(); } var displayName = UserSession.CurrentElectionDisplayNameAndInfo; return(new { Status = "Saved", Election = election, displayName }.AsJsonResult()); }