diff --git a/app/jobs/spree_cm_commissioner/order_complete_bib_generator_job.rb b/app/jobs/spree_cm_commissioner/order_complete_bib_generator_job.rb new file mode 100644 index 000000000..72e11e34f --- /dev/null +++ b/app/jobs/spree_cm_commissioner/order_complete_bib_generator_job.rb @@ -0,0 +1,8 @@ +module SpreeCmCommissioner + class OrderCompleteBibGeneratorJob < ApplicationJob + def perform(order_id) + order = ::Spree::Order.find(order_id) + order.generate_bib_number! + end + end +end diff --git a/app/models/concerns/spree_cm_commissioner/order_bib_number_concern.rb b/app/models/concerns/spree_cm_commissioner/order_bib_number_concern.rb index 8b01f76d3..1b2c8949e 100644 --- a/app/models/concerns/spree_cm_commissioner/order_bib_number_concern.rb +++ b/app/models/concerns/spree_cm_commissioner/order_bib_number_concern.rb @@ -3,14 +3,29 @@ module OrderBibNumberConcern extend ActiveSupport::Concern included do - after_update :generate_bib_number, if: :state_changed_to_complete? + after_update :generate_guests_and_bib_numbers, if: :state_changed_to_complete? - def generate_bib_number + def generate_bib_number! + transaction do + line_items.with_bib_prefix.find_each do |line_item| + line_item.guests.none_bib.find_each(&:generate_bib!) + end + end + end + + private + + def generate_guests_and_bib_numbers + generate_remaining_guests + generate_bib_number_aysnc + end + + def generate_remaining_guests line_items.find_each(&:generate_remaining_guests) + end - line_items.with_bib_prefix.find_each do |line_item| - line_item.guests.none_bib.find_each(&:generate_bib!) - end + def generate_bib_number_aysnc + SpreeCmCommissioner::OrderCompleteBibGeneratorJob.perform_later(id) end end end diff --git a/spec/jobs/spree_cm_commissioner/order_complete_bib_generator_job_spec.rb b/spec/jobs/spree_cm_commissioner/order_complete_bib_generator_job_spec.rb new file mode 100644 index 000000000..c284833d7 --- /dev/null +++ b/spec/jobs/spree_cm_commissioner/order_complete_bib_generator_job_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +RSpec.describe SpreeCmCommissioner::OrderCompleteBibGeneratorJob, type: :job do + let(:order) { create(:order) } + let(:order_id) { order.id } + + describe '#perform' do + it 'finds the order by id' do + expect(Spree::Order).to receive(:find).with(order_id).and_return(order) + subject.perform(order_id) + end + + it 'generates guest BIB for the order' do + allow(Spree::Order).to receive(:find).with(order_id).and_return(order) + + expect(order).to receive(:generate_bib_number!) + subject.perform(order_id) + end + end +end diff --git a/spec/models/concerns/spree_cm_commissioner/order_bib_number_concern_spec.rb b/spec/models/concerns/spree_cm_commissioner/order_bib_number_concern_spec.rb new file mode 100644 index 000000000..167a591c4 --- /dev/null +++ b/spec/models/concerns/spree_cm_commissioner/order_bib_number_concern_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +RSpec.describe SpreeCmCommissioner::OrderBibNumberConcern do + let(:taxonomy) { create(:taxonomy, kind: :event) } + let(:event1) { create(:taxon, name: 'BunPhum', taxonomy: taxonomy) } + let(:product_with_bib1) { create(:cm_bib_number_product, product_type: :ecommerce, taxons: [event1]) } + let(:order) { create(:order, completed_at: nil) } + + let(:line_item) { create(:line_item, order: order, variant: product_with_bib1.variants.first) } + let(:line_items) { [line_item] } + let(:guest) { create(:guest, line_item: line_item, seat_number: 1) } + let(:guests) { [guest] } + + describe 'callbacks' do + it 'calls generate_guests_and_bib_numbers after update if state is complete' do + expect(order).to receive(:generate_guests_and_bib_numbers) + order.update(state: 'complete') + end + end + + describe '#generate_guests_and_bib_numbers' do + it 'calls generate_remaining_guests' do + expect(order).to receive(:generate_remaining_guests) + order.send(:generate_guests_and_bib_numbers) + end + + it 'calls generate_bib_number_aysnc' do + expect(order).to receive(:generate_bib_number_aysnc) + order.send(:generate_guests_and_bib_numbers) + end + end + + describe "#generate_remaining_guests" do + it 'calls generate_remaining_guests for each line item' do + allow(order).to receive(:line_items).and_return(line_items) + allow(line_items).to receive(:find_each).and_yield(line_item) + + expect(line_item).to receive(:generate_remaining_guests).once + order.send(:generate_remaining_guests) + end + end + + describe "#generate_bib_number_aysnc" do + it "enque a job with SpreeCmCommissioner::OrderCompleteBibGeneratorJob" do + expect { + order.send(:generate_bib_number_aysnc) + }.to have_enqueued_job(SpreeCmCommissioner::OrderCompleteBibGeneratorJob).with(order.id) + end + end + + describe "#generate_bib_number!" do + it 'calls generate_bib! for each guest with none_bib' do + allow(order).to receive(:line_items).and_return(line_items) + allow(line_items).to receive(:with_bib_prefix).and_return(line_items) + allow(line_items).to receive(:find_each).and_yield(line_item) + allow(line_item).to receive_message_chain(:guests, :none_bib).and_return(guests) + allow(guests).to receive(:find_each).and_yield(guest) + + expect(guest).to receive(:generate_bib!).once + order.generate_bib_number! + end + end +end