private Company CreateCompany(CompanyQuickRegistrationVm registrationVm, Random randomizer) { string companyZipCode = $"{randomizer.Next(00001, 99999):5}"; Company company = new Company() { Active = true, CompanyCreationDate = DateTime.Now.AddMonths(randomizer.Next(3, 120)), CompanyEmail = registrationVm.CompanyEmail, CompanyName = registrationVm.CompanyName, CompanyPostAddress = $"companyZipCode {registrationVm.CompanyName}", CompanyRegistrationDate = DateTime.Now, CompanyZipCode = companyZipCode, TaxId = randomizer.Next().ToString(), CompanyState = _StringLocalizer["European state"], EnableLockoutForEmployees = false, CompanyPublicComment = _StringLocalizer["Automatic registration company"] }; return(company); }
public async Task <ActionResult> CreateTestCompany(CompanyQuickRegistrationVm registrationVm) { if ((await _UserManager.FindByNameAsync(registrationVm.CompanyAdminUserName)) != null) { ModelState.AddModelError("", _StringLocalizer["User with name '{0}' already exists. Please choose another", registrationVm.CompanyAdminUserName]); return(View(CompanyRegistrationForm, registrationVm)); } if ((await _UserManager.FindByEmailAsync(registrationVm.CompanyEmail)) != null) { ModelState.AddModelError("", _StringLocalizer["User with email '{0}' already exists. Please choose another", registrationVm.CompanyEmail]); return(View(CompanyRegistrationForm, registrationVm)); } Random randomizer = new Random(); bool result = true; StringBuilder messageBuilder = null; var company = CreateCompany(registrationVm, randomizer); result &= await _UnitOfWork.Companies.CreateAsync(company); var leaveTypes = CreateLeaveTypes(company); if (result) { var employeesData = await RegisterEmployees(company, registrationVm, randomizer); if (employeesData.Item1) { messageBuilder = employeesData.Item2; } } else { ModelState.AddModelError("", _StringLocalizer["Employees registration failed"]); } foreach (LeaveType leaveType in leaveTypes) { result &= await _UnitOfWork.LeaveTypes.CreateAsync(leaveType); } if (result) { result &= await _UnitOfWork.Save(); } if (result) { try { await _EmailSender.SendEmailAsync(email : registrationVm.CompanyEmail, subject : _StringLocalizer["Test company registration ({0})", registrationVm.CompanyName], messageBuilder.ToString()); } catch (AggregateException ae) { var aef = ae.Flatten(); _Logger.LogError(aef.Message, aef); result &= false; } catch (Exception e) { _Logger.LogError(e.Message, e); result &= false; } } else { ModelState.AddModelError("", _StringLocalizer["Company creation failed"]); } if (result) { return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", _StringLocalizer["Email sending failed"]); return(View(CompanyRegistrationForm, registrationVm)); } }
public async Task <ActionResult> CreateTestCompany() { CompanyQuickRegistrationVm registrationVm = await Task.FromResult(new CompanyQuickRegistrationVm()); return(View(CompanyRegistrationForm, registrationVm)); }
private async Task <Tuple <bool, StringBuilder> > RegisterEmployees(Company company, CompanyQuickRegistrationVm registrationVm, Random randomizer) { bool employeesCreationResult = true; var managerData = await CreateMasterEmployeeAndPassword(company, registrationVm, randomizer); employeesCreationResult &= await _UnitOfWork.Employees.RegisterEmployeeAsync(_UserManager, managerData.Item1, managerData.Item2); employeesCreationResult &= employeesCreationResult && await _UnitOfWork.Employees.SetEmployeesRoles(_UserManager, managerData.Item1, UserRoles.CompanyAdministrator); Tuple <Employee, string>[] ordinaryEmployees = new Tuple <Employee, string> [randomizer.Next(2, 8)]; for (int i = 0; i < ordinaryEmployees.Length && employeesCreationResult; i++) { ordinaryEmployees[i] = await CreateOrdinaryEmployeeAndPassword(company, randomizer, manager : managerData.Item1); employeesCreationResult &= employeesCreationResult ? await _UnitOfWork.Employees.RegisterEmployeeAsync(_UserManager, ordinaryEmployees[i].Item1, ordinaryEmployees[i].Item2) : employeesCreationResult; employeesCreationResult &= employeesCreationResult ? await _UnitOfWork.Employees.SetEmployeesRoles(_UserManager, ordinaryEmployees[i].Item1, UserRoles.Employee) : employeesCreationResult; } var messageBuilder = GetWelcomeHtmlMessage(managerData, ordinaryEmployees, company); return(Tuple.Create(employeesCreationResult, messageBuilder)); }
private async Task <Tuple <Employee, string> > CreateMasterEmployeeAndPassword(Company company, CompanyQuickRegistrationVm registrationVm, Random randomizer) { var daysFromCreation = (company.CompanyCreationDate - DateTime.Now).TotalDays; Employee result = new Employee() { UserName = registrationVm.CompanyAdminUserName, Email = registrationVm.CompanyEmail, ContactMail = registrationVm.CompanyEmail, Title = _StringLocalizer["Master"], FirstName = "Administrator", LastName = company.CompanyName, Company = company, Manager = null, EmploymentDate = DateTime.Now.AddDays(randomizer.NextDouble() * daysFromCreation), DateOfBirth = DateTime.Now.AddDays(Convert.ToDouble(randomizer.Next(19 * 365, 65 * 365))), TaxRate = randomizer.Next().ToString() }; string password = _PasswordGenerator.GeneratePassword(); return(await Task.FromResult(Tuple.Create(result, password))); }