private async Task <Service> MappingService(PostServiceViewModel vm, string currentUserContext) { Service sv = new Service(); sv.CategoryId = vm.CategoryId; sv.DateCreated = DateTime.Now; sv.PriceOfService = vm.PriceOfService; sv.Description = vm.Description; sv.ServiceName = vm.ServiceName; sv.codeConfirm = RandomCodeSupport.RandomString(6); sv.Status = (await _getPermissionActionQuery.ExecuteAsync(currentUserContext, ConstantFunctions.SERVICE, ActionSetting.CanCreate) || await _checkUserIsAdminQuery.ExecuteAsync(currentUserContext)) ? Status.WaitingApprove : Status.Pending; sv.ServiceImages = vm.listImages.Select(x => new ServiceImage { Path = x.Path != null ? x.Path : "", DateCreated = DateTime.Now, isAvatar = x.IsAvatar }).ToList(); sv.TagServices = vm.tagofServices.Where(x => x.isDelete == false && x.isAdd == false).Select(x => new Model.Entities.ServiceModel.TagService { TagId = Guid.Parse(x.TagId), }).ToList(); return(sv); }
public async Task <CommandResult <ProviderServiceViewModel> > ExecuteAsync(ProviderServiceViewModel vm) { var userId = _httpContextAccessor.HttpContext.User.Identity.Name; var userName = _userManager.FindByIdAsync(userId).Result.UserName; try { //Check category has available var availableCategory = await _providerRepository.FindSingleAsync(x => x.ProviderName.ToLower() == vm.ProviderName.ToLower()); if (availableCategory != null) { return(new CommandResult <ProviderServiceViewModel> { isValid = false, errorMessage = "Provider Name has available" }); } var checkUserIsProvider = await _checkUserIsProviderQuery.ExecuteAsync(userId); var mappingProvider = await MappingProvider(vm, Guid.Parse(userId), userId); //Check user is Provider var getAllUserProvider = await _providerRepository.FindAllAsync(x => x.UserId == mappingProvider.UserId); if (getAllUserProvider.Count() >= 1) { return(new CommandResult <ProviderServiceViewModel> { isValid = false, errorMessage = "This user cannot register over 1 provider" }); } var userEmail = ""; if (vm.UserId == null) { var findUser = await _userManager.FindByIdAsync(userId); userEmail = findUser.Email; } else { var findUser = await _userManager.FindByIdAsync(vm.UserId); userEmail = findUser.Email; } mappingProvider.OTPConfirm = RandomCodeSupport.RandomString(6); await _providerRepository.Add(mappingProvider); await _providerRepository.SaveAsync(); var getEmailContent = await _getAllEmailServiceQuery.ExecuteAsync(); if ((await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.PROVIDER, ActionSetting.CanCreate) || await _checkUserIsAdminQuery.ExecuteAsync(userId))) { var checkUser = !string.IsNullOrEmpty(vm.UserId) ? vm.UserId : userId; var findUserId = await _userManager.FindByIdAsync(checkUser); var getAllRole = await _userManager.GetRolesAsync(findUserId); var flagProvider = 0; foreach (var item in getAllRole) { if (item == ConstantRoles.Provider) { flagProvider++; } } if (flagProvider == 0) { await _userManager.AddToRoleAsync(findUserId, ConstantRoles.Provider); } //Set content for email var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmProvider").Value + mappingProvider.OTPConfirm + '_' + mappingProvider.Id; var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Approve_Provider).FirstOrDefault(); getFirstEmail.Message = getFirstEmail.Message.Replace(EmailKey.UserNameKey, userEmail).Replace(EmailKey.ConfirmLink, generateCode); ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, findUserId.Email).Wait(); } else { var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Receive_Register_Provider).FirstOrDefault(); getFirstEmail.Message = getFirstEmail.Message.Replace(EmailKey.UserNameKey, userEmail); //Check if UserId null var checkUser = !string.IsNullOrEmpty(vm.UserId) ? vm.UserId : userId; var findUserId = await _userManager.FindByIdAsync(checkUser); ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, findUserId.Email).Wait(); } vm.Id = mappingProvider.Id.ToString(); vm.Status = mappingProvider.Status; await Logging <RegisterProviderServiceCommand> . InformationAsync(ActionCommand.COMMAND_ADD, userName, JsonConvert.SerializeObject(vm)); return(new CommandResult <ProviderServiceViewModel> { isValid = true, myModel = vm }); } catch (System.Exception ex) { await Logging <RegisterProviderServiceCommand> . ErrorAsync(ex, ActionCommand.COMMAND_ADD, userName, "Has error"); return(new CommandResult <ProviderServiceViewModel> { isValid = false, myModel = vm, errorMessage = ex.InnerException.ToString() }); } }
public async Task <CommandResult <NewsProviderViewModel> > ExecuteAsync(NewsProviderViewModel vm) { var userId = _httpContextAccessor.HttpContext.User.Identity.Name; var userName = _userManager.FindByIdAsync(userId).Result.UserName; try { var getIsProvider = await _checkUserIsProviderQuery.ExecuteAsync(userId); if (await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.NEWS, ActionSetting.CanCreate) || getIsProvider.isValid) { var mappingProvider = await MappingProvider(vm, Guid.Parse(vm.ProviderId), userId); mappingProvider.CodeConfirm = RandomCodeSupport.RandomString(6); await _newProviderRepository.Add(mappingProvider); await _newProviderRepository.SaveAsync(); vm.Id = mappingProvider.Id; vm.Status = mappingProvider.Status; await Logging <RegisterNewsProviderServiceCommand> . InformationAsync(ActionCommand.COMMAND_ADD, userName, JsonConvert.SerializeObject(vm)); //Send mail confirm //Get Provider Information var providerInformation = await _providerRepository.FindByIdAsync(Guid.Parse(vm.ProviderId)); var findUser = await _userManager.FindByIdAsync(providerInformation.UserId.ToString()); //Set content for email var getEmailContent = await _getAllEmailServiceQuery.ExecuteAsync(); if (getIsProvider.isValid) { var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Receive_Register_News).FirstOrDefault(); getFirstEmail.Message = getFirstEmail.Message.Replace(EmailKey.UserNameKey, findUser.Email); ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, findUser.Email).Wait(); } else { var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmNewsProvider").Value + mappingProvider.CodeConfirm + '_' + mappingProvider.Id; var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Approve_News).FirstOrDefault(); getFirstEmail.Message = getFirstEmail.Message.Replace(EmailKey.UserNameKey, findUser.Email). Replace(EmailKey.ConfirmLink, generateCode); ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, findUser.Email).Wait(); } return(new CommandResult <NewsProviderViewModel> { isValid = true, myModel = vm }); } else { await Logging <RegisterNewsProviderServiceCommand> . WarningAsync(ActionCommand.COMMAND_ADD, userName, ErrorMessageConstant.ERROR_DELETE_PERMISSION); return(new CommandResult <NewsProviderViewModel> { isValid = true, errorMessage = ErrorMessageConstant.ERROR_ADD_PERMISSION }); } } catch (System.Exception ex) { await Logging <RegisterNewsProviderServiceCommand> . ErrorAsync(ex, ActionCommand.COMMAND_ADD, userName, "Has error"); return(new CommandResult <NewsProviderViewModel> { isValid = false, myModel = vm, errorMessage = ex.InnerException.ToString() }); } }