Skip to content

Commit

Permalink
bulk upload store credit button added
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshumishra31 committed Aug 6, 2018
1 parent 5f68c82 commit a01fd7e
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 2 deletions.
28 changes: 28 additions & 0 deletions app/assets/javascripts/spree/backend/csv_input_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function CsvInputButton(input) {
this.fileInput = input.fileInput;
this.submitButton = input.submitButton;
this.form = input.form;
}
CsvInputButton.prototype.init = function() {
var _this = this;
this.submitButton.on('click', function() {
event.preventDefault();
_this.fileInput.click();
_this.submitForm();
});
};
CsvInputButton.prototype.submitForm = function() {
var _this = this;
this.fileInput.on('change', function() {
_this.form.submit();
});
};
$(function() {
var inputs = {
fileInput: $('input#file'),
submitButton: $('input[data-disable-with="Import Store Credit CSV"]'),
form: $("form[data-hook='csv_form']")
},
csvButtonsManager = new CsvInputButton(inputs);
csvButtonsManager.init();
});
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Placeholder manifest file.
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js'
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js'
//= require spree/backend/csv_input_button
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
Placeholder manifest file.
the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css'
*/
input.file-browse {
display: none;
}

a.sample-download {
padding-right: 13px;
}
22 changes: 22 additions & 0 deletions app/controllers/spree/admin/users_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Spree::Admin::UsersController.class_eval do
def sample_store_credit_csv
send_file STORE_CREDIT_CSV_FILE[:sample_store_credit_file]
end

def import_store_credits
begin
create_store_credit_updater
redirect_to admin_users_path, notice: Spree.t(:email_sent, filename: @store_credit_updater.data_file_file_name)
rescue
redirect_to admin_users_path, notice: "Invalid CSV file format."
end
end

private

def create_store_credit_updater
@store_credit_updater = Spree::BulkStoreCreditUpdater.create(data_file: params[:file])
NotifyStoreCreditService.delay(run_at: 1.minutes.from_now).new(@store_credit_updater.id, try_spree_current_user.email)
end

end
11 changes: 11 additions & 0 deletions app/mailers/spree/store_credit_updater_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Spree
class StoreCreditUpdaterMailer < BaseMailer
def update_admin(status_csv, admin_email, filename, total_records, successfull_records)
@total_records = total_records
@failed_records = total_records - successfull_records
attachments['stock_items.csv'] = status_csv
subject = "#{Spree::Store.current.name} import of #{ filename } has finished"
mail(to: admin_email, from: from_address, subject: subject)
end
end
end
6 changes: 6 additions & 0 deletions app/models/spree/bulk_store_credit_updater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Spree
class BulkStoreCreditUpdater < Spree::Base
has_attached_file :data_file
validates_attachment :data_file, content_type: { content_type: ["text/csv", "text/plain"] }
end
end
6 changes: 6 additions & 0 deletions app/overrides/add_import_button_on_user_index_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Deface::Override.new(
virtual_path: 'spree/admin/shared/_content_header',
name: 'add_import_button_on_user_index_page',
insert_bottom: "div[data-hook='toolbar']",
partial: 'spree/admin/users/import_button'
)
66 changes: 66 additions & 0 deletions app/services/notify_store_credit_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'csv'
class NotifyStoreCreditService
CSV_HEADERS = { 'Email': :Email, 'Store_Credit': :Store_Credit, 'Memo': :Memo, 'Category_ID': :Category_ID, 'Status': :Status }

def initialize(store_credit_updater_id, admin_email)
@store_credit_updater = Spree::BulkStoreCreditUpdater.find_by(id: store_credit_updater_id)
update_store_credits
@store_credit_updater.update_column(:job_executed, true)
Spree::StoreCreditUpdaterMailer.update_admin(@csv_export, admin_email, @store_credit_updater.data_file_file_name, @total_records, @successfull_records).deliver_now
@store_credit_updater.destroy if @store_credit_updater.job_executed?
end

