Exemplo n.º 1
0
        /// <summary>
        ///   Process each person record, preparing it BEFORE the election starts. Altered... too dangerous to wipe information!
        /// </summary>
        public void SetInvolvementFlagsToDefault()
        {
            var Db           = SharedDbContext;
            var personCacher = new PersonCacher(Db);

            personCacher.DropThisCache();

            var peopleInElection = personCacher.AllForThisElection;
            var reason           = new ElectionModel().GetDefaultIneligibleReason();
            var counter          = 0;

            foreach (var person in peopleInElection)
            {
                SetInvolvementFlagsToDefault(person, reason);

                if (counter++ > 500)
                {
                    Db.SaveChanges();
                    counter = 0;
                }
            }

            Db.SaveChanges();
        }
Exemplo n.º 2
0
        public JsonResult SavePerson(Person personFromInput)
        {
            if (UserSession.CurrentElectionStatus == ElectionTallyStatusEnum.Finalized)
            {
                return(new { Message = UserSession.FinalizedNoChangesMessage }.AsJsonResult());
            }

            var Db = SharedDbContext;

            var personInDatastore = PeopleInElection.SingleOrDefault(p => p.Id == personFromInput.Id);
            var changed           = false;

            if (personInDatastore == null)
            {
                if (personFromInput.Id != -1)
                {
                    return(new
                    {
                        Message = "Unknown ID"
                    }.AsJsonResult());
                }

                // create new
                personInDatastore = new Person
                {
                    PersonGuid   = Guid.NewGuid(),
                    ElectionGuid = CurrentElectionGuid
                };

                var reason = new ElectionModel().GetDefaultIneligibleReason();
                SetInvolvementFlagsToDefault(personInDatastore, reason);
                Db.Person.Add(personInDatastore);

                PeopleInElection.Add(personInDatastore);

                changed = true;
            }
            else
            {
                Db.Person.Attach(personInDatastore);
            }

            if (personFromInput.IneligibleReasonGuid == Guid.Empty)
            {
                personFromInput.IneligibleReasonGuid = null;
            }

            var editableFields = new
            {
                // personFromInput.AgeGroup,
                personFromInput.BahaiId,
                personFromInput.FirstName,
                personFromInput.IneligibleReasonGuid,
                personFromInput.LastName,
                personFromInput.OtherInfo,
                personFromInput.OtherLastNames,
                personFromInput.OtherNames,
                personFromInput.Area,
            }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray();


            changed = personFromInput.CopyPropertyValuesTo(personInDatastore, editableFields) || changed;

            // these two may not be present, depending on the election type
            const string all             = ElectionModel.CanVoteOrReceive.All;
            var          canReceiveVotes = personFromInput.CanReceiveVotes.GetValueOrDefault(CurrentElection.CanReceive == all);

            if (personInDatastore.CanReceiveVotes != canReceiveVotes)
            {
                personInDatastore.CanReceiveVotes = canReceiveVotes;
                changed = true;
            }

            var canVote = personFromInput.CanVote.GetValueOrDefault(CurrentElection.CanVote == all);

            if (personInDatastore.CanVote != canVote)
            {
                personInDatastore.CanVote = canVote;
                changed = true;
            }

            if (changed)
            {
                SetCombinedInfos(personInDatastore);

                Db.SaveChanges();

                new PersonCacher(Db).ReplaceEntireCache(PeopleInElection);

                UpdateFrontDeskListing(personInDatastore);
            }

            var persons = new PersonCacher(Db).AllForThisElection;

            return(new
            {
                Status = "Saved",
                Person = PersonForEdit(personInDatastore),
                OnFile = persons.Count(),
                Eligible = persons.Count(p => p.IneligibleReasonGuid == null),
            }.AsJsonResult());
        }