Skip to content

Commit b00aabb

Browse files
authored
fix(customers): guard nil billing/shipping address on customer show page (#29)
Imported customers (Bottle) have a shipping_address but no billing_address, so the show page crashed with (KeyError) key :full_address not found in: nil when rendering @customer.billing_address.full_address. Guard both addresses with the same && idiom already used on the order invoice page.
1 parent ff6532d commit b00aabb

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

lib/craftplan_web/live/manage/customer_live/show.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ defmodule CraftplanWeb.CustomerLive.Show do
2626
<:item title="Name">{@customer.full_name}</:item>
2727
<:item title="Email">{@customer.email}</:item>
2828
<:item title="Phone">{@customer.phone}</:item>
29-
<:item title="Billing Address">{@customer.billing_address.full_address}</:item>
30-
<:item title="Shipping Address">{@customer.shipping_address.full_address}</:item>
29+
<:item title="Billing Address">
30+
{@customer.billing_address && @customer.billing_address.full_address}
31+
</:item>
32+
<:item title="Shipping Address">
33+
{@customer.shipping_address && @customer.shipping_address.full_address}
34+
</:item>
3135
</.list>
3236
</div>
3337
</div>

test/craftplan_web/manage_customers_live_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ defmodule CraftplanWeb.ManageCustomersLiveTest do
5252
assert render(view) =~ c.first_name
5353
end
5454

55+
@tag role: :staff
56+
test "renders details tab when billing_address is nil (imported customer)", %{conn: conn} do
57+
# Bottle-imported customers have a shipping_address but no billing_address.
58+
c =
59+
Customer
60+
|> Ash.Changeset.for_create(:create, %{
61+
type: :individual,
62+
first_name: "Imported",
63+
last_name: "Buyer",
64+
email: "imported+#{System.unique_integer([:positive])}@local",
65+
shipping_address: %{street: "1 St", city: "DC", country: "US"}
66+
})
67+
|> Ash.create!()
68+
69+
assert is_nil(c.billing_address)
70+
71+
{:ok, view, _html} = live(conn, ~p"/manage/customers/#{c.reference}")
72+
assert render(view) =~ "Imported"
73+
assert render(view) =~ "Billing Address"
74+
assert render(view) =~ "Shipping Address"
75+
end
76+
5577
@tag role: :staff
5678
test "renders orders and statistics tabs", %{conn: conn} do
5779
c = create_customer!()

0 commit comments

Comments
 (0)