public virtual async Task <IActionResult> SendTestEmail(CampaignModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageCampaigns))
            {
                return(AccessDeniedView());
            }

            //try to get a campaign with the specified id
            var campaign = await _campaignService.GetCampaignByIdAsync(model.Id);

            if (campaign == null)
            {
                return(RedirectToAction("List"));
            }

            //prepare model
            model = await _campaignModelFactory.PrepareCampaignModelAsync(model, campaign);

            //ensure that the entered email is valid
            if (!CommonHelper.IsValidEmail(model.TestEmail))
            {
                _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Common.WrongEmail"));
                return(View(model));
            }

            try
            {
                var emailAccount = await GetEmailAccountAsync(model.EmailAccountId);

                var subscription = await _newsLetterSubscriptionService
                                   .GetNewsLetterSubscriptionByEmailAndStoreIdAsync(model.TestEmail, (await _storeContext.GetCurrentStoreAsync()).Id);

                if (subscription != null)
                {
                    //there's a subscription. let's use it
                    await _campaignService.SendCampaignAsync(campaign, emailAccount, new List <NewsLetterSubscription> {
                        subscription
                    });
                }
                else
                {
                    //no subscription found
                    await _campaignService.SendCampaignAsync(campaign, emailAccount, model.TestEmail);
                }

                _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Promotions.Campaigns.TestEmailSentToCustomers"));

                return(View(model));
            }
            catch (Exception exc)
            {
                await _notificationService.ErrorNotificationAsync(exc);
            }

            //prepare model
            model = await _campaignModelFactory.PrepareCampaignModelAsync(model, campaign, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DeleteCustomer(int id)
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var customer = await _customerApiService.GetCustomerEntityByIdAsync(id);

            if (customer == null)
            {
                return(Error(HttpStatusCode.NotFound, "customer", "not found"));
            }

            await CustomerService.DeleteCustomerAsync(customer);

            //remove newsletter subscription (if exists)
            foreach (var store in await StoreService.GetAllStoresAsync())
            {
                var subscription = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(customer.Email, store.Id);

                if (subscription != null)
                {
                    await _newsLetterSubscriptionService.DeleteNewsLetterSubscriptionAsync(subscription);
                }
            }

            //activity log
            await CustomerActivityService.InsertActivityAsync("DeleteCustomer", await LocalizationService.GetResourceAsync("ActivityLog.DeleteCustomer"), customer);

            return(new RawJsonActionResult("{}"));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> DeactivateNewsLetterSubscription(string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return(Error(HttpStatusCode.BadRequest, "The email parameter could not be empty."));
            }

            var existingSubscription = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(email, _storeContext.GetCurrentStore().Id);

            if (existingSubscription == null)
            {
                return(Error(HttpStatusCode.BadRequest, "There is no news letter subscription with the specified email."));
            }

            existingSubscription.Active = false;

            await _newsLetterSubscriptionService.UpdateNewsLetterSubscriptionAsync(existingSubscription);

            return(Ok());
        }
