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 can apply to promotions #6013

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Feat(Promotions): Implement SolidusPromotions#order_activatable?
mamhoff authored and tvdeyen committed Dec 5, 2024
commit a2e034ea9736bbed94512058a17b6eba23b5bf68
10 changes: 10 additions & 0 deletions promotions/app/models/solidus_promotions/promotion.rb
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

module SolidusPromotions
class Promotion < Spree::Base
UNACTIVATABLE_ORDER_STATES = ["awaiting_return", "returned", "canceled"]

include Spree::SoftDeletable

belongs_to :category, class_name: "SolidusPromotions::PromotionCategory",
@@ -59,6 +61,14 @@ def self.ordered_lanes
lanes.sort_by(&:last).to_h
end

def self.order_activatable?(order)
return false if UNACTIVATABLE_ORDER_STATES.include?(order.state)
return false if order.shipped?
return false if order.complete? && !SolidusPromotions.config.recalculate_complete_orders

true
end

self.allowed_ransackable_associations = ["codes"]
self.allowed_ransackable_attributes = %w[name customer_label path promotion_category_id lane updated_at]
self.allowed_ransackable_scopes = %i[active with_discarded]
Original file line number Diff line number Diff line change
@@ -453,7 +453,7 @@ def expect_adjustment_creation(adjustable:, promotion:)
end
end

describe "#can_apply?", :pending do
describe "#can_apply?" do
let(:order) { double("Order").as_null_object }

subject { described_class.new(order).can_apply? }
52 changes: 52 additions & 0 deletions promotions/spec/models/solidus_promotions/promotion_spec.rb
Original file line number Diff line number Diff line change
@@ -686,4 +686,56 @@
expect(subject).to be_nil
end
end

describe ".order_activatable" do
let(:order) { create :order }

subject { described_class.order_activatable?(order) }

it "is true" do
expect(subject).to be true
end

context "when the order is in the cart state" do
let(:order) { create :order, state: "cart" }

it { is_expected.to be true }
end

context "when the order is shipped" do
let(:order) { create :order, state: "complete", shipment_state: "shipped" }

it { is_expected.to be false }
end

context "when the order is completed but not shipped" do
let(:order) { create :order, state: "complete", shipment_state: "ready" }

it { is_expected.to be true }

context "when the promotion system is configured to prohibit applying promotions to completed orders" do
before { stub_spree_preferences(SolidusPromotions.configuration, recalculate_complete_orders: false) }

it { is_expected.to be false }
end
end

context "when the order is canceled" do
let(:order) { create :order, state: "canceled" }

it { is_expected.to be false }
end

context "when the order is awaiting return" do
let(:order) { create :order, state: "awaiting_return" }

it { is_expected.to be false }
end

context "when the order is returned" do
let(:order) { create :order, state: "returned" }

it { is_expected.to be false }
end
end
end
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "Adjustments", :pending, type: :feature do
RSpec.describe "Adjustments", type: :feature do
stub_authorization!

let!(:ship_address) { create(:address) }
@@ -13,8 +13,10 @@

let(:tax_category) { create(:tax_category) }
let(:variant) { create(:variant, tax_category:) }
let(:preferences) { {} }

before(:each) do
stub_spree_preferences(SolidusPromotions.configuration, preferences)
order.recalculate

visit spree.admin_path
@@ -43,20 +45,14 @@
end

context "when the promotion system is configured to allow applying promotions to completed orders" do
before do
expect(SolidusPromotions.config).to receive(:recalculate_complete_orders).and_return(true)
end

it "shows input field for promotion code" do
expect(page).to have_content("Adjustments")
expect(page).to have_field("coupon_code")
end
end

context "when the promotion system is configured to not allow applying promotions to completed orders" do
before do
expect(SolidusPromotions.config).to receive(:recalculate_complete_orders).and_return(false)
end
let(:preferences) { { recalculate_complete_orders: false } }

it "does not show input field for promotion code" do
expect(page).to have_content("Adjustments")