Exemplo n.º 1
0
        public ActionResult RegisterAddress(RegisterAccountViewModel model)
        {
            if (ModelState.IsValid)
            {

                AddressModel addressModelFind = new AddressModel();

                // find new user's address
                AddressModel address = new UserService().FindMatchingAdressModel(
                    unitNumber: model.UnitNumber,
                    streetNumber: model.StreetNumber,
                    streetName: model.StreetName,
                    streetType: model.StreetType,
                    suburb: model.Suburb,
                    postcode: null
                    );

                //Address exists in database
                if (address != null)
                {
                    bool addressHasUsers = new UserService().CheckAddressHasUsersRegistered(
                        unitNumber: model.UnitNumber,
                        streetNumber: model.StreetNumber,
                        streetName: model.StreetName,
                        streetType: model.StreetType,
                        suburb: model.Suburb,
                        postcode: null
                        );

                    //This address still does not have registered users
                    if (!addressHasUsers)
                    {
                        var id = Guid.NewGuid();

                        Boolean isFBUser = false;
                        Boolean isApproved = false;
                        Int64 FBUserId = 0;

                        FacebookUserModel fbUser = null;
                        if (Session["FacebookUser"] != null)
                        {
                            fbUser = (FacebookUserModel)Session["FacebookUser"];
                        }

                        if (fbUser != null)
                        {
                            isFBUser = true;
                            isApproved = true;
                            FBUserId = Convert.ToInt64(fbUser.id);
                        }

                        // create the ASP membership user
                        MembershipCreateStatus status;

                        //Don't allow duplicate user
                        var tryToFindUser = Membership.GetUserNameByEmail(model.Email);
                        if (string.IsNullOrEmpty(tryToFindUser))
                        {
                            var membershipUser = Membership.CreateUser(username: model.Email, password: model.Password, email: model.Email,
                                passwordQuestion: null, passwordAnswer: null,
                                isApproved: isApproved, providerUserKey: id,
                                status: out status
                            );

                            if (status == MembershipCreateStatus.Success)
                            {
                                DateTime? saveDate = null;

                                if (model.DateOfBirthDay != null && model.DateOfBirthMonth != null && model.DateOfBirthYear != null)
                                {
                                    saveDate = new DateTime(year: (int)model.DateOfBirthYear.Value,
                                        month: (int)model.DateOfBirthMonth.Value,
                                        day: (int)model.DateOfBirthDay.Value);
                                }

                                // create the GM user
                                UserModel userModel = new UserModel();
                                userModel.Id = id;
                                userModel.FirstName = model.FirstName;
                                userModel.LastName = model.LastName;
                                userModel.AddressId = address.Id;
                                userModel.Instance_Id = address.Instance_Id;
                                userModel.SendEmailOffers = model.SendEmailOffers; //TODO check matching
                                userModel.SendEmailUpdates = model.SendEmailUpdates; //TODO check matching

                                userModel.IsFBAccount = isFBUser;
                                userModel.FBUserId = FBUserId;
                                userModel.IsAdditionalAccountHolder = false;
                                userModel.Sex = model.Sex;
                                userModel.DateOfBirth = saveDate;
                                userModel.PhoneNumber = model.PhoneNumber;

                                // Give them the New Member Bonus
                                int numBonusPoints = Convert.ToInt16(ConfigurationManager.AppSettings["BonusPoints.NewMember.Points"]);
                                string descBonusPoints = Convert.ToString(ConfigurationManager.AppSettings["BonusPoints.NewMember.Description"]);
                                int? transactionTypeId = Convert.ToInt16(ConfigurationManager.AppSettings["TransactionType.ShareHeart"]);

                                UserModel newUserModel = new UserService().CreateUser(userModel, numBonusPoints, descBonusPoints, transactionTypeId);

                                if (newUserModel != null)
                                {
                                    FormsAuthentication.SetAuthCookie(model.Email, createPersistentCookie: false);
                                }

                                if (!isApproved)
                                {
                                    SendVerifyEmail(membershipUser.Email, userModel.FirstName, userModel.Id, true);
                                    // THey are a normal user
                                    //TODO later - email sending before approve, not it is approved immediately
                                    //SendVerifyEmail(membershipUser, user, true);
                                    membershipUser.IsApproved = true;
                                    Membership.UpdateUser(membershipUser);

                                    //TODO later - checkout what is this
                                    //if (false)
                                    //{
                                    //    // Add them to the relevant MailChimp List
                                    //    MailChimpManager mc = new MailChimpManager(ConfigurationManager.AppSettings["MailChimp.APIKey"]);
                                    //    EmailParameter email = new EmailParameter()
                                    //    {
                                    //        Email = user.Email
                                    //    };

                                    //    EmailParameter results = mc.Subscribe(ConfigurationManager.AppSettings["MailChimp.ListId"], email);
                                    //}

                                }
                                else
                                {
                                    // They have just logged in with FB so Login as usual
                                    FormsAuthentication.SetAuthCookie(model.Email, createPersistentCookie: false);
                                }

                                // the next registration step
                                return RedirectToAction("AddHouseholdMembers", new { addressId = address.Id, inviter = model.Email, inviterId = id });

                            }
                            else
                            {
                                ModelState.AddModelError("", ErrorCodeToString(status));
                            }
                        }
                        else
                        {
                            // email has already been registered in the system
                            ModelState.AddModelError("", "User with the same email address has already been registered with GreenMoney.");
                        }
                    }
                    else
                    {
                        // address already has a user on it!
                        ModelState.AddModelError("", "Address has already been registered with GreenMoney.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Please contact us with your address details and we'll assist you in creating an account.");

                    TempData["ProblemWithAddress"] = true;
                }
            }

            var suburbs = new UserService().GetAllSuburbs();
            var streetTypes = new UserService().GetAllStreetTypes();

            model.Suburbs = new SelectList(suburbs);
            model.StreetTypes = new SelectList(streetTypes);

            //Display
            LayoutViewModel.HideTopWrapperMenu = true;

            // something failed, redisplay form
            return View(model);
        }
Exemplo n.º 2
0
        public ActionResult RegisterAddress(string instid, string firstname, string lastname, string email, string sex, 
            string birthDate, string p, string code, bool sendme = false)
        {
            DateTime bdate;
            var hasBirthDate = DateTime.TryParse(birthDate, out bdate);

            var passDecrypted = "notused";//p is empty for fb login
            if (!string.IsNullOrWhiteSpace(p))
            {
                passDecrypted = Encryption.DecryptString(p);
            }

            var viewModel = new RegisterAccountViewModel();
            viewModel.InstanceId = !string.IsNullOrWhiteSpace(instid) ? int.Parse(instid) : 0; //new added field
            viewModel.FirstName = firstname;
            viewModel.LastName = lastname;
            viewModel.Email = email;
            viewModel.SendEmailOffers = sendme;
            viewModel.SendEmailUpdates = sendme;
            viewModel.sendme = sendme;
            //model.Sex = sex;
            viewModel.Password = passDecrypted;
            viewModel.Postcode = code;

            var privateClientId = ConfigurationManager.AppSettings["PrivateClientId"] == null
                ? 5
                : int.Parse(ConfigurationManager.AppSettings["PrivateClientId"]);

            var suburbs = new UserService().GetAllSuburbs(privateClientId, toExclude: true);
            var streetTypes = new UserService().GetAllStreetTypes(privateClientId, toExclude: true);
            viewModel.Suburbs = new SelectList(suburbs);
            viewModel.StreetTypes = new SelectList(streetTypes);

            viewModel.DateOfBirth = hasBirthDate ? bdate : DateTime.MinValue;

            //Display
            LayoutViewModel.HideTopWrapperMenu = true;

            return View(viewModel);
        }