Skip to content

Commit

Permalink
fix expired cart bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Oct 31, 2023
1 parent 4b931e0 commit 92efe1d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/launch_cart/carts/cart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule LaunchCart.Carts.Cart do
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "carts" do
field :status, Ecto.Enum, values: [:open, :checkout_started, :checkout_complete], default: :open
field :status, Ecto.Enum, values: [:open, :checkout_started, :checkout_complete, :checkout_expired], default: :open
field :checkout_session, :map
has_many :items, CartItem
belongs_to :store, Store
Expand All @@ -35,4 +35,7 @@ defmodule LaunchCart.Carts.Cart do

defp status_for(%Stripe.Session{status: "open"}), do: :checkout_started
defp status_for(%Stripe.Session{status: "complete"}), do: :checkout_complete
defp status_for(%Stripe.Session{status: "expired"}), do: :checkout_expired
defp status_for(%Stripe.Session{}), do: :checkout_started

end
20 changes: 20 additions & 0 deletions test/launch_cart/carts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ defmodule LaunchCart.CartTest do
assert {:ok, %Cart{status: :checkout_complete}} = Carts.load_cart(cart.id)
end

test "with expired status from stripe" do
cart =
insert(:cart,
status: :checkout_started,
checkout_session: %{id: "sess_expired", url: "http://foo.bar"}
)

assert {:ok, %Cart{status: :checkout_expired}} = Carts.load_cart(cart.id)
end

test "with nil status from stripe" do
cart =
insert(:cart,
status: :checkout_started,
checkout_session: %{id: "sess_nil_status", url: "http://foo.bar"}
)

assert {:ok, %Cart{status: :checkout_started}} = Carts.load_cart(cart.id)
end

test "with an invalid id" do
assert {:error, :cart_not_found} = Carts.load_cart("garbage")
end
Expand Down
8 changes: 8 additions & 0 deletions test/support/fake_stripe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,14 @@ defmodule LaunchCart.Test.FakeLaunch do
}}
end

def get_session("sess_expired", connect_account: _account) do
{:ok, %Stripe.Session{status: "expired"}}
end

def get_session("sess_nil_status", connect_account: _account) do
{:ok, %Stripe.Session{status: nil}}
end

def get_session("sess_complete", connect_account: _account) do
{:ok,
%Stripe.Session{
Expand Down

0 comments on commit 92efe1d

Please sign in to comment.