Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions promotions/app/models/solidus_promotions/benefit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,28 @@ def available_calculators
# @param dry_run [Boolean] whether to collect detailed eligibility information
# @return [Boolean] true when all applicable conditions are eligible
def eligible_by_applicable_conditions?(promotable, dry_run: false)
applicable_conditions = conditions.select do |condition|
condition.applicable?(promotable)
end

applicable_conditions.map do |applicable_condition|
eligible = applicable_condition.eligible?(promotable)

break [false] if !eligible && !dry_run

if dry_run
if applicable_condition.eligibility_errors.details[:base].first
code = applicable_condition.eligibility_errors.details[:base].first[:error_code]
message = applicable_condition.eligibility_errors.full_messages.first
conditions.filter_map do |condition|
condition.applicable?(promotable) && begin
eligible = condition.eligible?(promotable)

break [false] if !eligible && !dry_run

if dry_run
if condition.eligibility_errors.details[:base].first
code = condition.eligibility_errors.details[:base].first[:error_code]
message = condition.eligibility_errors.full_messages.first
end
promotion.eligibility_results.add(
item: promotable,
condition: condition,
success: eligible,
code: eligible ? nil : (code || :coupon_code_unknown_error),
message: eligible ? nil : (message || I18n.t(:coupon_code_unknown_error, scope: [:solidus_promotions, :eligibility_errors]))
)
end
promotion.eligibility_results.add(
item: promotable,
condition: applicable_condition,
success: eligible,
code: eligible ? nil : (code || :coupon_code_unknown_error),
message: eligible ? nil : (message || I18n.t(:coupon_code_unknown_error, scope: [:solidus_promotions, :eligibility_errors]))
)
end

eligible
eligible
end
end.all?
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ def call
private

def perform_order_benefits(lane_benefits, lane)
lane_benefits.select { |benefit| benefit.respond_to?(:perform) }.each do |benefit|
benefit.perform(order)
lane_benefits.filter_map do |benefit|
benefit.respond_to?(:perform) && benefit.perform(order)
end

automated_line_items = order.line_items.select(&:managed_by_order_benefit)
return if automated_line_items.empty?

ineligible_line_items = automated_line_items.select do |line_item|
line_item.managed_by_order_benefit.promotion.lane == lane && !line_item.managed_by_order_benefit.in?(lane_benefits)
end

ineligible_line_items.each do |line_item|
line_item.managed_by_order_benefit.remove_from(order)
order.line_items.filter_map do |line_item|
line_item.managed_by_order_benefit &&
line_item.managed_by_order_benefit.promotion.lane == lane &&
!line_item.managed_by_order_benefit.in?(lane_benefits) &&
line_item.managed_by_order_benefit.remove_from(order)
end
end

def adjust_line_items(benefits)
order.discountable_line_items.select do |line_item|
line_item.variant.product.promotionable?
end.map do |line_item|
order.discountable_line_items.filter_map do |line_item|
next unless line_item.variant.product.promotionable?

discounts = generate_discounts(benefits, line_item)
chosen_item_discounts = SolidusPromotions.config.discount_chooser_class.new(discounts).call
[line_item, chosen_item_discounts]
Expand All @@ -67,7 +63,9 @@ def adjust_shipments(benefits)
end

def adjust_shipping_rates(benefits)
order.shipments.flat_map(&:shipping_rates).select(&:cost).map do |rate|
order.shipments.flat_map(&:shipping_rates).filter_map do |rate|
next unless rate.cost

discounts = generate_discounts(benefits, rate)
chosen_item_discounts = SolidusPromotions.config.discount_chooser_class.new(discounts).call
[rate, chosen_item_discounts]
Expand All @@ -82,11 +80,9 @@ def eligible_benefits_for_promotable(possible_benefits, promotable)

def generate_discounts(possible_benefits, item)
eligible_benefits = eligible_benefits_for_promotable(possible_benefits, item)
eligible_benefits.select do |benefit|
benefit.can_discount?(item)
end.map do |benefit|
benefit.discount(item)
end.compact
eligible_benefits.filter_map do |benefit|
benefit.can_discount?(item) && benefit.discount(item)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
expect(promotion.eligibility_results.success?).to be false
end

it "can tell us about all the errors", :pending do
it "can tell us about all the errors" do
subject
expect(promotion.eligibility_results.error_messages).to eq(
[
Expand Down
Loading