testmycode/tmc-server

View on GitHub
app/controllers/api/v8/organizations_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module Api
  module V8
    class OrganizationsController < Api::V8::BaseController
      include Swagger::Blocks

      swagger_path '/api/v8/org.json' do
        operation :get do
          key :description, 'Returns a list of all organizations'
          key :operationId, 'findOrganizations'
          key :produces, ['application/json']
          key :tags, ['organization']
          response 403, '$ref': '#/responses/error'
          response 404, '$ref': '#/responses/error'
          response 200 do
            key :description, 'List of organizations in json'
            schema do
              key :type, :array
              items do
                key :'$ref', :Organization
              end
            end
          end
        end
      end

      swagger_path '/api/v8/org/{organization_slug}.json' do
        operation :get do
          key :description, 'Returns a json representation of the organization'
          key :operationId, 'findOrganization'
          key :produces, ['application/json']
          key :tags, ['organization']
          parameter '$ref': '#/parameters/path_organization_slug'
          response 403, '$ref': '#/responses/error'
          response 404, '$ref': '#/responses/error'
          response 200 do
            key :description, 'Organization in json'
            schema do
              key :title, :organization
              key :required, [:organization]
              property :organization, '$ref': :Organization
            end
          end
        end
      end

      def index
        orgs = Organization
               .visible_organizations
               .select { |org| org.visibility_allowed?(request, current_user) }

        orgs = Organization.all if current_user.administrator?
        orgs = orgs.map { |o| o.attributes.merge(logo_path: o.logo_path) } if current_user.administrator?

        unless current_user.administrator?
          orgs = orgs.map { |o| { name: o.name, information: o.information, slug: o.slug, logo_path: o.logo_path, pinned: o.pinned } }
        end
        authorize! :read, orgs
        present(orgs)
      end

      def show
        org = Organization.find_by!(slug: params[:slug])
        authorize! :read, org
        present org.org_as_json
      end
    end
  end
end