Exemplo n.º 1
0
        public async Task <string> ChangeUserActiveStatus([FromBody] JObject data)
        {
            try
            {
                if (data != null)
                {
                    if (data["UserId"] == null)
                    {
                        return("UserId cannot be null");
                    }
                    else if (data["Action"] == null)
                    {
                        return("Action cannot be null");
                    }

                    StringConversion stringConversion = new StringConversion();
                    string           actualUserId     = stringConversion.DecryptString(data["UserId"]?.ToString()); //Since the userId sent to the view is encrypted, before sending it to the BOS API, we have to decrypt it

                    var action = data["Action"]?.ToString();

                    //Based on the action that has been requested, we either make a call to the BOS' ActivateUser API or DeactivateUser API
                    if (action == "activate")
                    {
                        var response = await _bosAuthClient.ActivateUserAsync(Guid.Parse(actualUserId)); //Making the BOS API call with the userId

                        if (response != null && response.IsSuccessStatusCode)
                        {
                            return("The user has been activated successfully"); //On success, returning an appropriate message
                        }
                        else
                        {
                            return(response.BOSErrors[0].Message); //On error, returing the BOS error message
                        }
                    }
                    else if (action == "deactivate")
                    {
                        var response = await _bosAuthClient.DeactivateUserAsync(Guid.Parse(actualUserId));  //Making the BOS API call with the userId

                        if (response != null && response.IsSuccessStatusCode)
                        {
                            return("The user has been deactivated successfully"); //On success, returning an appropriate message
                        }
                        else
                        {
                            return(response.BOSErrors[0].Message); //On error, returing the BOS error message
                        }
                    }
                    else
                    {
                        return("You are trying to perform an unrecognized operation");
                    }
                }
                else
                {
                    return("Data cannot be null");
                }
            }
            catch (Exception ex)
            {
                Logger.LogException("Users", "ChangeUserActiveStatus", ex);
                return(ex.Message);
            }
        }