I want to import promotion codes from csv to the db for which I have created a worker the worker is being dispatched as
def create
CouponsImportWorker.perform_async(params)
@status = Spree.t('coupons_import.in_progress')
flash[:notice] = "CSV processing is in progress. You will be notified upon completion."
redirect_back fallback_location: spree.admin_promotions_path
end
below is the worker
require 'csv'
require 'json'
class CouponsImportWorker
include Sidekiq::Worker
def perform(params)
Rails.logger.info("Worker is running #{params[:coupons_import]}")
# begin
products = []
params[:coupons_import][:product].split(',').each do |product_id|
products << Spree::Product.find(product_id)
end
# Rails.logger.info("Worker is running after finding products ")
filename = params[:coupons_import][:file].path
Rails.logger.info(filename)
keys = File.readlines(filename)
csv_table = CSV.parse(File.read(filename), headers: true, col_sep: ';')
codes = csv_table.map(&:to_h).map { |e| e['code'] }
Rails.logger.info("Worker is running after finding codes ")
percent = params[:coupons_import][:percent].to_i
amount = params[:coupons_import][:amount].to_d
Rails.logger.info("just before checking if percent and amount are more than 0")
if percent > 0 || amount > 0
codes.each do |code|
promotion = Spree::Promotion.create(
name: params[:coupons_import][:promo_name],
description: params[:coupons_import][:promo_desc],
match_policy: 'all',
usage_limit: params[:coupons_import][:usage_limit],
starts_at: params[:coupons_import][:starts_at],
expires_at: params[:coupons_import][:expires_at],
code: code,
store_ids: 1
)
Rails.logger.info("just before calculating flatrate")
if amount > 0
promotion.promotion_actions << Spree::Promotion::Actions::CreateAdjustment.create(
calculator: Spree::Calculator::FlatRate.new(preferred_amount: amount)
)
else
promotion.promotion_actions << Spree::Promotion::Actions::CreateItemAdjustments.create(
calculator: Spree::Calculator::PercentOnLineItem.new(preferred_percent: percent)
)
end
if params[:coupons_import][:option_values].blank?
create_promotion_rule(promotion, products)
else
create_promotion_option_value_rule(promotion, products, params[:coupons_import][:option_values])
end
end
end
Rails.logger.info("running fine until now.....")
ActionCable.server.broadcast('coupons_channel', message: 'CSV processing completed.')
# rescue StandardError => e
# Rails.logger.error("Error in CouponsImportWorker: #{e}")
# end
end
private
def create_promotion_rule(promotion, products)
rule = Spree::Promotion::Rules::Product.create(promotion: promotion)
rule.products << products
rule.save!
end
def create_promotion_option_value_rule(promotion, products, option_value)
Spree::Promotion::Rules::OptionValue.create(
promotion_id: promotion.id,
type: 'Spree::Promotion::Rules::OptionValue',
preferences: {
eligible_values: { products.last.id => [option_value] },
match_policy: 'any'
}
)
end
end
I have tried to run the code but everytime it returns
no implicit conversion of Symbol into Integer excluded from capture: DSN not set
I want to know whether the code is broken. I am beginner in rails. But it seems the error is related to params.Thank you!
here as you write,
CouponsImportWorker.perform_async(params)
sidekiq didn't take params(ActionController::Parameters) as a input because it is quite complicated for sidekiq.The arguments you pass to perform_async must be composed of simple JSON datatypes: string, integer, float, boolean, null(nil), array and hash. This means you must not use ruby symbols as arguments.
here is best practices for sidekiq