protected Ballot CreateAndRegisterBallot() { var currentLocationGuid = UserSession.CurrentLocationGuid; if (!currentLocationGuid.HasContent()) { var locationModel = new LocationModel(); currentLocationGuid = locationModel.GetLocations_Physical().First().LocationGuid; UserSession.CurrentLocationGuid = currentLocationGuid; } var computerCode = UserSession.CurrentComputerCode; var ballotCacher = new BallotCacher(Db); var firstBallot = ballotCacher.AllForThisElection.Any(); var ballot = new Ballot { BallotGuid = Guid.NewGuid(), LocationGuid = currentLocationGuid, ComputerCode = computerCode, BallotNumAtComputer = NextBallotNumAtComputer(), StatusCode = BallotStatusEnum.Empty, Teller1 = UserSession.GetCurrentTeller(1), Teller2 = UserSession.GetCurrentTeller(2) }; Db.Ballot.Add(ballot); if (firstBallot) { var locationCacher = new LocationCacher(Db); var location = locationCacher.AllForThisElection.FirstOrDefault(l => l.LocationGuid == currentLocationGuid); if (location != null && location.BallotsCollected.AsInt() == 0) { var ballotSources = new PersonCacher(Db) .AllForThisElection .Count(p => !string.IsNullOrEmpty(p.VotingMethod) && p.VotingLocationGuid == currentLocationGuid); location.BallotsCollected = ballotSources; locationCacher.UpdateItemAndSaveCache(location); } } Db.SaveChanges(); ballotCacher.UpdateItemAndSaveCache(ballot); SessionKey.CurrentBallotId.SetInSession(ballot.C_RowId); return(ballot); //TODO: used to be view }
/// <Summary>Current Ballot... could be null</Summary> public Ballot GetCurrentBallot(bool refresh = false) { var isSingleNameElection = UserSession.CurrentElection.IsSingleNameElection; var currentBallotId = SessionKey.CurrentBallotId.FromSession(0); var ballotCacher = new BallotCacher(Db); var ballot = ballotCacher.GetById(currentBallotId); if (ballot == null && isSingleNameElection) { ballot = ballotCacher.GetByComputerCode(); } if (ballot == null) { if (isSingleNameElection) { // will create empty ballot for this computer... do we need it? // ballot = CreateAndRegisterBallot(); } } else { if (refresh) { Db.Ballot.Attach(ballot); var voteCacher = new VoteCacher(Db); var votes = voteCacher.AllForThisElection; var voteInfos = VoteInfosFor(ballot, votes); SortVotes(voteInfos.OrderBy(vi => vi.PositionOnBallot).Select(v => v.VoteId).ToList(), voteCacher); voteInfos = VoteInfosFor(ballot, votes); BallotAnalyzerLocal.UpdateBallotStatus(ballot, voteInfos, true); ballotCacher.UpdateItemAndSaveCache(ballot); Db.SaveChanges(); } } if (ballot != null && ballot.C_RowId != currentBallotId) { SessionKey.CurrentBallotId.SetInSession(ballot.C_RowId); } return(ballot); }
public bool CreateBallotForOnlineVoter(List <OnlineRawVote> poolList, out string errorMessage) { //{Id: 0, First:"", Last:"", OtherInfo:""} // double check var numberToElect = UserSession.CurrentElection.NumberToElect; if (poolList.Count != numberToElect) { errorMessage = $"Invalid number of votes ({poolList.Count}). Need {numberToElect}."; return(false); } var ballotCacher = new BallotCacher(Db); var voteCacher = new VoteCacher(Db); var locationHelper = new LocationModel(Db); var location = locationHelper.GetOnlineLocation(); // create ballot var ballot = new Ballot { BallotGuid = Guid.NewGuid(), LocationGuid = location.LocationGuid, ComputerCode = ComputerModel.ComputerCodeForOnline, BallotNumAtComputer = 0, // maxNum + 1, // will reset later StatusCode = BallotStatusEnum.Empty, }; Db.Ballot.Add(ballot); Db.SaveChanges(); ballotCacher.UpdateItemAndSaveCache(ballot); // add Votes var nextVoteNum = 0; foreach (var rawVote in poolList) { Vote vote; if (rawVote.Id > 0) { var person = new PersonCacher(Db).AllForThisElection.FirstOrDefault(b => b.C_RowId == rawVote.Id); if (person == null) { errorMessage = $"Error converting pool id {rawVote.Id} to person."; return(false); } vote = new Vote { BallotGuid = ballot.BallotGuid, PositionOnBallot = ++nextVoteNum, StatusCode = VoteStatusCode.Ok, PersonGuid = person.PersonGuid, PersonCombinedInfo = person.CombinedInfo, SingleNameElectionCount = 1, // okay if set for normal election too InvalidReasonGuid = person.CanReceiveVotes.AsBoolean(true) ? null : person.IneligibleReasonGuid }; } else { // "random" vote vote = new Vote { BallotGuid = ballot.BallotGuid, PositionOnBallot = ++nextVoteNum, StatusCode = VoteStatusCode.OnlineRaw, SingleNameElectionCount = 1, OnlineVoteRaw = JsonConvert.SerializeObject(rawVote), }; // attempt to match if it is exact... var matched = new PersonCacher(Db).AllForThisElection // match on first and last name only .Where(p => p.FirstName.ToLower() == rawVote.First.ToLower() && p.LastName.ToLower() == rawVote.Last.ToLower()) // don't match if our list has "otherInfo" for this person - there might be some special considerations .Where(p => p.OtherInfo.HasNoContent()) .ToList(); if (matched.Count == 1) { // found one exact match var person = matched[0]; vote.StatusCode = VoteStatusCode.Ok; vote.PersonGuid = person.PersonGuid; vote.PersonCombinedInfo = person.CombinedInfo; vote.InvalidReasonGuid = person.CanReceiveVotes.AsBoolean(true) ? null : person.IneligibleReasonGuid; } } Db.Vote.Add(vote); Db.SaveChanges(); voteCacher.UpdateItemAndSaveCache(vote); } var votes = voteCacher.AllForThisElection; BallotAnalyzerLocal.UpdateBallotStatus(ballot, VoteInfosFor(ballot, votes), true); ballotCacher.UpdateItemAndSaveCache(ballot); Db.SaveChanges(); errorMessage = ""; return(true); }