Skip to content

Commit

Permalink
Add route, controller action and form
Browse files Browse the repository at this point in the history
for promo codes csv import.
  • Loading branch information
tomdonarski committed Nov 27, 2023
1 parent aa76170 commit 88df8cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/controllers/spree/admin/promotion_batches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def csv_export
disposition: :attachment,
type: "text/csv"
end

def csv_import
content = params[:file]&.read.to_s
Spree::PromotionBatches::PromoCodesImporter.new(content: content, promotion_batch_id: params[:id]).call
redirect_back fallback_location: admin_promotions_path, notice: "Promo codes uploaded"
rescue Spree::PromotionBatches::PromoCodesImporter::Error => e
redirect_back fallback_location: admin_promotions_path, alert: e.message
end
end
end
end
5 changes: 5 additions & 0 deletions app/views/spree/admin/promotion_batches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<% end %>
<% end %>

<%= form_tag csv_import_admin_promotion_batch_path, method: :post, multipart: true do %>
<%= file_field_tag :file, accept: ".csv" %>
<%= submit_tag 'Import codes from CSV' %>
<% end %>

<table class="table">
<thead class="text-muted">
<tr>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
resources :promotion_batches do
member do
get :csv_export, to: 'promotion_batches#csv_export'
post :csv_import, to: 'promotion_batches#csv_import'
end
end

Expand Down

0 comments on commit 88df8cd

Please sign in to comment.