Sha256: 4baf1d09bc5705705a113b204ddc655a4f6ec2a267162a75f13bf802fc958870

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

class Eco::API::UseCases::GraphQL::Helpers::Location::Command::Diff
  module AsUpdate
    # Unique access point to generate an update
    def as_update(operation, after_id_update: false)
      update_hash = diff_hash(after_id_update: after_id_update)
      update_hash.slice(*update_attrs(operation))
    end

    # Adjustments to be able to generate correct updates
    # @note it transforms the method on a very effective helper
    def diff_hash(after_id_update: true) # rubocop:disable Metrics/AbcSize
      super().tap do |h|
        h.delete('archived')
        h['classificationIds'] = h.delete('classifications') if h.key?('classifications')

        if archive? || insert?
          # We assume archives do not have `move` nor update `id` or `name`
          h['nodeId']   = node_id || node_id_prev
          h['parentId'] = parent_id if insert?
        elsif update?
          if id? && !after_id_update
            h['nodeId'] = node_id_prev
            h['newId']  = node_id
          elsif id? # after id update
            h['nodeId'] = node_id
          else # no id? change , and before the id_update stage
            h['nodeId'] = node_id_prev
          end
          if move?
            h['parentId'] = after_id_update ? parent_id : parent_id_prev
          end
        else
          # Something is wrong (not expected to be any other category)
          raise "Unexpected condition, please review code base"
        end
      end
    end

    # List of attributes that can be part of an operation
    def update_attrs(operation)
      ['nodeId'].tap do |attrs|
        plus =
          case operation
          when :id_name, :update
            %w[newId name classificationIds]
          when :id
            %w[newId]
          when :insert
            %w[name parentId classificationIds]
          when :move
            %w[parentId]
          else
            []
          end
        attrs.push(*plus)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eco-helpers-2.7.4 lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb
eco-helpers-2.7.2 lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb
eco-helpers-2.7.1 lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb
eco-helpers-2.7.0 lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb