ikuseiGmbH/smart-village-app-cms

View on GitHub
app/controllers/data_provider_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

class DataProviderController < ApplicationController
  before_action :verify_current_user
  before_action :init_graphql_client

  def edit
    auth_server = SmartVillageApi.auth_server_url
    uri = Addressable::URI.parse("#{auth_server}/data_provider/edit.json")
    data_to_send = { auth_token: @current_user.authentication_token }
    result = ApiRequestService.new(uri.to_s, nil, nil, data_to_send).get_request
    if result.code == "200"
      @data_provider = OpenStruct.new(JSON.parse(result.body))
    else
      flash[:error] = "Achtung! Verbindungsproblem zum Server, bitte versuchen Sie es später noch einmal (#{result.body})"
    end
  end

  def update
    auth_server = SmartVillageApi.auth_server_url
    uri = Addressable::URI.parse("#{auth_server}/data_provider/update.json")
    data_to_send = {
      data_provider: data_provider_params,
      auth_token: @current_user.authentication_token
    }
    result = ApiRequestService.new(uri.to_s, nil, nil, data_to_send).post_request

    if result.code == "200"
      flash[:notice] = "DataProvider wurde erfolgreich gespeichert"
    else
      flash[:error] = result.inspect
    end

    redirect_to "/data_provider"
  end

  def data_provider_params
    params.permit!.require(:data_provider)
  end

  def visibility
    @smart_village.query <<~GRAPHQL
      mutation {
        changeVisibility (
          id: #{params[:id]},
          recordType: "#{visibility_record_type(params)}",
          visible: #{params[:visible] == "true"}
        ) {
          id
          status
          statusCode
        }
      }
    GRAPHQL

    redirect_to visibility_redirect_to(params)
  end

  private

    def visibility_record_type(params)
      return "Survey::Poll" if params[:item_type] === "Survey"
      return "Survey::Comment" if params[:item_type] === "Survey_Comment"
      return "GenericItem" if params[:item_type] === "Noticeboard"

      params[:item_type]
    end

    def visibility_redirect_to(params)
      return "/events" if params[:item_type] === "EventRecord"
      return "/surveys/#{params[:parent_id]}/comments" if params[:item_type] === "Survey_Comment"

      "/#{params[:item_type].underscore.pluralize}"
    end
end