public void SaveUnsubscribedSmsStatus(CustomerUnsubscribedSmsNotification smsReceived)
        {
            using (var adapter = PersistenceLayer.GetDataAccessAdapter())
            {
                var entity = Mapper.Map <CustomerUnsubscribedSmsNotification, CustomerUnsubscribedSmsNotificationEntity>(smsReceived);

                if (!adapter.SaveEntity(entity, true))
                {
                    throw new PersistenceFailureException();
                }
            }
        }
        public void SmsReceivedNotification(TwillioMessageResponse message)
        {
            if (message != null)
            {
                var phoneMobile = _phoneNumberFactory.CreatePhoneNumber(message.From, PhoneNumberType.Mobile);
                if (phoneMobile == null)
                {
                    return;
                }

                var mobilePhonNumber = phoneMobile.AreaCode + phoneMobile.Number;
                var customers        = _customerRepository.GetCustomersByPhoneNumber(mobilePhonNumber, PhoneNumberType.Mobile);
                var smsReceivedTime  = DateTime.Now;

                var smsReceived = new SmsReceived
                {
                    DateCreated = smsReceivedTime,
                    Message     = message.Body,
                    PhoneNumber = mobilePhonNumber
                };

                smsReceived = _smsReceivedRepository.SaveSmsReceived(smsReceived);

                if (customers.IsNullOrEmpty())
                {
                    _logger.Info("No customer found with matching Phone Number");
                    return;
                }

                if (!string.IsNullOrEmpty(message.Body) && SmsOptOutKeyWords.OptOutKey.Contains(message.Body.ToLower()))
                {
                    var unsubscriptModel = new CustomerUnsubscribedSmsNotification
                    {
                        DateCreated   = smsReceivedTime,
                        SmsReceivedId = smsReceived.Id,
                        StatusId      = (long)SmsNotificationSubscriptionStatus.Unsubscribe
                    };

                    foreach (var customer in customers)
                    {
                        unsubscriptModel.CustomerId = customer.CustomerId;

                        _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel);

                        customer.IsSubscribed = false;
                        _customerService.SaveCustomerOnly(customer, customer.CustomerId);
                    }
                }
                else if (!string.IsNullOrEmpty(message.Body) && SmsOptInKeyWords.OptInKey.Contains(message.Body.ToLower()))
                {
                    var unsubscriptModel = new CustomerUnsubscribedSmsNotification
                    {
                        DateCreated   = smsReceivedTime,
                        SmsReceivedId = smsReceived.Id,
                        StatusId      = (long)SmsNotificationSubscriptionStatus.Subscribe
                    };

                    foreach (var customer in customers)
                    {
                        unsubscriptModel.CustomerId = customer.CustomerId;
                        _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel);

                        customer.IsSubscribed = true;
                        _customerService.SaveCustomerOnly(customer, customer.CustomerId);
                    }
                }
                else
                {
                    if (!customers.IsNullOrEmpty())
                    {
                        Customer customer = null;
                        if (customers.Count() > 1)
                        {
                            var lastNotificationSentTo =
                                _notificationRepository.GetLatestNotificationByPhone(mobilePhonNumber);
                            customer = customers.FirstOrDefault(s => s.Id == lastNotificationSentTo.UserId) ?? customers.First();
                        }
                        else
                        {
                            customer = customers.First();
                        }

                        if (customer != null)
                        {
                            var screeningReminderSmsNotification = _phoneNotificationModelsFactory.GetDummyWrongSmsResponseNotificationViewModel();

                            var notification = _notifier.NotifyViaSms(NotificationTypeAlias.WrongSmsReponse, EmailTemplateAlias.WrongSmsReponse, screeningReminderSmsNotification, customer.Id, customer.CustomerId, "Wrong Sms Response");
                        }
                    }
                }
            }
        }