private async Task LoadSelections(ManageRegistrationViewModel model)
        {
            var options = await Factory.GetByIdAsync <IRegistrationOptionsDto>(null);

            model.StateSelection        = options.StateSelection;
            model.RequestTypeSelection  = options.RequestTypeSelection;
            model.BillingCycleSelection = options.BillingCycleSelection;
            model.CurrencySelection     = options.CurrencySelection;
            model.EstimatePlanSelection = options.EstimatePlanSelection;
            model.GroupSelection        = options.GroupSelection;
            model.PricingPlanSelection  = options.PricingPlanSelection;
            model.AllVehicleMakes       = options.AllVehicleMakes;
            model.InsuranceCompanies    = options.InsuranceCompanies;
        }
        public async Task <ActionResult> ManageRegistration(Guid?id = null)
        {
            var model = new ManageRegistrationViewModel();

            if (id != null)
            {
                var registrationDTO = await Factory.GetByIdAsync <IRegistrationDto>(id.ToString());

                model = Mapper.Map <ManageRegistrationViewModel>(registrationDTO);
            }
            await LoadSelections(model);

            return(View("_ManageRegistration", model));
        }
        public async Task <ActionResult> ManageRegistration(ManageRegistrationViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Determine Notification.
                var sendLink = model.RegistrationId == Guid.Empty || model.RegistrationStatus == RegistrationStatus.Resent;

                // Update Status.
                model.RegistrationStatus = model.RegistrationStatus == RegistrationStatus.Resent ? RegistrationStatus.Sent : model.RegistrationStatus;

                // Save Update.
                var registration = await Factory.SaveAsync <IRegistrationDto>(model);

                // Check Notifications.
                if (!sendLink && registration.RegistrationStatus != RegistrationStatus.Completed)
                {
                    return(new EmptyResult());
                }

                // Send Notifications.
                using (var queue = new MessageQueue())
                {
                    if (sendLink)
                    {
                        await queue.AddNotificationQueueMessageAsync(template : NotificationTemplate.RegistrationEmail, id : registration.RegistrationShop.RegistrationShopId, userGuid : Guid.Parse(User.Identity.GetUserId()));
                    }
                    else
                    {
                        await queue.AddNotificationQueueMessageAsync(template : NotificationTemplate.RegistrationWelcomeEmail, id : registration.RegistrationShop.RegistrationShopId, userGuid : Guid.Parse(User.Identity.GetUserId()));
                    }
                }

                // Check Callbacks.
                if (registration.RegistrationStatus != RegistrationStatus.Completed || string.IsNullOrWhiteSpace(registration.CallbackUrl))
                {
                    return(new EmptyResult());
                }

                try
                {
                    // Perform Callbacks.
                    using (var conn = new SqlConnection(MvcApplication.ConnectionString))
                    {
                        // Load Registration Status.
                        var status = await conn.QueryFirstAsync <MitchellRegistrationStatusDto>("Service.usp_GetMitchellRegistration", new { registration.RegistrationId }, commandType : CommandType.StoredProcedure);

                        // Check Status.
                        if (status != null)
                        {
                            // Send Registration Status.
                            var client   = new HttpClient();
                            var response = await client.PostAsJsonAsync(registration.CallbackUrl, status);

                            // Check Response.
                            if (response.IsSuccessStatusCode)
                            {
                                await conn.ExecuteAsync("Service.usp_UpdateMitchellRegistration", new { status.MitchellAccountId, UserEmail = registration.Email }, commandType : CommandType.StoredProcedure);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.LogException(e);
                }

                return(new EmptyResult());
            }

            await LoadSelections(model);

            return(PartialView("_ManageRegistration", model));
        }