Sha256: aa9df606df84a252e7aae65ef5190eb96bc939d9971845270ba28111f668492c

Contents?: true

Size: 1.62 KB

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
osso-0.0.4 lib/osso/graphql/mutations/base_mutation.rb
osso-0.0.3.27 lib/osso/graphql/mutations/base_mutation.rb