feat(customer): manage default address selection (#6295)

**What**
- Catches unique constraints on customer_id, is_default_billing/is_default_shipping and reformats
- Adds an step to create and update of addresses that unsets the previous default shipping/billing address if necessary.
  - This creates a behavior in the API where you can always set an address to be default and it will automatically unset the previous one for you.
This commit is contained in:
Sebastian Rindom
2024-02-01 12:28:14 +00:00
committed by GitHub
parent a28822e0d4
commit a2bf6756ac
16 changed files with 506 additions and 15 deletions
@@ -100,6 +100,37 @@ describe("Customer Module Service", () => {
)
})
it("should fail to create two default shipping", async () => {
const customerData = {
company_name: "Acme Corp",
first_name: "John",
last_name: "Doe",
addresses: [
{
address_1: "Testvej 1",
address_2: "Testvej 2",
city: "Testby",
country_code: "DK",
province: "Test",
postal_code: "8000",
phone: "123456789",
metadata: { membership: "gold" },
is_default_shipping: true,
},
{
address_1: "Test Ave 1",
address_2: "Test Ave 2",
city: "Testville",
country_code: "US",
is_default_shipping: true,
},
],
}
await expect(service.create(customerData)).rejects.toThrow(
"A default shipping address already exists"
)
})
it("should create multiple customers", async () => {
const customersData = [
{
@@ -662,7 +693,7 @@ describe("Customer Module Service", () => {
country_code: "US",
is_default_shipping: true,
})
).rejects.toThrow()
).rejects.toThrow("A default shipping address already exists")
})
it("should only be possible to add one default billing address per customer", async () => {
@@ -696,7 +727,7 @@ describe("Customer Module Service", () => {
country_code: "US",
is_default_billing: true,
})
).rejects.toThrow()
).rejects.toThrow("A default billing address already exists")
})
})
@@ -813,6 +844,29 @@ describe("Customer Module Service", () => {
])
)
})
it("should fail when updating address to a default shipping address when one already exists", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
addresses: [
{
address_name: "Home",
address_1: "123 Main St",
is_default_shipping: true,
},
],
})
const address = await service.addAddresses({
customer_id: customer.id,
address_name: "Work",
address_1: "456 Main St",
})
await expect(
service.updateAddress(address.id, { is_default_shipping: true })
).rejects.toThrow("A default shipping address already exists")
})
})
describe("listAddresses", () => {