Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PromotionHandler::Page #70

Merged
merged 1 commit into from
Nov 2, 2023
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
35 changes: 35 additions & 0 deletions app/models/solidus_friendly_promotions/promotion_handler/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module SolidusFriendlyPromotions
module PromotionHandler
class Page
attr_reader :order, :path

def initialize(order, path)
@order = order
@path = path.gsub(/\A\//, "")
end

def activate
if promotion
active_promotions = SolidusFriendlyPromotions::PromotionLoader.new(order: order).call
SolidusFriendlyPromotions::FriendlyPromotionDiscounter.new(
order,
active_promotions + [promotion],
collect_eligibility_results: true
).call
if promotion.eligibility_results.success?
order.friendly_promotions << promotion
order.recalculate
end
end
end

private

def promotion
@promotion ||= SolidusFriendlyPromotions::Promotion.active.find_by(path: path)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SolidusFriendlyPromotions::PromotionHandler::Page, type: :model do
subject { described_class.new(order, path).activate }

let(:order) { create(:order_with_line_items, line_items_count: 1) }
let!(:promotion) { create(:friendly_promotion, :with_adjustable_action, name: "10% off", path: "10off") }
let(:path) { "10off" }

it "activates at the right path" do
expect(order.line_item_adjustments.count).to eq(0)
subject
expect(order.line_item_adjustments.count).to eq(1)
end

context "when promotion is expired" do
before do
promotion.update(
starts_at: 1.week.ago,
expires_at: 1.day.ago
)
end

it "is not activated" do
expect(order.line_item_adjustments.count).to eq(0)
subject
expect(order.line_item_adjustments.count).to eq(0)
end
end

context "with a wrong path" do
let(:path) { "wrongpath" }
it "does not activate at the wrong path" do
expect(order.line_item_adjustments.count).to eq(0)
subject
expect(order.line_item_adjustments.count).to eq(0)
end
end

context "when promotion is not eligible" do
let(:impossible_rule) { SolidusFriendlyPromotions::Rules::NthOrder.new(preferred_nth_order: 2) }
before do
promotion.rules << impossible_rule
end

it "is not applied" do
expect { subject }.not_to change { order.line_item_adjustments.count }
end

it "does not connect the promotion to the order" do
expect { subject }.not_to change { order.friendly_order_promotions.count }
end
end
end