public async Task <CommandResult <ProviderServiceViewModel> > ExecuteAsync(string userProvider, string providerId)
        {
            var userId   = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userName = _userRepository.FindByIdAsync(userId).Result.UserName;

            try
            {
                if (await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.PROVIDER, ActionSetting.CanUpdate))
                {
                    var getUserService = await _getPostUserServiceByUserIdQuery.ExecuteAsync(userProvider);

                    if (getUserService != null)
                    {
                        await _deleteServiceFromUserCommand.ExecuteAsync(getUserService.Id.ToString());
                    }

                    var mappingProvider = await _providerRepository.FindByIdAsync(Guid.Parse(providerId));

                    //Write Log
                    await Logging <ApproveProviderServiceCommand> .
                    WarningAsync(ActionCommand.COMMAND_APPROVE, userName, ErrorMessageConstant.ERROR_CANNOT_FIND_ID);

                    if (mappingProvider == null)
                    {
                        return(new CommandResult <ProviderServiceViewModel>
                        {
                            isValid = false,
                            errorMessage = ErrorMessageConstant.ERROR_CANNOT_FIND_ID
                        });
                    }
                    //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"
                        });
                    }
                    mappingProvider.Status = Status.WaitingApprove;
                    _providerRepository.Update(mappingProvider);
                    var findUserId = await _userRepository.FindByIdAsync(mappingProvider.UserId.ToString());

                    var getAllRoleUser = await _userRepository.GetRolesAsync(findUserId);

                    var countRole = 0;
                    foreach (var item in getAllRoleUser)
                    {
                        if (item == ConstantRoles.Provider)
                        {
                            countRole++;
                        }
                    }
                    if (countRole == 0)
                    {
                        await _userRepository.AddToRoleAsync(findUserId, ConstantRoles.Provider);
                    }
                    await _providerRepository.SaveAsync();

                    var userMail = await _userRepository.FindByIdAsync(mappingProvider.UserId.ToString());

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

                    var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Approve_Provider).FirstOrDefault();

                    var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmProvider").Value +
                                       mappingProvider.OTPConfirm + '_' + mappingProvider.Id;
                    getFirstEmail.Message = getFirstEmail.Message.
                                            Replace(EmailKey.UserNameKey, userMail.Email).
                                            Replace(EmailKey.ConfirmLink, generateCode);

                    ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject,
                                 getFirstEmail.Message, userMail.Email).Wait();

                    //Write log
                    await LoggingUser <ApproveProviderServiceCommand> .
                    InformationAsync(mappingProvider.UserId.ToString(), userName, userName + "Nhà cung cấp:" + mappingProvider.ProviderName + "đã được chấp thuận. Check mail để xác nhận dịch vụ");

                    await Logging <ApproveProviderServiceCommand> .
                    InformationAsync(ActionCommand.COMMAND_APPROVE, userName, mappingProvider.ProviderName + "has been approved");

                    return(new CommandResult <ProviderServiceViewModel>
                    {
                        isValid = true,
                    });
                }
                else
                {
                    await Logging <ApproveProviderServiceCommand> .
                    WarningAsync(ActionCommand.COMMAND_APPROVE, userName, ErrorMessageConstant.ERROR_UPDATE_PERMISSION);

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

                return(new CommandResult <ProviderServiceViewModel>
                {
                    isValid = false,
                    errorMessage = ex.InnerException.ToString()
                });
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> DeleteServiceFromUser(string id)
        {
            var model = await _deleteServiceFromUserCommand.ExecuteAsync(id);

            return(new OkObjectResult(model));
        }