public PreAssessmentCustomerCallQueueCallAttempt Save(PreAssessmentCustomerCallQueueCallAttempt domain, bool refatch = true)
 {
     using (var adapter = PersistenceLayer.GetDataAccessAdapter())
     {
         var entity = Mapper.Map <PreAssessmentCustomerCallQueueCallAttempt, PreAssessmentCustomerCallQueueCallAttemptEntity>(domain);
         if (!adapter.SaveEntity(entity, refatch))
         {
             throw new PersistenceFailureException();
         }
         return(!refatch
             ? domain
             : Mapper.Map <PreAssessmentCustomerCallQueueCallAttemptEntity, PreAssessmentCustomerCallQueueCallAttempt>(entity));
     }
 }
        private long SaveCallQueueCustomerDetailAndGetCallId(Customer customer, CallQueue callQueue, long orgRoleUserId, long callId, long dialerType, long eventId = 0)
        {
            var prospectCustomer = _prospectCustomerRepository.GetProspectCustomerByCustomerId(customer.CustomerId);

            var _event       = _eventRepository.GetById(eventId);
            var HealthPlanId = _eventRepository.GetById(eventId) != null?_eventRepository.GetById(eventId).AccountId : null;

            long?prospectCustomerId_ = null;

            if (prospectCustomer != null)
            {
                prospectCustomerId_ = prospectCustomer.Id;
            }

            using (var scope = new TransactionScope())
            {
                //lock customer in call queue so that other agent not be able to call them;
                var domain = new PreAssessmentCallQueueCustomerLock
                {
                    CustomerId         = customer.CustomerId,
                    ProspectCustomerId = prospectCustomerId_,
                    CreatedBy          = orgRoleUserId,
                    CreatedOn          = DateTime.Now
                };
                _preAssessmentCallQueueCustomerLockRepository.SavePreAssessmentCallQueueCustomerLock(domain);
                var customerId = customer != null ? customer.CustomerId : 0;

                var prospectCustomerId     = prospectCustomerId_ != null ? (long)prospectCustomerId_ : 0;
                var organizationRoleUserId = orgRoleUserId;
                var callStatus             = customerId > 0 ? CallType.Existing_Customer.ToString().Replace("_", " ") : CallType.Register_New_Customer.ToString().Replace("_", " ");

                PhoneNumber glocomNumber = null;

                if (customer != null && customer.Address != null && HealthPlanId > 0)
                {
                    glocomNumber = _customerAccountGlocomNumberService.GetGlocomNumber((long)HealthPlanId, customer.Address.StateId, customerId);

                    if (glocomNumber == null)
                    {
                        var corporateAccount = _corporateAccountRepository.GetByTag(customer.Tag);
                        glocomNumber = corporateAccount != null ? corporateAccount.CheckoutPhoneNumber : null;
                    }
                }

                var incomingPhoneLine = glocomNumber != null ? glocomNumber.AreaCode + glocomNumber.Number : "";

                callId = _outboundCallQueueService.SetCallDetail(organizationRoleUserId, customerId, incomingPhoneLine, "", callStatus, null,
                                                                 HealthPlanId, "", callQueue.Id, callId, true, dialerType: dialerType, callQueueCategory: callQueue.Category, eventId: eventId, ProductTypeId: customer.ProductTypeId);

                if (customerId == 0 && prospectCustomerId > 0)
                {
                    UpdateContactedInfo(prospectCustomerId, callId, orgRoleUserId);
                }
                else if (customerId > 0)
                {
                    if (!(callQueue.Category == CallQueueCategory.Upsell || callQueue.Category == CallQueueCategory.PreAssessmentCallQueue))
                    {
                        if (prospectCustomer == null && customer != null)
                        {
                            prospectCustomer = _prospectCustomerFactory.CreateProspectCustomerFromCustomer(customer, false);
                            prospectCustomer = ((IUniqueItemRepository <ProspectCustomer>)_prospectCustomerRepository).Save(prospectCustomer);
                        }
                        if (prospectCustomer != null)
                        {
                            UpdateContactedInfo(prospectCustomer.Id, callId, orgRoleUserId);
                        }
                    }
                }
                var customerCallQueueCallAttempt = new PreAssessmentCustomerCallQueueCallAttempt
                {
                    DateCreated              = DateTime.Now,
                    IsCallSkipped            = false,
                    IsNotesReadAndUnderstood = true,
                    CreatedBy             = orgRoleUserId,
                    CustomerId            = customerId,
                    CallId                = callId,
                    NotInterestedReasonId = null
                };

                _preAssessmentCustomerCallQueueCallAttemptRepository.Save(customerCallQueueCallAttempt, false);

                if (glocomNumber != null)
                {
                    var customerAccountGlocomNumber = new CustomerAccountGlocomNumber
                    {
                        CallId       = callId,
                        CustomerId   = customerId,
                        GlocomNumber = glocomNumber.AreaCode + glocomNumber.Number,
                        CreatedDate  = DateTime.Now,
                        IsActive     = true
                    };
                    _customerAccountGlocomNumberService.SaveAccountCheckoutPhoneNumber(customerAccountGlocomNumber);
                }

                scope.Complete();
                return(callId);
            }
        }