/// <summary>
 /// Create or update the synchronization record with passed parameters
 /// </summary>
 /// <param name="entityType">Entity type</param>
 /// <param name="id">Entity identifier</param>
 /// <param name="operationType">Operation type</param>
 /// <param name="email">Subscription email</param>
 /// <param name="productId">Product identifier</param>
 private void AddRecord(EntityType entityType, int?id, OperationType operationType, string email = null, int?productId = null)
 {
     _synchronizationRecordService.CreateOrUpdateRecord(entityType, id ?? 0, operationType, email, productId ?? 0);
 }
Exemplo n.º 2
0
        public async Task <IActionResult> Configure(ConfigurationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(await Configure());
            }

            //load settings for a chosen store scope
            var storeId           = _storeContext.ActiveStoreScopeConfiguration;
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            //update stores if the list was changed
            if (!string.IsNullOrEmpty(model.ListId) && !model.ListId.Equals(Guid.Empty.ToString()) && !model.ListId.Equals(mailChimpSettings.ListId))
            {
                (storeId > 0 ? new[] { storeId } : _storeService.GetAllStores().Select(store => store.Id)).ToList()
                .ForEach(id => _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, id, OperationType.Update));
            }

            //prepare webhook
            if (!string.IsNullOrEmpty(mailChimpSettings.ApiKey))
            {
                var listId          = !string.IsNullOrEmpty(model.ListId) && !model.ListId.Equals(Guid.Empty.ToString()) ? model.ListId : string.Empty;
                var webhookPrepared = await _mailChimpManager.PrepareWebhook(listId);

                //display warning if webhook is not prepared
                if (!webhookPrepared && !string.IsNullOrEmpty(listId))
                {
                    _notificationService.WarningNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.Webhook.Warning"));
                }
            }

            //save settings
            mailChimpSettings.ApiKey            = model.ApiKey;
            mailChimpSettings.PassEcommerceData = model.PassEcommerceData;
            mailChimpSettings.ListId            = model.ListId;
            _settingService.SaveSetting(mailChimpSettings, x => x.ApiKey, clearCache: false);
            _settingService.SaveSetting(mailChimpSettings, x => x.PassEcommerceData, clearCache: false);
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, x => x.ListId, model.ListId_OverrideForStore, storeId, false);
            _settingService.ClearCache();

            //create or update synchronization task
            var task = _scheduleTaskService.GetTaskByType(MailChimpDefaults.SynchronizationTask);

            if (task == null)
            {
                task = new ScheduleTask
                {
                    Type    = MailChimpDefaults.SynchronizationTask,
                    Name    = MailChimpDefaults.SynchronizationTaskName,
                    Seconds = MailChimpDefaults.DefaultSynchronizationPeriod * 60 * 60
                };
                _scheduleTaskService.InsertTask(task);
            }

            var synchronizationPeriodInSeconds = model.SynchronizationPeriod * 60 * 60;
            var synchronizationEnabled         = model.AutoSynchronization;

            if (task.Enabled != synchronizationEnabled || task.Seconds != synchronizationPeriodInSeconds)
            {
                //task parameters was changed
                task.Enabled = synchronizationEnabled;
                task.Seconds = synchronizationPeriodInSeconds;
                _scheduleTaskService.UpdateTask(task);
                _notificationService.WarningNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.Fields.AutoSynchronization.Restart"));
            }

            _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));

            return(await Configure());
        }
Exemplo n.º 3
0
 /// <summary>
 /// Handles the event.
 /// </summary>
 /// <param name="insertedStore">Inserted store</param>
 public void HandleEvent(EntityInserted <Store> insertedStore)
 {
     if (PluginIsActive)
     {
         _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, insertedStore.Entity.Id, ActionType.Create);
     }
 }
Exemplo n.º 4
0
        public ActionResult Configure(MailChimpModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Configure());
            }

            //load settings for a chosen store scope
            var storeId           = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            //update stores if the list was changed
            if (model.ListId != null && !model.ListId.Equals("0") && !model.ListId.Equals(mailChimpSettings.ListId))
            {
                if (storeId > 0)
                {
                    _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, storeId, ActionType.Update);
                }
                else
                {
                    foreach (var store in _storeService.GetAllStores())
                    {
                        _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, store.Id, ActionType.Update);
                    }
                }
            }

            //webhook
            if (!string.IsNullOrEmpty(model.ListId))
            {
                //delete current webhook
                if (!model.ListId.Equals(mailChimpSettings.ListId))
                {
                    _mailChimpManager.DeleteWebhook(mailChimpSettings.ListId, mailChimpSettings.WebhookId);
                    mailChimpSettings.WebhookId = string.Empty;
                }

                //and create new one
                if (!model.ListId.Equals("0"))
                {
                    //we use Task.Run() because child actions cannot be run asynchronously
                    mailChimpSettings.WebhookId = tasks.Task.Run(() => _mailChimpManager.CreateWebhook(model.ListId, mailChimpSettings.WebhookId)).Result;
                    if (string.IsNullOrEmpty(mailChimpSettings.WebhookId))
                    {
                        ErrorNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.WebhookError"));
                    }
                }
            }

            //settings
            mailChimpSettings.ApiKey          = model.ApiKey;
            mailChimpSettings.UseEcommerceApi = model.UseEcommerceApi;
            mailChimpSettings.ListId          = model.ListId;
            _settingService.SaveSetting(mailChimpSettings, x => x.ApiKey, 0, false);
            _settingService.SaveSetting(mailChimpSettings, x => x.UseEcommerceApi, 0, false);
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, x => x.ListId, model.ListId_OverrideForStore, storeId, false);
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, x => x.WebhookId, true, storeId, false);

            //now clear settings cache
            _settingService.ClearCache();

            //create or update synchronization task
            var task = FindScheduledTask();

            if (task != null)
            {
                //task parameters was changed
                if (task.Enabled != model.AutoSync || task.Seconds != model.AutoSyncEachMinutes * 60)
                {
                    task.Enabled = model.AutoSync;
                    task.Seconds = model.AutoSyncEachMinutes * 60;
                    _scheduleTaskService.UpdateTask(task);
                    SuccessNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.AutoSyncRestart"));
                }
                else
                {
                    SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));
                }
            }
            else
            {
                _scheduleTaskService.InsertTask(new ScheduleTask
                {
                    Name    = "MailChimp synchronization",
                    Seconds = model.AutoSyncEachMinutes * 60,
                    Enabled = model.AutoSync,
                    Type    = "Nop.Plugin.Misc.MailChimp.Services.MailChimpSynchronizationTask, Nop.Plugin.Misc.MailChimp",
                });
                SuccessNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.AutoSyncRestart"));
            }

            return(Configure());
        }