Module: Eco::API::UseCases::GraphQL::Helpers::Location::Command::Diff::AsUpdate

Included in:
Eco::API::UseCases::GraphQL::Helpers::Location::Command::Diff
Defined in:
lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb

Instance Method Summary collapse

Instance Method Details

#as_update(operation, after_id_update: false) ⇒ Object

Unique access point to generate an update



4
5
6
7
# File 'lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb', line 4

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

#diff_hash(after_id_update: true) ⇒ Object

Note:

it transforms the method on a very effective helper

Adjustments to be able to generate correct updates



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb', line 11

def diff_hash(after_id_update: true) # rubocop:disable Metrics/AbcSize
  super().tap do |h|
    h.delete('archived')

    if h.key?('classifications')
      class_ids = h.delete('classifications')
      class_ids = [class_ids].flatten.compact unless class_ids.is_a?(Array)
      h['classificationIds'] = class_ids
    end

    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
      return h if marked_for_unarchive?

      # Something is wrong (not expected to be any other category)
      raise "Please review code base, unexpected condition for #{h.pretty_inspect}"
    end
  end
end

#update_attrs(operation) ⇒ Object

List of attributes that can be part of an operation



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb', line 47

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