Class: SpreeCmCommissioner::InventoryItem

Inherits:
Base
  • Object
show all
Includes:
ProductType
Defined in:
app/models/spree_cm_commissioner/inventory_item.rb

Constant Summary collapse

MAX_DISPLAY_STOCK =
20

Constants included from ProductType

ProductType::PERMANENT_STOCK_PRODUCT_TYPES, ProductType::PRE_INVENTORY_DAYS, ProductType::PRODUCT_TYPES

Instance Method Summary collapse

Methods included from ProductType

#permanent_stock?, #pre_inventory_days

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 80

def active?
  inventory_date.nil? || inventory_date >= Time.zone.today
end

#adjust_quantity!(quantity) ⇒ Object

This method is only used when admin update stock



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 34

def adjust_quantity!(quantity)
  with_lock do
    # IMPORTANT: Apply quantity changes directly without defensive clamping.
    # The model validation will catch any attempts to go negative, surfacing bugs
    # in upstream logic rather than silently losing data.
    #
    # ❌ DO NOT use defensive clamping like:
    #   self.max_capacity = [max_capacity + quantity, 0].max
    #
    # Why? Clamping masks bugs. Validation errors are better than silent data loss.
    # See: /docs/lessons-learned/inventory-consistency-issues.md#lesson-learned-async-job-validation-strategy
    self.max_capacity = max_capacity + quantity
    self.quantity_available = quantity_available + quantity
    save!

    # When user has been searched or booked a product, it has cached the quantity in redis,
    # So we need to update redis cache if inventory key has been created in redis
    adjust_quantity_in_redis(quantity)
  end
end

#adjust_quantity_in_redis(quantity) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 59

def adjust_quantity_in_redis(quantity)
  SpreeCmCommissioner.redis_pool.with do |redis|
    # Always update Redis cache, even if it doesn't exist yet.
    # This prevents admin adjustments from being lost when cache is later initialized.
    script = "      local key = KEYS[1]\n      local increment = tonumber(ARGV[1])\n      local expiry = tonumber(ARGV[2])\n      local current = tonumber(redis.call('GET', key) or 0)\n      local new_value = current + increment\n      if new_value < 0 then\n        new_value = 0\n      end\n      redis.call('SET', key, new_value, 'EX', expiry)\n      return new_value\n    LUA\n\n    redis.eval(script, keys: [redis_key], argv: [quantity, redis_expired_in])\n  end\nend\n"

#price_in(currency) ⇒ Object



29
30
31
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 29

def price_in(currency)
  prices.detect { |price| price.currency == currency } || prices.build(currency: currency)
end

#public_quantity_availableObject



25
26
27
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 25

def public_quantity_available
  [quantity_available, MAX_DISPLAY_STOCK].min
end

#redis_expired_inObject



84
85
86
87
88
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 84

def redis_expired_in
  expired_in = 31_536_000 # 1 year for normal stock
  expired_in = Time.parse(inventory_date.to_s).end_of_day.to_i - Time.zone.now.to_i if inventory_date.present?
  [expired_in, 0].max
end

#redis_keyObject



55
56
57
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 55

def redis_key
  "inventory:#{id}"
end