class Eco::API::UseCases::GraphQL::Samples::Location # Logic to: Create tags remap csv table batch design module Command::TrackChangedIds include Eco::API::UseCases::GraphQL::Helpers::Base::CaseEnv attr_accessor :tags_remap_csv_file REMAP_LOC_IDS_FOLDER = 'cache'.freeze REMAP_LOC_IDS_FILENAME = 'remap_loc_ids.csv'.freeze def tags_remap_csv_full_filename folder = self.class::REMAP_LOC_IDS_FOLDER filename = self.class::REMAP_LOC_IDS_FILENAME File.join(folder, filename) end def tags_remap_class Eco::API::UseCases::GraphQL::Helpers::Location::TagsRemap end # The maps of tags to be used in batch remap tags # @return [Array] source/destination pairs of `Array` def tags_remap_table @tags_remap_table ||= tags_remap_class.new end # Generates the file # @note this method used to only work if we could run cummulative dry-runs to the back-end. # However, after RS P3, as mappings are one-to-one (not many-to-many per row), # we can just display the mappings in dry-run as well. def close_handling_tags_remap_csv if tags_remap_table.any? puts "REMAP LOC IDs CSV (content):" puts tags_remap_table true else log(:info) { "Remap location ids NOT needed :)" } false end end # Based on commands that succeded, and the batch stage, it tracks # the tag remaps that should be batches against existing pages # @note The only update operation that generate tag remaps # is `:id` (or `:id_name`). # @return [Boolean] whether new maps were inserted to the tracking table def update_tags_remap_table(results, stage, ref_tree = nil) return false unless %i[id id_name].include?(stage) msg = "Expecting CommandResults object. Given: #{results.class}" raise msg unless results.is_a?(request_results_class) target = simulate?? results.results : results.applied target.each do |result| prev_id, new_id = result.command_input_data.values_at(:nodeId, :newId) next if new_id.nil? # not an id change next if prev_id == new_id tags_remap_table << [[prev_id], [new_id]] # next unless ref_tree.is_a?(Eco::API::Organization::TagTree) # next unless ref_tree.tag?(new_id) # msg = "Node '#{prev_id}' was updated to '#{new_id}', " # msg << "but in current structure '#{new_id}' is not present" # log(:warn) { msg } end end # Generates the final tags remap file def generate_tags_remap_csv(filename = tags_remap_csv_full_filename) return nil if tags_remap_table.empty? timestamp_file(filename).tap do |file| CSV.open(file, 'w') do |csv| csv << %w[prev_node_ids new_node_ids] tags_remap_table.each do |tags_remap| csv << tags_remap.to_csv_row end end log(:info) { "Generated file '#{file}'" } end end # Makes the file relative to the enviro def timestamp_file(filename, enviro_relative: true) filename = session.file_manager.dir.file(filename) if enviro_relative Eco::Data::Files.timestamp_file(filename) end end end