public async Task <IActionResult> RegisterContractor([FromBody] RegisterContractorRequestDto model) { if (_appDbContext.Organisations.Any(org => org.OrganisationName == model.OrganisationName)) { return(BadRequest(new List <IdentityError> { new IdentityError { Code = "OrganisationTaken", Description = "The given organisation name already exists." } })); } ApplicationUser user = new ApplicationUser() { Email = model.Email, UserName = model.Email, FullName = model.FullName, IsActive = true, IsInitialSet = true, PhoneNumber = model.PhoneNumber, SecurityStamp = Guid.NewGuid().ToString(), }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var org = _mapper.Map <Organisation>(model); // The model is certainly mappable since the Required properties are identical in the input DTO. await _appDbContext.Organisations.AddAsync(org); await _appDbContext.SaveChangesAsync(); await _userManager.AddClaimAsync(user, new Claim(Seniority.OrganisationIdClaimKey, org.Id.ToString())); await _userManager.AddToRoleAsync(user, Seniority.Contractor); var token = await _userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Auth", new { userId = user.Id, token = token }, Request.Scheme); _queue.QueueBackgroundWorkItem(async token => { using (var scope = _serviceScopeFactory.CreateScope()) { var mailService = scope.ServiceProvider.GetRequiredService <IMailService>(); await mailService.SendConfirmationEmailContractorAsync(org, user, confirmationLink); } }); return(Ok()); } else { return(BadRequest(result.Errors.ToList())); } }
protected async Task <HttpResponseMessage> RegisterContractorOneAsync() { using (var serviceScope = _serviceProvider.CreateScope()) { await Seed.SeedAsync(serviceScope); } var content = new RegisterContractorRequestDto { Email = TestConstants.ContractorOneEmail, FullName = "Matthew Test", Password = TestConstants.ContractorOnePassword, ConfirmPassword = TestConstants.ContractorOnePassword, OrganisationName = "Test Corp X", PhoneNumber = "000000000000000", Address = "TES123", Industry = "Testing Work" }; var response = await TestClient.PostAsync("api/auth/register/contractor", new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json")); return(response); }