Class: SpreeCmCommissioner::Integrations::VireakBuntham::Polling::SyncProduct

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_product.rb

Overview

SyncProduct — one Spree::Product per VET schedule.

Mirrors Larryta::Polling::SyncProduct. External id: “product-vendor_id-schedule_id”. Product name: “Phnom Penh (Branch) → Siem Reap (VIP) (06:00:00)”.

Instance Method Summary collapse

Constructor Details

#initialize(integration:, sync_result:) ⇒ SyncProduct

Returns a new instance of SyncProduct.



9
10
11
12
13
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_product.rb', line 9

def initialize(integration:, sync_result:)
  @integration = integration
  @sync_result = sync_result
  @vendor = integration.vendor
end

Instance Method Details

#call(schedule:, price:, on_date:) ⇒ Spree::Product?

Parameters:

  • schedule (Resources::Schedule)
  • price (Numeric)

    effective local price (USD) per PRICING_COMPLETE.md

  • on_date (String)

    YYYY-MM-DD sync date

Returns:

  • (Spree::Product, nil)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_product.rb', line 19

def call(schedule:, price:, on_date:)
  return nil if schedule.id.blank?

  external_id = "product-#{@vendor.id}-#{schedule.id}-#{on_date}"

  mapping = Spree::Product.find_or_initialize_integration_mapping(
    integration_id: @integration.id,
    external_id: external_id
  )

  product = mapping.internal

  @sync_result.track(:product, product) do |tracker|
    product.assign_attributes(
      name: build_name(schedule),
      short_name: build_short_name(schedule),
      status: :active,
      vendor: @vendor,
      product_type: :transit,
      available_on: Time.current,
      shipping_category: default_shipping_category,
      stores: [Spree::Store.default],
      option_types: [seat_type_option].compact,
      price: price
    )
    tracker.save_if_changed!(product)
  end

  mapping.mark_as_active!(external_payload: { schedule_id: schedule.id })
  product
end