From 92efe1dfa41dbc94abfaca0bcc50e446ba0113f3 Mon Sep 17 00:00:00 2001 From: Chris Nelson Date: Tue, 31 Oct 2023 14:35:39 -0400 Subject: [PATCH] fix expired cart bug --- lib/launch_cart/carts/cart.ex | 5 ++++- test/launch_cart/carts_test.exs | 20 ++++++++++++++++++++ test/support/fake_stripe.ex | 8 ++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/launch_cart/carts/cart.ex b/lib/launch_cart/carts/cart.ex index 1d49026..525177c 100644 --- a/lib/launch_cart/carts/cart.ex +++ b/lib/launch_cart/carts/cart.ex @@ -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 @@ -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 diff --git a/test/launch_cart/carts_test.exs b/test/launch_cart/carts_test.exs index e0c9a15..e20cda1 100644 --- a/test/launch_cart/carts_test.exs +++ b/test/launch_cart/carts_test.exs @@ -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 diff --git a/test/support/fake_stripe.ex b/test/support/fake_stripe.ex index 2affd3b..93150bf 100644 --- a/test/support/fake_stripe.ex +++ b/test/support/fake_stripe.ex @@ -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{