Sha256: a04e62f5af7a2db5f56d87dca0395818f1075b4a9cc0136a1661da2fef8b657e

Contents?: true

Size: 1.62 KB

Versions: 21

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Osso
  module GraphQL
    module Mutations
      class BaseMutation < ::GraphQL::Schema::RelayClassicMutation
        object_class Types::BaseObject
        input_object_class Types::BaseInputObject

        def response_data(data)
          data.merge(errors: [])
        end

        def response_error(errors)
          raise ::GraphQL::ExecutionError.new(
            'Mutation error',
            extensions: {
              'errors' => field_errors(errors),
            },
          )
        end

        def field_errors(errors)
          errors.map do |attribute, messages|
            attribute = attribute.to_s.camelize(:lower)
            {
              attribute: attribute,
              message: messages,
            }
          end
        end

        def ready?(**args)
          return true if internal_ready?

          return true if domain_ready?(args[:domain] || domain(**args))

          raise ::GraphQL::ExecutionError, 'This user lacks the permission to make the requested changes'
        end

        def admin_ready?
          context[:scope] == 'admin'
        end

        def internal_ready?
          return true if admin_ready?

          context[:scope] == 'internal'
        end

        def domain_ready?(domain)
          context[:email].split('@')[1] == domain
        end

        def account_domain(id)
          return false unless id

          Osso::Models::EnterpriseAccount.find(id)&.domain
        end

        def provider_domain(id)
          return false unless id

          Osso::Models::IdentityProvider.find(id)&.domain
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
osso-0.1.2 lib/osso/graphql/mutations/base_mutation.rb
osso-0.1.1 lib/osso/graphql/mutations/base_mutation.rb
osso-0.1.0 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.11 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.10 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.8 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.7 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.6 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.6.alpha lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.lambda lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.kappa lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.iota lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.theta lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.eta lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.zeta lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.epsilon lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.delta lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.gamma lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.5.pre.beta lib/osso/graphql/mutations/base_mutation.rb