Sha256: 4cc0eb23e91419b01fa471252c65aaf2dddef7b513339c8687495538ca4d966c

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    class DelegationsCsvImporter < CsvImporter
      def process(row, params, details_csv, import_summary, iterator)
        if delegation_exists?(params)
          message = generate_info_message(row)

          handle_skipped_row(row, details_csv, import_summary, iterator, message)

          false
        else
          true
        end
      end

      private

      def extract_params(row)
        params = {
          granter_email: row["from"].to_s.strip.downcase,
          grantee_email: row["to"].to_s.strip.downcase
        }

        @form = form(Decidim::ActionDelegator::Admin::DelegationForm).from_params(params, setting: @current_setting)

        {
          granter_id: @form.granter&.id,
          grantee_id: @form.grantee&.id
        }
      end

      def delegation_exists?(params)
        @delegation = Delegation.find_by(granter_id: params[:granter_id], grantee_id: params[:grantee_id])

        @delegation.present?
      end

      def process_delegation(form)
        Decidim::ActionDelegator::Admin::CreateDelegation.call(form, @current_user, @current_setting)
      end

      def handle_form_validity(row, details_csv, import_summary, row_number)
        if @form.valid?
          process_delegation(@form)
          import_summary[:imported_rows] += 1
        else
          handle_import_error(row, details_csv, import_summary, row_number, @form.errors.full_messages.join(", "))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-action_delegator-0.8.1 app/services/decidim/action_delegator/delegations_csv_importer.rb
decidim-action_delegator-0.7.2 app/services/decidim/action_delegator/delegations_csv_importer.rb
decidim-action_delegator-0.7.1 app/services/decidim/action_delegator/delegations_csv_importer.rb
decidim-action_delegator-0.7.0 app/services/decidim/action_delegator/delegations_csv_importer.rb