private
def update_store_credits
@total_records = 0
@successfull_records = 0
@csv_export = CSV.generate do |csv|
unless csv_empty?
csv << CSV_HEADERS.keys
CSV.foreach(@store_credit_updater.data_file.path, headers: true) do |row|
@error = nil
@total_records += 1
@row = row
@user = find_user
if @user
update_stocks_with_csv_values
else
@error = Spree.t(:user_not_found)
end
csv << set_row
end
end
end
end

def csv_empty?
CSV.readlines(@store_credit_updater.data_file.path) == [[]]
end

def set_row
row = []
CSV_HEADERS.each do |key, value|
row << create_csv_row(key, value)
end
row
end

def create_csv_row(key, value)
( key == :Status ) ? error_message : @row[value.to_s]
end

def error_message
@error ? @error : 'Successfully Updated'
end

def update_stocks_with_csv_values
admin = Spree::User.admin.first
store_credit = Spree::StoreCredit.new(amount: @row['Store_Credit'].to_f, created_by_id: admin.id, currency: "USD", category_id: @row['Category_ID'], memo: @row['Memo'])
@user.store_credits << store_credit
@successfull_records += 1
end

def find_user
Spree::User.find_by_email(@row['Email'])
end
end
7 changes: 7 additions & 0 deletions app/views/spree/admin/users/_import_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if request.path == admin_users_path %>
<%= form_tag import_store_credits_admin_users_path, multipart: true, class: 'form-inline', data: { hook: "csv_form" } do %>
<%= file_field_tag :file, class: 'file-browse' %>
<%= submit_tag Spree.t(:import), class: 'btn-success btn' %>
<%= link_to Spree.t(:example) , sample_store_credit_csv_admin_users_path, class: 'sample-download' %>
<% end %>
<% end %>
33 changes: 33 additions & 0 deletions app/views/spree/shared/_base_mailer_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!-- You can override this template to design your own header. -->
<table class="row header">
<tr>
<td class="center" align="center">
<center>

<table class="container">
<tr>
<td class="wrapper last">

<table class="twelve columns">
<tr>
<td class="twelve sub-columns last">
<% if frontend_available? %>
<%= link_to root_url, class: 'template-label' do %>
<%= image_tag Spree::Config.logo, class: 'logo', alt: Spree::Store.current.name, title: Spree::Store.current.name %>
<% end %>
<% else %>
<%= image_tag Spree::Config.logo, class: 'logo', alt: Spree::Store.current.name, title: Spree::Store.current.name %>
<% end %>
</td>
<td class="expander"></td>
</tr>
</table>

</td>
</tr>
</table>

</center>
</td>
</tr>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<h5><%= Spree.t(:update_status) %></h5>
<ul>
<li>Number of records imported : <%= @total_records %> </li>
<li>Number of records failed : <%= @failed_records %> </li>
</ul>
<strong>Attached is a csv file of failed records for your information.</strong>
</div>
3 changes: 3 additions & 0 deletions config/initializers/spree_bulk_store_credits_constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
STORE_CREDIT_CSV_FILE = {
sample_store_credit_file: SpreeBulkStoreCredits::Engine.root.join('lib', 'sample_csv', 'bulk_store_credits_example.csv')
}
6 changes: 5 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
hello: "Hello world"
spree:
import: Import Store Credit CSV
example: Download Sample File
email_sent: "%{filename} has been successfully imported. Details will be sent shortly to you over an email."
user_not_found: User with this email Not found
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Spree::Core::Engine.add_routes do
# Add your extension routes here
namespace :admin, path: Spree.admin_path do
resources :users do
collection { post :import_store_credits }
collection { get :sample_store_credit_csv }
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateSpreeBulkStoreCreditUpdater < ActiveRecord::Migration[4.2]
def change
create_table :spree_bulk_store_credit_updaters do |t|
t.boolean :job_executed, default: false
t.attachment :data_file
end
end
end
2 changes: 2 additions & 0 deletions lib/sample_csv/bulk_store_credits_example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Email,Store Credit,Memo,Category ID
[email protected],10,Order benefit,1

0 comments on commit a01fd7e

Please sign in to comment.