# frozen_string_literal: true module NeetoCommonsBackend module Helpers class << self attr_reader :current_user, :organization def common_client_props(organization, current_user) @organization = organization @current_user = current_user @neeto_data = neeto_data if current_user.present? global_props = { rails_env: Rails.env, honeybadger_api_key: ENV["HONEYBADGER_JS_API_KEY"] || ENV["HONEYBADGER_API_KEY"], honeybadger_revision: ENV["HEROKU_SLUG_COMMIT"], organization: organization&.as_json(only: %i[name subdomain favicon_url]), authenticated: current_user.present?, app_name: build_app_name } user_props = if current_user.present? { user: current_user.as_json.merge( authentication_token: current_user.authentication_token, date_format: current_user.date_format ? current_user.date_format_before_type_cast.upcase : "DD/MM/YYYY" ), neeto_apps: user_data(:neeto_apps), is_owner: user_data(:is_owner), permissions: user_permissions } else {} end global_props.merge(user_props) end private def user_permissions current_user_role = current_user.organization_role return [] if current_user_role.nil? Rails.cache.fetch("#{organization.cache_key}/#{current_user_role.cache_key_with_version}/permissions") do current_user_role.permissions.pluck(:name) || [] end end def user_data(entity) @neeto_data.with_indifferent_access[entity] end def neeto_data if Rails.env.test? || Rails.env.heroku? || Rails.env.development? { neeto_apps: [], is_owner: true } else Organizations::NeetoAppsService.new(current_user).process || { neeto_apps: [], is_owner: false } end end def build_app_name client_app_name = Rails.application.secrets.client_app_name unless client_app_name raise "client_app_name not found. Please add client_app_name field in secrets.yml file." end "neeto" + client_app_name end end end end