Example #1
0
        private UserPolicyAnswersHistoryRecord CopyForHistory(UserPolicyAnswersRecord originalRecord)
        {
            UserPolicyAnswersHistoryRecord recordForHistory = new UserPolicyAnswersHistoryRecord();

            recordForHistory.Accepted    = originalRecord.Accepted;
            recordForHistory.AnswerDate  = originalRecord.AnswerDate;
            recordForHistory.EndValidity = DateTime.UtcNow.AddSeconds(-1);
            recordForHistory.PolicyTextInfoPartRecord = originalRecord.PolicyTextInfoPartRecord;
            recordForHistory.UserPolicyPartRecord     = originalRecord.UserPolicyPartRecord;
            recordForHistory.UserPartRecord           = originalRecord.UserPartRecord;
            return(recordForHistory);
        }
Example #2
0
        public void PolicyForItemUpdate(PolicyForUserViewModel viewModel, ContentItem item)
        {
            if (item.As <UserPolicyPart>() == null)
            {
                return;                                    // if the content item has not the UserPolicyPart, i cannot save the answers, so we skip the update.
            }
            UserPolicyAnswersRecord record       = null;
            var            currentUser           = _workContext.GetContext().CurrentUser;
            UserPartRecord currentUserPartRecord = null;

            if (currentUser != null)
            {
                currentUserPartRecord = currentUser.ContentItem.As <UserPart>().Record;
            }
            else if (item.ContentType == "User")
            {
                // in fase di registrazione di un nuovo utente
                currentUserPartRecord = item.As <UserPart>().Record;
            }

            // Recupero la risposta precedente, se esiste
            if (viewModel.AnswerId > 0)
            {
                record = _userPolicyAnswersRepository.Get(viewModel.AnswerId);
            }
            else
            {
                record = _userPolicyAnswersRepository.Table.Where(w => w.PolicyTextInfoPartRecord.Id == viewModel.PolicyTextId && w.UserPolicyPartRecord.Id == item.Id).SingleOrDefault();
            }

            bool oldAnswer = record != null ? record.Accepted : false;

            // Entro nella funzione solo se il valore della nuova risposta รจ diverso da quello della precedente o se si tratta della prima risposta
            if ((oldAnswer != viewModel.Accepted) || (record == null))
            {
                var policyText = _contentManager.Get <PolicyTextInfoPart>(viewModel.PolicyTextId).Record;
                if ((policyText.UserHaveToAccept && viewModel.Accepted) || !policyText.UserHaveToAccept)
                {
                    var shouldCreateRecord = false;
                    if (item != null)
                    {
                        if (viewModel.AnswerId <= 0 && record == null)
                        {
                            record             = new UserPolicyAnswersRecord();
                            shouldCreateRecord = true;
                        }
                        UserPolicyAnswersHistoryRecord recordForHistory = CopyForHistory(record);

                        //date and user should be updated only if it is a new record, or the answer has actually changed
                        record.AnswerDate = (shouldCreateRecord || oldAnswer != viewModel.Accepted) ? DateTime.UtcNow : record.AnswerDate;
                        if (shouldCreateRecord || oldAnswer != viewModel.Accepted)
                        {
                            if (currentUserPartRecord == null && viewModel.UserId.HasValue)
                            {
                                // utilizza il valore del viewModel
                                var userPart = _contentManager.Get <UserPart>(viewModel.UserId.Value);
                                currentUserPartRecord = (userPart == null) ? null : userPart.Record;
                            }
                            record.UserPartRecord = currentUserPartRecord;
                        }
                        record.Accepted                 = viewModel.Accepted;
                        record.UserPolicyPartRecord     = item.As <UserPolicyPart>().Record;
                        record.PolicyTextInfoPartRecord = policyText;
                        if (shouldCreateRecord)
                        {
                            _userPolicyAnswersRepository.Create(record);
                            _policyEventHandler.PolicyChanged(new PolicyEventViewModel {
                                policyType             = record.PolicyTextInfoPartRecord.PolicyType,
                                accepted               = record.Accepted,
                                ItemPolicyPartRecordId = item.Id
                            });
                        }
                        else if (record.Accepted != recordForHistory.Accepted)
                        {
                            _userPolicyAnswersHistoryRepository.Create(recordForHistory);
                            _userPolicyAnswersRepository.Update(record);
                            _policyEventHandler.PolicyChanged(new PolicyEventViewModel {
                                policyType             = record.PolicyTextInfoPartRecord.PolicyType,
                                accepted               = record.Accepted,
                                ItemPolicyPartRecordId = item.Id
                            });
                        }
                    }
                }
                else if (policyText.UserHaveToAccept && !viewModel.Accepted && record != null)
                {
                    UserPolicyAnswersHistoryRecord recordForHistory = CopyForHistory(record);
                    _userPolicyAnswersHistoryRepository.Create(recordForHistory);
                    _userPolicyAnswersRepository.Delete(record);
                    _policyEventHandler.PolicyChanged(new PolicyEventViewModel {
                        policyType             = recordForHistory.PolicyTextInfoPartRecord.PolicyType,
                        accepted               = false,
                        ItemPolicyPartRecordId = item.Id
                    });
                }
            }
        }