public async Task <EmployeeDto> RegisterAsync(RegisterEmployeeRequest request) { var existingEmployee = _employeeRepository.GetByEmail(request.Email); if (existingEmployee != null) { throw new InsertFailedException("Email already existing in the System!"); } var id = Guid.NewGuid().ToString(); var employee = new Employee { Id = id, CreatedBy = id, CreateOn = DateTime.UtcNow, IsActive = true, Email = request.Email, Name = request.Name, IsAdmin = request.IsAdmin, IsEmailVerified = false }; await _employeeRepository.CreateAsync(employee); return(MapEmployeeDto(employee)); }
public async Task <IActionResult> CreateAsync([FromBody] RegisterEmployeeRequest request) { if (!ModelState.IsValid) { return(BadRequest()); } var newEmploee = await _employeeService.RegisterAsync(request); var stateObj = new EmailVerificationDto { Email = newEmploee.Email, EmployeeId = newEmploee.Id, ValidUntil = DateTime.Now.AddMinutes(15) }; var state = CryptoHelper.Encode(CryptoHelper.Encrypt(CryptoHelper.Serialize(stateObj), _configuration["SecurityConfiguraiton:EncryptionKey"])); _emailService.SendEmail(new EmailDto { To = new List <string> { newEmploee.Email }, Subject = "AOT Invitation Email Verification", IsHtml = true, Body = $"Hi {newEmploee.Name},<br/><br/>Welcome to AOT.<br/><br />Please click on below link to verify your email address. Token validity: {stateObj.ValidUntil.ToShortTimeString()}.<br/><br/><a href=\"https://localhost:44300/api/v1/Employees/verify?state={state}\">Please click here to verify</a><br/><br/>Thanks, AOT Team" }); return(Ok(newEmploee)); }
public async Task <Response <string> > RegisterAsync(int companyId, RegisterEmployeeRequest request) { if (request.Role == Roles.SuperAdmin) { throw new ApiException("Role SuperAdmin can be added to this user."); } var userWithSameUserName = await _userManager.FindByNameAsync(request.UserName); if (userWithSameUserName != null) { throw new ApiException($"Username '{request.UserName}' is already taken."); } var user = new ApplicationUser { Email = request.Email, FirstName = request.FirstName, LastName = request.LastName, UserName = request.UserName, CompanyId = companyId }; var userWithSameEmail = await _userManager.FindByEmailAsync(request.Email); if (userWithSameEmail == null) { user.EmailConfirmed = true; var result = await _userManager.CreateAsync(user, request.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, request.Role.ToString()); //var verificationUri = await SendVerificationEmail(user, origin); //TODO: Attach Email Service here and configure it via appsettings //await _emailService.SendAsync(new Application.DTOs.Email.EmailRequest() { From = "*****@*****.**", To = user.Email, Body = $"Please confirm your account by visiting this URL {verificationUri}", Subject = "Confirm Registration" }); //return new Response<string>(user.Id, message: $"User Registered. Please confirm your account by visiting this URL {verificationUri}"); var permissions = request.Permissions.Select(x => new UserPermission { PermissionId = x.PermissionId, UserId = user.Id, Enabled = x.Enabled }).ToList(); _applicationDbContext.Set <UserPermission>().AddRange(permissions); _applicationDbContext.SaveChanges(); return(new Response <string>(user.Id)); } else { throw new ApiException($"{result.Errors}"); } } else { throw new ApiException($"Email {request.Email } is already registered."); } }
public ActionResult RegisterEmployyeInCompany(RegisterEmployeeRequest request) { if (!ModelState.IsValid) { return(CustomResponse(ModelState)); } try { var companyResult = _companyAppService.RegisterEmployeeInCompany(request.IdCompany, request.Employees); if (companyResult == null) { AdicionarErroProcessamento("Não foi possível localizar a empresa pelo id informado."); return(CustomResponse()); } if (companyResult.Notifications.Any()) { return(CustomResponse(companyResult.Notifications)); } return(CustomResponse()); } catch (Exception ex) { MessageException(); return(CustomExceptionResponse()); } }
public async Task <IActionResult> CreateAccountAsync(RegisterEmployeeRequest request) { var currentUser = await _authenticatedUserService.GetCurentApplicationUser(); return(Ok(await _accountService.RegisterAsync(currentUser.CompanyId.Value, request))); }