Example #1
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterVM model)
        {
            if (model == null)
            {
                throw new NullReferenceException("Register model is null");
            }
            if (ModelState.IsValid)
            {
                var role = _roleManager.FindByIdAsync("4").Result;
                var user = new Account
                {
                    UserName    = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    Email       = model.Email,
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var roleResult = await _userManager.AddToRoleAsync(user, role.Name);

                    if (!roleResult.Succeeded)
                    {
                        throw new NullReferenceException("Role is not good");
                    }
                    Customer customer = new Customer();
                    customer.Account = user;
                    _customerService.AddCustomer(customer);
                    var confirmEmailToken = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailToken);
                    var validEmailToken   = WebEncoders.Base64UrlEncode(encodedEmailToken);

                    string url = $"https://www.api.customer.app.fit.ba/account/confirmemail?userid={user.Id}&token={validEmailToken}";

                    await _emailService.SendEmailAsync(user.Email, "Confirm your email", $"<h1>Hi,</h1>" +
                                                       $"<p>Please confirm your email by <a href='{url}'>Clicking here</a></p>");

                    if (_friendService.CheckFriend(user.Email))
                    {
                        var CustomerID = _friendService.GetUserID(user.Email);
                        var customer1  = await _userManager.FindByIdAsync(CustomerID);

                        Coupon coupon = new Coupon()
                        {
                            Code    = _couponService.GenerateChode(),
                            IsValid = true,
                            Value   = 0.10
                        };
                        _couponService.AddCoupon(coupon);

                        await _emailService.SendEmailAsync(customer1.Email, "E-commerce", "<h1>Your friend accepted your invitation</h1>" +
                                                           $"<p>Use the following discount code for your next purchase " + coupon.Code + "</p>");
                    }

                    return(Ok(new { message = "User created successfully!" })); // Status Code: 200
                }


                return(BadRequest(result));
            }

            return(BadRequest("Some properties are not valid")); // Status code: 400
        }