private async Task <Provider> MappingProvider(Provider pro, ProviderServiceViewModel vm, string currentUserContext)
 {
     pro.PhoneNumber = vm.PhoneNumber;
     pro.Status      = (await _checkUserIsAdminQuery.ExecuteAsync(currentUserContext) ||
                        await _getPermissionActionQuery.ExecuteAsync(currentUserContext, ConstantFunctions.PROVIDER, ActionSetting.CanUpdate)) ?
                       Status.WaitingApprove : Status.Pending;
     pro.CityId       = vm.CityId;
     pro.TaxCode      = vm.TaxCode;
     pro.Description  = vm.Description;
     pro.DateModified = DateTime.Now;
     pro.ProviderName = vm.ProviderName;
     pro.Address      = vm.Address;
     pro.AvartarPath  = vm.AvatarPath;
     return(pro);
 }
        private async Task <ProviderServiceViewModel> MappingProvider(Provider vm, CityProvince cityProvince)
        {
            var findUserId = await _userManager.FindByIdAsync(vm.UserId.ToString());

            ProviderServiceViewModel pro = new ProviderServiceViewModel();

            pro.Id            = vm.Id.ToString();
            pro.PhoneNumber   = vm.PhoneNumber;
            pro.Status        = vm.Status;
            pro.CityId        = vm.CityId;
            pro.UserId        = vm.UserId.ToString();
            pro.TaxCode       = pro.TaxCode;
            pro.Description   = pro.Description;
            pro.DateModified  = DateTime.Now;
            pro.ProviderName  = vm.ProviderName;
            pro.Address       = vm.Address;
            pro.CityName      = cityProvince.City;
            pro.ProvinceName  = cityProvince.Province;
            pro.AvatarPath    = vm.AvartarPath;
            pro.ProviderEmail = findUserId.Email;
            return(pro);
        }
        public async Task <CommandResult <ProviderServiceViewModel> > ExecuteAsync(ProviderServiceViewModel vm)
        {
            var userId   = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userName = _userRepository.FindByIdAsync(userId).Result.UserName;

            try
            {
                var checkIsProvider = await _checkUserIsProviderQuery.ExecuteAsync(userId);

                var getProvider = await _providerRepository.FindByIdAsync(Guid.Parse(vm.Id));

                if (getProvider != null)
                {
                    if (getProvider.UserId == Guid.Parse(userId) ||
                        await _checkUserIsAdminQuery.ExecuteAsync(userId) ||
                        await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.PROVIDER, ActionSetting.CanUpdate))
                    {
                        var mapping = await MappingProvider(getProvider, vm, userId);

                        _providerRepository.Update(mapping);
                        await _providerRepository.SaveAsync();

                        await Logging <UpdateProviderServiceCommand> .
                        InformationAsync(ActionCommand.COMMAND_UPDATE, userName, JsonConvert.SerializeObject(vm));

                        if ((await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.PROVIDER, ActionSetting.CanCreate) ||
                             await _checkUserIsAdminQuery.ExecuteAsync(userId)))
                        {
                            var findUserId = await _userRepository.FindByIdAsync(vm.UserId);



                            //Set content for email
                            var getEmailContent = await _getAllEmailServiceQuery.ExecuteAsync();

                            var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmProvider").Value +
                                               mapping.OTPConfirm + '_' + mapping.Id;

                            var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Approve_Provider).FirstOrDefault();
                            getFirstEmail.Message = getFirstEmail.Message.Replace(EmailKey.UserNameKey, findUserId.Email).Replace(EmailKey.ConfirmLink, generateCode);

                            ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject,
                                         getFirstEmail.Message, findUserId.Email).Wait();
                        }
                        return(new CommandResult <ProviderServiceViewModel>
                        {
                            isValid = true,
                            myModel = vm
                        });
                    }
                    else
                    {
                        await Logging <UpdateProviderServiceCommand> .
                        WarningAsync(ActionCommand.COMMAND_UPDATE, userName, ErrorMessageConstant.ERROR_UPDATE_PERMISSION);

                        return(new CommandResult <ProviderServiceViewModel>
                        {
                            isValid = false,
                            errorMessage = ErrorMessageConstant.ERROR_UPDATE_PERMISSION
                        });
                    }
                }
                else
                {
                    await Logging <UpdateProviderServiceCommand> .
                    WarningAsync(ActionCommand.COMMAND_UPDATE, userName, ErrorMessageConstant.ERROR_CANNOT_FIND_ID);

                    return(new CommandResult <ProviderServiceViewModel>
                    {
                        isValid = false,
                        errorMessage = ErrorMessageConstant.ERROR_CANNOT_FIND_ID
                    });
                }
            }
            catch (System.Exception ex)
            {
                await Logging <UpdateProviderServiceCommand> .
                ErrorAsync(ex, ActionCommand.COMMAND_UPDATE, userName, "Has error");

                return(new CommandResult <ProviderServiceViewModel>
                {
                    isValid = false,
                    errorMessage = ex.InnerException.ToString()
                });
            }
        }
        public async Task <IActionResult> UpdateProviderServiceCommand([FromBody] ProviderServiceViewModel vm)
        {
            var model = await _updateProviderServiceCommand.ExecuteAsync(vm);

            return(new OkObjectResult(model));
        }
        public async Task <IActionResult> RejectAProvider(ProviderServiceViewModel vm)
        {
            var model = await _rejectProviderServiceCommand.ExecuteAsync(vm.Id, vm.Reason);

            return(new OkObjectResult(model));
        }
        public async Task <IActionResult> ApproveAProvider(ProviderServiceViewModel vm)
        {
            var model = await _approveProviderServiceCommand.ExecuteAsync(vm.UserId, vm.Id);

            return(new OkObjectResult(model));
        }
        public async Task <IActionResult> RegisterAProvider(ProviderServiceViewModel vm)
        {
            var model = await _registerProviderServiceCommand.ExecuteAsync(vm);

            return(new OkObjectResult(model));
        }
Exemple #8
0
        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()
                });
            }
        }