Ejemplo n.º 4
0
        public virtual async Task <IActionResult> SubscribeNewsletter(string email, bool subscribe)
        {
            string result;
            var    success = false;

            if (!CommonHelper.IsValidEmail(email))
            {
                result = await _localizationService.GetResourceAsync("Newsletter.Email.Wrong");
            }
            else
            {
                email = email.Trim();

                var subscription = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(email, (await _storeContext.GetCurrentStoreAsync()).Id);

                if (subscription != null)
                {
                    if (subscribe)
                    {
                        if (!subscription.Active)
                        {
                            await _workflowMessageService.SendNewsLetterSubscriptionActivationMessageAsync(
                                subscription,
                                (await _workContext.GetWorkingLanguageAsync()).Id
                                );
                        }
                        result = await _localizationService.GetResourceAsync("Newsletter.SubscribeEmailSent");
                    }
                    else
                    {
                        if (subscription.Active)
                        {
                            await _workflowMessageService.SendNewsLetterSubscriptionDeactivationMessageAsync(
                                subscription,
                                (await _workContext.GetWorkingLanguageAsync()).Id
                                );
                        }
                        result = await _localizationService.GetResourceAsync("Newsletter.UnsubscribeEmailSent");
                    }
                }
                else if (subscribe)
                {
                    try
                    {
                        var freshAddressResponse = await _freshAddressService.ValidateEmailAsync(email);

                        if (!freshAddressResponse.IsValid)
                        {
                            return(Json(new
                            {
                                Success = false,
                                Result = freshAddressResponse.ErrorResponse,
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        await _logger.ErrorAsync($"An error occured while trying to validate a Newsletter signup through FreshAddress: {ex}");
                    }

                    subscription = new NewsLetterSubscription
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email        = email,
                        Active       = false,
                        StoreId      = (await _storeContext.GetCurrentStoreAsync()).Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };
                    await _newsLetterSubscriptionService.InsertNewsLetterSubscriptionAsync(subscription);

                    await _workflowMessageService.SendNewsLetterSubscriptionActivationMessageAsync(subscription, (_workContext.GetWorkingLanguageAsync()).Id);

                    result = await _localizationService.GetResourceAsync("Newsletter.SubscribeEmailSent");
                }
                else
                {
                    result = await _localizationService.GetResourceAsync("Newsletter.UnsubscribeEmailSent");
                }
                success = true;
            }

            return(Json(new
            {
                Success = success,
                Result = result,
            }));
        }
        /// <summary>
        /// Sets a user email
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="newEmail">New email</param>
        /// <param name="requireValidation">Require validation of new email address</param>
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task SetEmailAsync(Customer customer, string newEmail, bool requireValidation)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            if (newEmail == null)
            {
                throw new NopException("Email cannot be null");
            }

            newEmail = newEmail.Trim();
            var oldEmail = customer.Email;

            if (!CommonHelper.IsValidEmail(newEmail))
            {
                throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.NewEmailIsNotValid"));
            }

            if (newEmail.Length > 100)
            {
                throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.EmailTooLong"));
            }

            var customer2 = await _customerService.GetCustomerByEmailAsync(newEmail);

            if (customer2 != null && customer.Id != customer2.Id)
            {
                throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.EmailAlreadyExists"));
            }

            if (requireValidation)
            {
                //re-validate email
                customer.EmailToRevalidate = newEmail;
                await _customerService.UpdateCustomerAsync(customer);

                //email re-validation message
                await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.EmailRevalidationTokenAttribute, Guid.NewGuid().ToString());

                await _workflowMessageService.SendCustomerEmailRevalidationMessageAsync(customer, (await _workContext.GetWorkingLanguageAsync()).Id);
            }
            else
            {
                customer.Email = newEmail;
                await _customerService.UpdateCustomerAsync(customer);

                if (string.IsNullOrEmpty(oldEmail) || oldEmail.Equals(newEmail, StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }

                //update newsletter subscription (if required)
                foreach (var store in await _storeService.GetAllStoresAsync())
                {
                    var subscriptionOld = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(oldEmail, store.Id);

                    if (subscriptionOld == null)
                    {
                        continue;
                    }

                    subscriptionOld.Email = newEmail;
                    await _newsLetterSubscriptionService.UpdateNewsLetterSubscriptionAsync(subscriptionOld);
                }
            }
        }
Ejemplo n.º 6
0
        public virtual async Task <IActionResult> SubscribeNewsletter(string email, bool subscribe)
        {
            string result;
            var    success = false;

            if (!CommonHelper.IsValidEmail(email))
            {
                result = await _localizationService.GetResourceAsync("Newsletter.Email.Wrong");
            }
            else
            {
                email = email.Trim();
                var store = await _storeContext.GetCurrentStoreAsync();

                var subscription = await _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreIdAsync(email, store.Id);

                var currentLanguage = await _workContext.GetWorkingLanguageAsync();

                if (subscription != null)
                {
                    if (subscribe)
                    {
                        if (!subscription.Active)
                        {
                            await _workflowMessageService.SendNewsLetterSubscriptionActivationMessageAsync(subscription, currentLanguage.Id);
                        }
                        result = await _localizationService.GetResourceAsync("Newsletter.SubscribeEmailSent");
                    }
                    else
                    {
                        if (subscription.Active)
                        {
                            await _workflowMessageService.SendNewsLetterSubscriptionDeactivationMessageAsync(subscription, currentLanguage.Id);
                        }
                        result = await _localizationService.GetResourceAsync("Newsletter.UnsubscribeEmailSent");
                    }
                }
                else if (subscribe)
                {
                    subscription = new NewsLetterSubscription
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email        = email,
                        Active       = false,
                        StoreId      = store.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };
                    await _newsLetterSubscriptionService.InsertNewsLetterSubscriptionAsync(subscription);

                    await _workflowMessageService.SendNewsLetterSubscriptionActivationMessageAsync(subscription, currentLanguage.Id);

                    result = await _localizationService.GetResourceAsync("Newsletter.SubscribeEmailSent");
                }
                else
                {
                    result = await _localizationService.GetResourceAsync("Newsletter.UnsubscribeEmailSent");
                }
                success = true;
            }

            return(Json(new
            {
                Success = success,
                Result = result,
            }));
        }