Class: SpreeCmCommissioner::AssignProductToSection::Update

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/assign_product_to_section/update.rb

Instance Method Summary collapse

Constructor Details

#initialize(ticket:, section_id:, event_id:) ⇒ Update

Returns a new instance of Update.



6
7
8
9
10
# File 'app/services/spree_cm_commissioner/assign_product_to_section/update.rb', line 6

def initialize(ticket:, section_id:, event_id:)
  @ticket = ticket
  @section_id = section_id
  @event_id = event_id
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/spree_cm_commissioner/assign_product_to_section/update.rb', line 12

def call
  return if @section_id.blank?
  return if event.nil? || !event.children.exists?(@section_id)

  ApplicationRecord.transaction do
    # Remove classifications for the ticket from all other sections under the event.
    # This ensures the ticket belongs to only one section without unnecessary deletions.
    Spree::Classification.where(
      product_id: @ticket.id,
      taxon_id: event.children.ids
    ).where.not(taxon_id: @section_id).delete_all

    # Ensure the ticket is assigned to the specified section.
    Spree::Classification.find_or_create_by!(
      product_id: @ticket.id,
      taxon_id: @section_id
    )
  end

  success(@ticket)
rescue ActiveRecord::RecordInvalid => e
  failure(nil, e.message)
end