diff --git a/app/controllers/spree/admin/users_controller_decorator.rb b/app/controllers/spree/admin/users_controller_decorator.rb index e1c32f8..352c1b2 100644 --- a/app/controllers/spree/admin/users_controller_decorator.rb +++ b/app/controllers/spree/admin/users_controller_decorator.rb @@ -8,7 +8,7 @@ def import_store_credits 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." + redirect_to admin_users_path, notice: Spree.t(:invalid_format) end end diff --git a/app/mailers/spree/store_credit_updater_mailer.rb b/app/mailers/spree/store_credit_updater_mailer.rb index 5dee1f8..d72e0b1 100644 --- a/app/mailers/spree/store_credit_updater_mailer.rb +++ b/app/mailers/spree/store_credit_updater_mailer.rb @@ -5,7 +5,7 @@ def update_admin(status_csv, admin_email, filename, total_records, successfull_r @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) + mail(to: 'himanshu.mishra@vinsol.com', from: from_address, subject: subject) end end end diff --git a/app/services/notify_store_credit_service.rb b/app/services/notify_store_credit_service.rb index a8f57c5..fb5bbce 100644 --- a/app/services/notify_store_credit_service.rb +++ b/app/services/notify_store_credit_service.rb @@ -1,6 +1,6 @@ require 'csv' class NotifyStoreCreditService - CSV_HEADERS = { 'Email': :Email, 'Store_Credit': :Store_Credit, 'Memo': :Memo, 'Category_ID': :Category_ID, 'Status': :Status } + CSV_HEADERS = { 'Email': :Email, 'Store_Credit': :Store_Credit, 'Memo': :Memo, 'Status': :Status } def initialize(store_credit_updater_id, admin_email) @store_credit_updater = Spree::BulkStoreCreditUpdater.find_by(id: store_credit_updater_id) @@ -22,10 +22,10 @@ def update_store_credits @total_records += 1 @row = row @user = find_user - if @user - update_stocks_with_csv_values + if @user && @row['Store_Credit'].scan(/\D/).empty? + update_store_credits_with_csv_values else - @error = Spree.t(:user_not_found) + @error = @user ? Spree.t(:store_credit_not_integer) : Spree.t(:user_not_found) end csv << set_row end @@ -50,13 +50,15 @@ def create_csv_row(key, value) end def error_message - @error ? @error : 'Successfully Updated' + @error ? @error : Spree.t(:successfull_update) end - def update_stocks_with_csv_values + def update_store_credits_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']) + category = Spree::StoreCreditCategory.first + store_credit = Spree::StoreCredit.new(amount: @row['Store_Credit'].to_f, created_by_id: admin.id, currency: "USD", category_id: category.try(:id), memo: @row['Memo']) @user.store_credits << store_credit + @error = store_credit.valid? ? nil : store_credit.errors.full_messages.join(',') @successfull_records += 1 end diff --git a/config/locales/en.yml b/config/locales/en.yml index 4cd47bb..7cd6bb5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,3 +7,6 @@ en: 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 + store_credit_not_integer: Amount not integer + invalid_format: Invalid CSV file format. + successfull_update: Successfully Updated diff --git a/db/migrate/20180806080802_create_spree_bulk_store_credit_updater.rb b/db/migrate/20180806080802_create_spree_bulk_store_credit_updater.rb index dfd6f37..c953501 100644 --- a/db/migrate/20180806080802_create_spree_bulk_store_credit_updater.rb +++ b/db/migrate/20180806080802_create_spree_bulk_store_credit_updater.rb @@ -1,7 +1,7 @@ class CreateSpreeBulkStoreCreditUpdater < ActiveRecord::Migration[4.2] def change create_table :spree_bulk_store_credit_updaters do |t| - t.boolean :job_executed, default: false + t.boolean :job_executed, default: false, null: false t.attachment :data_file end end diff --git a/lib/sample_csv/bulk_store_credits_example.csv b/lib/sample_csv/bulk_store_credits_example.csv index c2b4dd3..2d0f3f5 100644 --- a/lib/sample_csv/bulk_store_credits_example.csv +++ b/lib/sample_csv/bulk_store_credits_example.csv @@ -1,2 +1,2 @@ -Email,Store_Credit,Memo,Category_ID -spree@example.com,10,Order benefit,1 +Email,Store_Credit,Memo +spree@example.com,10,Order benefit