Пример #1
0
        public async Task <IActionResult> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
        {
            using (var transaction = _baseRepository.GetTransaction())
            {
                try
                {
                    var validator = new RegisterDataValidator();
                    var result    = validator.Validate(request.Data);

                    if (!result.IsValid)
                    {
                        var errorMsg = "";

                        foreach (var err in result.Errors)
                        {
                            errorMsg += err.ErrorMessage + "\r\n";
                        }

                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 406,
                            ErrorMessage = errorMsg,
                            Data = null
                        }));
                    }

                    if (await _userRepository.GetUserByEmailAsync(request.Data.User.Email) != null)
                    {
                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 409,
                            ErrorMessage = "Ten adres email jest już w użyciu.",
                            Data = null
                        }));
                    }

                    if (await _userRepository.GetUserByLoginAsync(request.Data.User.Login) != null)
                    {
                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 409,
                            ErrorMessage = "Ta nazwa użytkownika jest już w użyciu.",
                            Data = null
                        }));
                    }

                    if (await _companyRepository.GetByNameAsync(request.Data.Company.CompanyName) != null)
                    {
                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 409,
                            ErrorMessage = "Ta firma istnieje w bazie danych.",
                            Data = null
                        }));
                    }

                    var userAddress = new Address
                    {
                        ApartmentNumber = request.Data.UserAddress.ApartmentNumber,
                        City            = request.Data.UserAddress.City,
                        HouseNumber     = request.Data.UserAddress.HouseNumber,
                        PostCode        = request.Data.UserAddress.PostCode,
                        Province        = request.Data.UserAddress.Province,
                        Street          = request.Data.UserAddress.Street
                    };
                    var createdUserAddress = await _addressRepository.CreateAddressAsync(userAddress);

                    var companyAddress = new Address
                    {
                        ApartmentNumber = request.Data.CompanyAddress.ApartmentNumber,
                        City            = request.Data.CompanyAddress.City,
                        HouseNumber     = request.Data.CompanyAddress.HouseNumber,
                        PostCode        = request.Data.CompanyAddress.PostCode,
                        Province        = request.Data.CompanyAddress.Province,
                        Street          = request.Data.CompanyAddress.Street
                    };
                    var createdCompanyAddress = await _addressRepository.CreateAddressAsync(companyAddress);

                    var company = new Company
                    {
                        Address     = createdCompanyAddress,
                        CompanyName = request.Data.Company.CompanyName,
                        NIP         = "",
                        Employees   = new List <ApplicationUser>(),
                        Regon       = ""
                    };
                    var createdCompany = await _companyRepository.CreateCompanyAsync(company);

                    var user = new ApplicationUser
                    {
                        Address            = createdUserAddress,
                        CompanyPosition    = "IT Team Leader",
                        Email              = request.Data.User.Email,
                        EmailConfirmed     = false,
                        FirstName          = request.Data.User.FirstName,
                        LastName           = request.Data.User.LastName,
                        NormalizedEmail    = request.Data.User.Email,
                        NormalizedUserName = request.Data.User.Login,
                        UserName           = request.Data.User.Login,
                        PhoneNumber        = request.Data.User.PhoneNumber,
                        Company            = createdCompany,
                        Gender             = request.Data.User.Gender,
                        WorkStartDate      = DateTime.Now.Date
                    };
                    var createdUser = await _userRepository.CreateUserAsync(user, request.Data.User.Password);

                    var appClaims = await _claimRepository.GetApplicationClaimsAsync();

                    await _userRepository.AssignClaimsAsync(appClaims, createdUser.Id);

                    var token = await _userRepository.GenerateEmailConfirmationTokenAsync(createdUser);

                    var confirmationLink = $"http://localhost:3000/confirmEmail?userId={createdUser.Id}&token={token}";

                    EmailSender sender = new EmailSender();
                    await sender.SendEmailAsync(createdUser.Email, "Konto zostało utworzone",
                                                $"Twój link do potwierdzenia konta:<br>{confirmationLink}<br>Dziękujemy za skorzystanie z naszych usług");

                    transaction.Commit();
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Code = 201,
                        Data = null,
                        ErrorMessage = ""
                    }));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Code = 500,
                        Data = null,
                        ErrorMessage = "Nastąpił problem z serwerem, skontaktuj się z działem IT."
                    }));
                }
            }
        }
