/// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> ExportCsv(NewsletterSubscriptionSearchModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageNewsletterSubscribers))
            {
                return(AccessDeniedView());
            }

            bool?isActive = null;

            if (model.ActiveId == 1)
            {
                isActive = true;
            }
            else if (model.ActiveId == 2)
            {
                isActive = false;
            }

            var startDateValue = model.StartDate == null ? null
                : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.StartDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync());
            var endDateValue = model.EndDate == null ? null
                : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.EndDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()).AddDays(1);

            var subscriptions = await _newsLetterSubscriptionService.GetAllNewsLetterSubscriptionsAsync(model.SearchEmail,
                                                                                                        startDateValue, endDateValue, model.StoreId, isActive, model.CustomerRoleId);

            var result = await _exportManager.ExportNewsletterSubscribersToTxtAsync(subscriptions);

            var fileName = $"newsletter_emails_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}_{CommonHelper.GenerateRandomDigitCode(4)}.csv";

            return(File(Encoding.UTF8.GetBytes(result), MimeTypes.TextCsv, fileName));
        }
Ejemplo n.º 2
0
        public virtual async Task <IActionResult> SendMassEmail(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);

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

                //subscribers of certain store?
                var storeId       = (await _storeService.GetStoreByIdAsync(campaign.StoreId))?.Id ?? 0;
                var subscriptions = await _newsLetterSubscriptionService.GetAllNewsLetterSubscriptionsAsync(storeId : storeId,
                                                                                                            customerRoleId : model.CustomerRoleId,
                                                                                                            isActive : true);

                var totalEmailsSent = await _campaignService.SendCampaignAsync(campaign, emailAccount, subscriptions);

                _notificationService.SuccessNotification(string.Format(await _localizationService.GetResourceAsync("Admin.Promotions.Campaigns.MassEmailSentToCustomers"), totalEmailsSent));

                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));
        }
        /// <summary>
        /// Prepare paged newsletter subscription list model
        /// </summary>
        /// <param name="searchModel">Newsletter subscription search model</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the newsletter subscription list model
        /// </returns>
        public virtual async Task <NewsletterSubscriptionListModel> PrepareNewsletterSubscriptionListModelAsync(NewsletterSubscriptionSearchModel searchModel)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get parameters to filter newsletter subscriptions
            var isActivatedOnly = searchModel.ActiveId == 0 ? null : searchModel.ActiveId == 1 ? true : (bool?)false;
            var startDateValue  = !searchModel.StartDate.HasValue ? null
                : (DateTime?)_dateTimeHelper.ConvertToUtcTime(searchModel.StartDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync());
            var endDateValue = !searchModel.EndDate.HasValue ? null
                : (DateTime?)_dateTimeHelper.ConvertToUtcTime(searchModel.EndDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()).AddDays(1);

            //get newsletter subscriptions
            var newsletterSubscriptions = await _newsLetterSubscriptionService.GetAllNewsLetterSubscriptionsAsync(email : searchModel.SearchEmail,
                                                                                                                  customerRoleId : searchModel.CustomerRoleId,
                                                                                                                  storeId : searchModel.StoreId,
                                                                                                                  isActive : isActivatedOnly,
                                                                                                                  createdFromUtc : startDateValue,
                                                                                                                  createdToUtc : endDateValue,
                                                                                                                  pageIndex : searchModel.Page - 1, pageSize : searchModel.PageSize);

            //prepare list model
            var model = await new NewsletterSubscriptionListModel().PrepareToGridAsync(searchModel, newsletterSubscriptions, () =>
            {
                return(newsletterSubscriptions.SelectAwait(async subscription =>
                {
                    //fill in model values from the entity
                    var subscriptionModel = subscription.ToModel <NewsletterSubscriptionModel>();

                    //convert dates to the user time
                    subscriptionModel.CreatedOn = (await _dateTimeHelper.ConvertToUserTimeAsync(subscription.CreatedOnUtc, DateTimeKind.Utc)).ToString();

                    //fill in additional values (not existing in the entity)
                    subscriptionModel.StoreName = (await _storeService.GetStoreByIdAsync(subscription.StoreId))?.Name ?? "Deleted";

                    return subscriptionModel;
                }));
            });

            return(model);
        }