feat(customer): add customer addresses (#6224)

**What**
- adds methods to create update list customer addresses
- removes default_shipping_id and billing id from customer record and moves them to address (better normalization)
This commit is contained in:
Sebastian Rindom
2024-01-26 15:35:23 +00:00
committed by GitHub
parent 638b47ff70
commit 8c6cc82c5d
9 changed files with 672 additions and 112 deletions
@@ -59,6 +59,51 @@ describe("Customer Module Service", () => {
)
})
it("should create address", 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,
},
],
}
const customer = await service.create(customerData)
expect(customer).toEqual(
expect.objectContaining({
id: expect.any(String),
company_name: "Acme Corp",
first_name: "John",
last_name: "Doe",
addresses: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
address_1: "Testvej 1",
address_2: "Testvej 2",
city: "Testby",
country_code: "DK",
province: "Test",
postal_code: "8000",
phone: "123456789",
metadata: expect.objectContaining({ membership: "gold" }),
is_default_shipping: true,
}),
]),
})
)
})
it("should create multiple customers", async () => {
const customersData = [
{
@@ -419,6 +464,34 @@ describe("Customer Module Service", () => {
expect(remainingCustomers.length).toBe(0)
})
it("should cascade address relationship when deleting customer", async () => {
// Creating a customer and an address
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
})
// verify that the address was added
const customerWithAddress = await service.retrieve(customer.id, {
relations: ["addresses"],
})
expect(customerWithAddress.addresses?.length).toBe(1)
await service.delete(customer.id)
const res = await service.listAddresses({
customer_id: customer.id,
})
expect(res.length).toBe(0)
})
it("should cascade relationship when deleting customer", async () => {
// Creating a customer and a group
const customer = await service.create({
@@ -505,6 +578,288 @@ describe("Customer Module Service", () => {
})
})
describe("addAddresses", () => {
it("should add a single address to a customer", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const address = await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
})
const [customerWithAddress] = await service.list(
{ id: customer.id },
{ relations: ["addresses"] }
)
expect(customerWithAddress.addresses).toEqual([
expect.objectContaining({ id: address.id }),
])
})
it("should add multiple addresses to a customer", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const addresses = await service.addAddresses([
{
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
},
{
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10002",
country_code: "US",
},
])
const [customerWithAddresses] = await service.list(
{ id: customer.id },
{ relations: ["addresses"] }
)
expect(customerWithAddresses.addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: addresses[0].id }),
expect.objectContaining({ id: addresses[1].id }),
])
)
})
it("should only be possible to add one default shipping address per customer", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
is_default_shipping: true,
})
await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
is_default_shipping: false,
})
await expect(
service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10002",
country_code: "US",
is_default_shipping: true,
})
).rejects.toThrow()
})
it("should only be possible to add one default billing address per customer", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
is_default_billing: true,
})
await service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10001",
country_code: "US",
is_default_billing: false,
})
await expect(
service.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
postal_code: "10002",
country_code: "US",
is_default_billing: true,
})
).rejects.toThrow()
})
})
describe("updateAddresses", () => {
it("should update a single address", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const address = await service.addAddresses({
customer_id: customer.id,
address_name: "Home",
address_1: "123 Main St",
})
await service.updateAddress(address.id, {
address_name: "Work",
address_1: "456 Main St",
})
const updatedCustomer = await service.retrieve(customer.id, {
select: ["id"],
relations: ["addresses"],
})
expect(updatedCustomer.addresses).toEqual([
expect.objectContaining({
id: address.id,
address_name: "Work",
address_1: "456 Main St",
}),
])
})
it("should update multiple addresses", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const address1 = await service.addAddresses({
customer_id: customer.id,
address_name: "Home",
address_1: "123 Main St",
})
const address2 = await service.addAddresses({
customer_id: customer.id,
address_name: "Work",
address_1: "456 Main St",
})
await service.updateAddress(
{ customer_id: customer.id },
{
address_name: "Under Construction",
}
)
const updatedCustomer = await service.retrieve(customer.id, {
select: ["id"],
relations: ["addresses"],
})
expect(updatedCustomer.addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: address1.id,
address_name: "Under Construction",
}),
expect.objectContaining({
id: address2.id,
address_name: "Under Construction",
}),
])
)
})
it("should update multiple addresses with ids", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const [address1, address2] = await service.addAddresses([
{
customer_id: customer.id,
address_name: "Home",
address_1: "123 Main St",
},
{
customer_id: customer.id,
address_name: "Work",
address_1: "456 Main St",
},
])
await service.updateAddress([address1.id, address2.id], {
address_name: "Under Construction",
})
const updatedCustomer = await service.retrieve(customer.id, {
select: ["id"],
relations: ["addresses"],
})
expect(updatedCustomer.addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: address1.id,
address_name: "Under Construction",
}),
expect.objectContaining({
id: address2.id,
address_name: "Under Construction",
}),
])
)
})
})
describe("listAddresses", () => {
it("should list all addresses for a customer", async () => {
const customer = await service.create({
first_name: "John",
last_name: "Doe",
})
const [address1, address2] = await service.addAddresses([
{
customer_id: customer.id,
address_name: "Home",
address_1: "123 Main St",
},
{
customer_id: customer.id,
address_name: "Work",
address_1: "456 Main St",
},
])
const addresses = await service.listAddresses({
customer_id: customer.id,
})
expect(addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: address1.id,
address_name: "Home",
address_1: "123 Main St",
}),
expect.objectContaining({
id: address2.id,
address_name: "Work",
address_1: "456 Main St",
}),
])
)
})
})
describe("removeCustomerFromGroup", () => {
it("should remove a single customer from a group", async () => {
// Creating a customer and a group