public async Task <UserAccountDto> RegisterAgentAsync(RegisterAgentDto registerAgentDto, CancellationToken cancellationToken) { registerAgentDto.Email = registerAgentDto.Email.ToLower().Trim(); registerAgentDto.Username = registerAgentDto.Username.ToLower().Trim(); var estate = await CheckIfEstateExistById(registerAgentDto.RealEstateId, cancellationToken); await CheckIfUserIsValid(registerAgentDto.Email, registerAgentDto.Username, cancellationToken); var activationKey = _hasher.CalculateHash(registerAgentDto.Firstname + registerAgentDto.Lastname + registerAgentDto.Email + registerAgentDto.Username); var passwordHash = _passwordService.HashUserPassword(registerAgentDto.Email, registerAgentDto.Password); var user = CreateUser(registerAgentDto, UserGroup.Agent, registerAgentDto.Password, passwordHash, activationKey); _entityService.DbContext.Agent.Add(new Agent { RealEstateId = estate.Id, UserAccountId = user.Id, MetadataJson = "{}" }); _entityService.DbContext.SaveChanges(); return(await AuthenticateAsync(registerAgentDto.Email, registerAgentDto.Password, cancellationToken)); }
public async Task <ActionResult <AgentAccountDto> > CreateAgent([FromBody] AgentAccountDto dto, CancellationToken cancellationToken) { if (_userProvider.RealEstateId is null) { throw new AppException("you cannot create new agent please try again later"); } var dupUser = await _userAccountService.GetAsync(u => u.Email == dto.Email || u.UserName == dto.UserName, cancellationToken); if (dupUser != null) { if (dupUser.Email == dto.Email) { throw new AppException("There is another user registered with this email address. Please chose another one."); } if (dupUser.UserName == dto.UserName) { throw new AppException("There is another user registered with this username. Please chose another one."); } } var passwordHash = _passwordService.HashUserPassword(dto.Email, dto.Password); var user = await _userAccountService.CreateAsync(new UserAccount { ActivationKey = _fastHasher.CalculateHash(dto.FirstName + dto.LastName + dto.Email + dto.UserName), Email = dto.Email, UserName = dto.UserName, FirstName = dto.FirstName, LastName = dto.LastName, HasExternalAuthentication = false, IsConfirmed = false, PasswordHash = passwordHash, RegistrationDate = DateTime.Now, Address01 = dto.Address01, Address02 = dto.Address02, Phone01 = dto.Phone01, Phone02 = dto.Phone02, IsActive = true, MiddleName = dto.MiddleName, ReferralCode = _fastHasher.CalculateTimeHash(dto.Email), ZipCode = dto.ZipCode, }, cancellationToken); dto.UserId = user.Id; var agent = await ModelService.CreateAsync(new Agent { Description = dto.Description, HasPublishingAuthorization = dto.HasPublishingAuthorization, IsResponsible = dto.IsResponsible, RealEstateId = _userProvider.RealEstateId.Value, MetadataJson = "{}", UserAccountId = user.Id, }, cancellationToken); dto.AgentId = agent.Id; return(CreatedAtAction(nameof(CreateAgent), dto)); }