Exemple #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            model.FormBehavior = new FormBehavior();

            ViewData["ReturnUrl"] = returnUrl;
            bool fromBinary = false;

            var position = string.IsNullOrWhiteSpace(model.Position) ? Enums.BPosition.Left : (Enums.BPosition)Enum.Parse(typeof(Enums.BPosition), model.Position);

            if (!string.IsNullOrWhiteSpace(model.ParentId))
            {
                fromBinary = true;
                var parent = _context.ApplicationUser.Find(model.ParentId);
                if (parent == null)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Action");
                }
                else
                {
                    var hasExistingUser = _context.ApplicationUser.Where(x => x.ParentId == model.ParentId && x.ChildPosition == position && x.CreatedBy == _userHandler.User.Id).FirstOrDefault();
                    if (hasExistingUser != null)
                    {
                        ModelState.AddModelError(string.Empty, "Invalid Action");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var isSecurityCode = _register.CheckSecurityCode(model.Pin, model.SecurityCode);
                if (!isSecurityCode)
                {
                    model.FormBehavior = new FormBehavior();
                    model.FormBehavior.Notification = new Notification
                    {
                        IsError = true,
                        Message = "Invalid combination of PIN and Security Code",
                        Title   = "Referral Code"
                    };
                    model.Pin          = string.Empty;
                    model.SecurityCode = string.Empty;
                    return(PartialView("_Register", model));
                }

                var user = new ApplicationUser
                {
                    UserName         = model.UserName,
                    Email            = model.Email,
                    FirstName        = model.FirstName,
                    LastName         = model.LastName,
                    MiddleName       = model.MiddleName,
                    PhoneNumber      = model.PhoneNumber,
                    ParentId         = model.ParentId,
                    ChildPosition    = position,
                    RegistrationType = fromBinary ? Enums.RegistrationType.Geneology : Enums.RegistrationType.Bayanihan
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    _register.UpdateReferralCode(model.SecurityCode, user.Id);

                    //_register.RegisterBayanihan(new Bayanihan { UserId = user.Id });
                    CreateIncomeRecord(user.Id);
                    //_incomeCompute.BayanihanIncomeCompute(user.Id);
                    //_incomeCompute.GeneologyIncomeCompute(user.Id);
                    //_incomeCompute.DirectReferralCompute(_userManager.GetUserId(User));
                    _incomeCompute.IncentiveCompute(_userManager.GetUserId(User), user.Id);
                    _incomeCompute.ComputeNetIncome(_userManager.GetUserId(User));

                    var roleName = string.Empty;
                    if (model.RegistrationType == "bayanihan")
                    {
                        roleName = "User Bayanihan";
                    }
                    else
                    {
                        roleName = "User";
                        //_incomeCompute.UnilevelCompute(user.Id);
                    }
                    var role = _context.ApplicationRole.Where(r => r.Name == roleName).FirstOrDefault();
                    if (role != null)
                    {
                        await _userManager.AddToRoleAsync(user, role.Name);
                    }

                    _logger.LogInformation("User created a new account with password.");
                    model = new RegisterViewModel();
                    model.FormBehavior = new FormBehavior();
                    model.FormBehavior.Notification = new Notification
                    {
                        IsError = false,
                        Message = "Account successfuly created.",
                        Title   = "Registration"
                    };

                    if (fromBinary)
                    {
                        model.FormBehavior.PageRedirect = new PageRedirect {
                            Reload = true, URL = Url.Action("Index", "BinaryTree")
                        };
                    }

                    ModelState.Clear();
                }
                else
                {
                    AddErrors(result);
                }
            }
            return(PartialView("_Register", model));
        }