Beispiel #1
0
        public ActionResult Synchronization(MailChimpModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Configure());
            }

            if (string.IsNullOrEmpty(model.ListId) || model.ListId.Equals("0"))
            {
                ErrorNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.SynchronizationError"));
                return(Configure());
            }

            //we use Task.Run() because child actions cannot be run asynchronously
            var batchId = tasks.Task.Run(() => _mailChimpManager.Synchronize()).Result;

            if (!string.IsNullOrEmpty(batchId))
            {
                Session.Add("synchronization", true);
                Session.Add("batchId", batchId);
            }
            else
            {
                ErrorNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.SynchronizationError"));
            }

            return(Configure());
        }
Beispiel #2
0
        public async Task <IActionResult> Subscribe([FromForm] MailChimpModel mailChimp)
        {
            if (string.IsNullOrEmpty(mailChimp.Email))
            {
                return(BadRequest(new
                {
                    Message = "Email is required"
                }));
            }

            try
            {
                await _mailchimpService.Subscribe(mailChimp);

                return(Ok(new
                {
                    Message = "Thanks for your Subscribe!"
                }));
            }
            catch (Exception e)
            {
                return(BadRequest(new
                {
                    Message = e.Message
                }));
            }
        }
        public async Task Subscribe(MailChimpModel mailChimp)
        {
            var listId = _config["MailChimp:ListId"];
            // Use the Status property if updating an existing member
            var member = new Member {
                EmailAddress = $"{mailChimp.Email}", StatusIfNew = Status.Subscribed
            };

            member.MergeFields.Add("LNAME", mailChimp.Name);

            await this.mailChimpManager.Members.AddOrUpdateAsync(listId, member);
        }
Beispiel #4
0
        public ActionResult Configure()
        {
            //load settings for a chosen store scope
            var storeId           = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            var model = new MailChimpModel
            {
                ApiKey          = mailChimpSettings.ApiKey,
                UseEcommerceApi = mailChimpSettings.UseEcommerceApi,
                ListId          = mailChimpSettings.ListId,
                ActiveStoreScopeConfiguration = storeId
            };

            if (storeId > 0)
            {
                model.ListId_OverrideForStore = _settingService.SettingExists(mailChimpSettings, x => x.ListId, storeId);
            }

            //synchronization task
            var task = FindScheduledTask();

            if (task != null)
            {
                model.AutoSyncEachMinutes = task.Seconds / 60;
                model.AutoSync            = task.Enabled;
            }

            //get account info
            //we use Task.Run() because child actions cannot be run asynchronously
            model.AccountInfo = tasks.Task.Run(() => _mailChimpManager.GetAccountInfo()).Result;

            //prepare available lists
            model.AvailableLists = tasks.Task.Run(() => _mailChimpManager.GetAvailableLists()).Result;

            return(View("~/Plugins/Misc.MailChimp/Views/MailChimp/Configure.cshtml", model));
        }
Beispiel #5
0
        public IActionResult FacebookLogin([FromForm] SocialLoginRequest request)
        {
            var user = _context.Users.Where(x => x.Email == request.Email.Trim()).FirstOrDefault();

            //Email chưa được sử dụng trong hệ thống, đăng ký tài khoản mới
            if (user == null)
            {
                var newUser = new AppUser()
                {
                    NumberOfFiends = 0,
                    NumberOfImages = 0,
                    NumberOfLikes  = 0,
                    Email          = request.Email,
                    Role           = ERole.User,
                    FullName       = request.FullName,
                    UserName       = request.Email,
                    AvatarPath     = request.Avatar,
                    IsInfoUpdated  = false,
                    PassWord       = Guid.NewGuid().ToString()
                };

                switch (request.Provider.ToLower())
                {
                case "facebook":
                    newUser.TypeAccount = ETypeAccount.Facebook;
                    break;

                case "google":
                    newUser.TypeAccount = ETypeAccount.Google;
                    break;

                default:
                    return(BadRequest(new { Message = "Provider is not correct" }));
                }

                var mailchimp = new MailChimpModel()
                {
                    Email = request.Email,
                    Name  = request.FullName
                };

                _mailchimpService.Subscribe(mailchimp);

                try
                {
                    _context.Users.Add(newUser);
                    _context.SaveChanges();
                    var response = new UserResponse(newUser, _storageService);
                    response.Token = this.GenerateJSONWebToken(newUser);
                    return(Ok(response));
                }
                catch (Exception e)
                {
                    return(BadRequest(new
                    {
                        Message = e.Message
                    }));
                }
            }
            else // Email đã có trong hệ thống, tiến hành đăng nhập
            {
                if (user.Status == EUserStatus.Inactive)
                {
                    return(BadRequest(new
                    {
                        Message = "Tài khoản của bạn đã bị khóa bởi quản trị viên!"
                    }));
                }

                var userResponse = new UserResponse(user, _storageService);

                userResponse.Token = this.GenerateJSONWebToken(user);

                //_sessionService.SetSessionUser(user);

                return(Ok(userResponse));
            }
        }
Beispiel #6
0
        public async Task <IActionResult> SignUp([FromForm] SignUpSystemRequest signUpRequest)
        {
            _loginInfo = new LoginInfo();
            LoginInfo loginInfo = new LoginInfo();
            var       request   = new LoginInfo()
            {
                Email    = signUpRequest.Email,
                FullName = signUpRequest.FullName,
                Password = signUpRequest.Password,
                UserName = signUpRequest.Email
            };

            loginInfo = await this.CheckRecordExistence(request);

            _loginInfo = loginInfo;
            if (loginInfo == null)
            {
                var user = new AppUser()
                {
                    CreatedAt  = DateTime.Now,
                    Email      = request.Email,
                    UserName   = request.UserName,
                    FullName   = request.FullName,
                    PassWord   = request.Password,
                    Role       = ERole.User,
                    Status     = EUserStatus.IsVerifying,
                    AvatarPath = "image.png"
                };
                var mailchimp = new MailChimpModel()
                {
                    Email = request.Email,
                    Name  = request.FullName
                };


                try
                {
                    await _mailchimpService.Subscribe(mailchimp);

                    _context.Users.Add(user);
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    return(BadRequest(new
                    {
                        Message = e.InnerException
                    }));
                }
                _loginInfo = new LoginInfo()
                {
                    Email           = user.Email,
                    FullName        = user.FullName,
                    IsMailConfirmed = false,
                    Message         = MessageMail.Success,
                    UserId          = user.Id,
                    UserName        = user.UserName
                };
            }

            if (_loginInfo.Message == MessageMail.UserAlreadyCreated)
            {
                return(BadRequest(new
                {
                    Message = _loginInfo.Message
                }));
            }

            if (_loginInfo.Message == MessageMail.VerifyMail)
            {
                MailClass mailClass = this.GetMailObject(_loginInfo);
                await _mailService.SendMail(mailClass);

                return(BadRequest(new
                {
                    Message = MessageMail.VerifyMail
                }));
            }

            var message = "";

            if (_loginInfo.Message == MessageMail.Success)
            {
                MailClass mailClass = this.GetMailObject(_loginInfo);
                message = await _mailService.SendMail(mailClass);
            }

            if (message != MessageMail.MailSent)
            {
                return(BadRequest(new
                {
                    Message = message
                }));
            }
            else
            {
                return(Ok(new
                {
                    Message = MessageMail.UserCreatedVerifyMail
                }));
            }
        }
Beispiel #7
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());
        }