update bulk invoice job to check if the invoices feature is enabled for the current user

This commit is contained in:
Mohamed ABDELLANI
2023-08-07 14:55:50 +01:00
committed by Konrad
parent bd2a1b3e22
commit adecf64cf3
2 changed files with 13 additions and 3 deletions

View File

@@ -3,10 +3,13 @@
class BulkInvoiceJob < ApplicationJob
include CableReady::Broadcaster
delegate :render, to: ActionController::Base
attr_reader :options
def perform(order_ids, filepath, options = {})
@options = options
orders = sorted_orders(order_ids)
orders.filter!(&:invoiceable?) if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
orders.filter!(&:invoiceable?) if OpenFoodNetwork::FeatureToggle.enabled?(:invoices,
current_user)
orders.each(&method(:generate_invoice))
ensure_directory_exists filepath
@@ -29,7 +32,7 @@ class BulkInvoiceJob < ApplicationJob
end
def generate_invoice(order)
renderer_data = if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
renderer_data = if OpenFoodNetwork::FeatureToggle.enabled?(:invoices, current_user)
OrderInvoiceGenerator.new(order).generate_or_update_latest_invoice
order.invoices.first.presenter
else
@@ -58,4 +61,10 @@ class BulkInvoiceJob < ApplicationJob
def pdf
@pdf ||= CombinePDF.new
end
def current_user
return unless options[:current_user_id]
@current_user ||= Spree::User.find(options[:current_user_id])
end
end

View File

@@ -37,7 +37,8 @@ module Admin
BulkInvoiceJob.perform_later(
params[:bulk_ids],
"tmp/invoices/#{Time.zone.now.to_i}-#{SecureRandom.hex(2)}.pdf",
channel: SessionChannel.for_request(request)
channel: SessionChannel.for_request(request),
current_user_id: current_user.id
)
morph :nothing