Пример #2
0
        public async Task <IActionResult> Handle(UpsertUserCommand request, CancellationToken cancellationToken)
        {
            using (var transaction = _baseRepository.GetTransaction())
            {
                try
                {
                    var validator = new UserUpsertValidator();
                    var result    = validator.Validate(request.Dto);
                    if (!result.IsValid)
                    {
                        var errorMsg = "";

                        foreach (var err in result.Errors)
                        {
                            errorMsg += err.ErrorMessage + "\r\n";
                        }

                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 406,
                            ErrorMessage = errorMsg,
                            Data = null
                        }));
                    }

                    if (request.Dto.Id == "" && await _userRepository.GetUserByEmailAsync(request.Dto.Email) != null)
                    {
                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 409,
                            ErrorMessage = "Ten adres email jest już w użyciu.",
                            Data = null
                        }));
                    }

                    if (request.Dto.Id == "" && await _userRepository.GetUserByLoginAsync(request.Dto.Login) != null)
                    {
                        return(new JsonResult(new ApiResponse <object>
                        {
                            Code = 409,
                            ErrorMessage = "Ta nazwa użytkownika jest już w użyciu.",
                            Data = null
                        }));
                    }

                    var user = new ApplicationUser();

                    if (request.Dto.Id == "")
                    {
                        var userAddress = new Address
                        {
                            ApartmentNumber = request.Dto.Address.ApartmentNumber,
                            City            = request.Dto.Address.City,
                            HouseNumber     = request.Dto.Address.HouseNumber,
                            PostCode        = request.Dto.Address.PostCode,
                            Province        = request.Dto.Address.Province,
                            Street          = request.Dto.Address.Street
                        };
                        var DBUserAddress = await _addressRepository.CreateAddressAsync(userAddress);

                        user = new ApplicationUser
                        {
                            Address            = DBUserAddress,
                            CompanyPosition    = request.Dto.Department,
                            Email              = request.Dto.Email,
                            EmailConfirmed     = false,
                            FirstName          = request.Dto.FirstName,
                            LastName           = request.Dto.LastName,
                            NormalizedEmail    = request.Dto.Email,
                            NormalizedUserName = request.Dto.Login,
                            UserName           = request.Dto.Login,
                            PhoneNumber        = request.Dto.PhoneNumber,
                            Company            = await _companyRepository.GetByIdAsync(request.CompanyId),
                            Gender             = request.Dto.Gender,
                            WorkStartDate      = DateTime.Now.Date
                        };
                        user = await _userRepository.CreateUserAsync(user, request.Dto.Password);
                    }
                    else
                    {
                        user = await _userRepository.GetUserByIdAsync(request.Dto.Id);

                        user.Address.PostCode        = request.Dto.Address.PostCode;
                        user.Address.Province        = request.Dto.Address.Province;
                        user.Address.Street          = request.Dto.Address.Street;
                        user.Address.HouseNumber     = request.Dto.Address.HouseNumber;
                        user.Address.City            = request.Dto.Address.City;
                        user.Address.ApartmentNumber = request.Dto.Address.ApartmentNumber;
                        user.Address = await _addressRepository.UpdateAddressAsync(user.Address);

                        user.Email              = request.Dto.Email;
                        user.FirstName          = request.Dto.FirstName;
                        user.Gender             = request.Dto.Gender;
                        user.LastName           = request.Dto.LastName;
                        user.PhoneNumber        = request.Dto.PhoneNumber;
                        user.UserName           = request.Dto.Login;
                        user.NormalizedUserName = request.Dto.Login;
                        user.NormalizedEmail    = request.Dto.Email;
                        user.CompanyPosition    = request.Dto.Department;

                        user = await _userRepository.UpdateUserAsync(user, request.Dto.Password);
                    }

                    var appClaims = await _claimRepository.GetApplicationClaimsAsync();

                    await _userRepository.AssignClaimsAsync(request.Dto.Permissions.Where(x => x.Selected).Select(x => x.Name).ToList(), user.Id);

                    transaction.Commit();
                    return(new JsonResult(new ApiResponse <string>
                    {
                        Code = 201,
                        Data = user.Id,
                        ErrorMessage = ""
                    }));
                }
                catch
                {
                    transaction.Rollback();
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Code = 500,
                        Data = null,
                        ErrorMessage = "Nastąpił problem z serwerem, skontaktuj się z działem IT."
                    }));
                }
            }
        }