Skip to content

Commit

Permalink
Close #2180: enqueue generate BIB when order completed
Browse files Browse the repository at this point in the history
  • Loading branch information
hengsokly committed Dec 25, 2024
1 parent 15a4239 commit ba598e9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ba598e9

Please sign in to comment.