public ServiceResult <List <CreateSupervisorAgentDto> > Create(List <CreateSupervisorAgentDto> createSupervisor) { if (createSupervisor == null) { return(new ServiceResult <List <CreateSupervisorAgentDto> >(null)); } SupervisorUserAgent newAgents; createSupervisor.ForEach(x => { newAgents = new SupervisorUserAgent() { Id = new Guid(), AgentId = x.AgentId, SupervisorId = x.SupervisorId, SupervisorId2 = x.SupervisorId2 }; _Context.SupervisorUserAgents.Add(newAgents); _Context.SaveChanges(); }); return(new ServiceResult <List <CreateSupervisorAgentDto> >(createSupervisor)); }
public async Task <ServiceResult <CreateUserDto> > CreateAsync(CreateUserDto user) { var valid = _fluentValidatorCreateUser.Validate(user); if (!valid.IsValid) { return(_mapper.Map <ServiceResult <CreateUserDto> >(valid.ToServiceResult <CreateUserDto>(null))); } var notifications = new ServiceResult <CreateUserDto>(); var userExistOrNot = await _userManager.FindByNameAsync(user.UserName); var distribution = _context.Distributions.FirstOrDefault(x => x.Id == user.DistributionId); if (userExistOrNot == null) { var NewUser = new User { Id = Guid.NewGuid(), Dni = user.Dni, UserName = user.UserName, Email = user.UserName, PhoneNumber = user.PhoneNumber, Distribution = distribution, DistributionId = user.DistributionId, FirstName = user.FirstName, LastName = user.LastName, CategoryId = user.CategoryId }; var result = await _userManager.CreateAsync(NewUser, user.Password); // update password if it was entered if (!string.IsNullOrWhiteSpace(user.Password)) { NewUser.PasswordHash = _userManager.PasswordHasher.HashPassword(NewUser, user.Password); } //actualizo los roles del usuario foreach (var role in user.RolesUser) { await UpdateUserRoleWhenModify(NewUser.Id, role.Id, role.RolBelongUser); } if (!result.Succeeded) { return(result.ToServiceResult <CreateUserDto>(null)); } var newSupervisor = new SupervisorUserAgent() { Id = new Guid(), Agents = NewUser, }; if (user.SupervisorAgentId.CompareTo(Guid.Empty) != 0) { newSupervisor.SupervisorId = user.SupervisorAgentId; } if (user.SupervisorAgentId2.CompareTo(Guid.Empty) != 0) { newSupervisor.SupervisorId2 = user.SupervisorAgentId2; } _context.SupervisorUserAgents.Add(newSupervisor); _context.SaveChanges(); string template = Path.Combine(StaticFilesDirectory, "Templates"); var engine = new RazorLightEngineBuilder() .UseFilesystemProject(template) .UseMemoryCachingProvider() .Build(); var callbackUrl = string.Format(_configuration["AppSettings:baseUrl"] + "/login"); var html = new CreateUserHtmlDto() { LastName = user.LastName, FirstName = user.FirstName, UserName = user.UserName, Password = user.Password, CallbackUrl = callbackUrl }; string resultHtml = await engine.CompileRenderAsync("Email/whenAnUserIsCreated.cshtml", html); var emailSended = await _emailSender.SendEmail(NewUser.Email, "Alta al sistema", resultHtml); if (!(emailSended.StatusCode == HttpStatusCode.Accepted)) { if (emailSended.StatusCode == HttpStatusCode.Unauthorized) { notifications.AddError("error", "La clave api expiró , está mal escrito o es errónea"); } else { notifications.AddError("error", "La solicitud no pudo ser enviada al correo del supervisor."); } return(notifications); } } return(new ServiceResult <CreateUserDto>(user)); }