Skip to content

Commit

Permalink
feat(customer): store addresses (#6283)
Browse files Browse the repository at this point in the history
- GET /customers/me/addresses
- POST /customers/me/addresses
- GET /customers/me/addresses/:address_id
- POST /customers/me/addresses/:address_id
- DELETE /customers/me/addresses/:address_id
  • Loading branch information
srindom authored Feb 1, 2024
1 parent fab1799 commit a28822e
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }

describe("POST /store/customers/me/addresses", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should create a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
customerModuleService,
appContainer.resolve(ModuleRegistrationName.AUTH)
)

const api = useApi() as any
const response = await api.post(
`/store/customers/me/addresses`,
{
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
},
{ headers: { authorization: `Bearer ${jwt}` } }
)

expect(response.status).toEqual(200)
expect(response.data.address).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
customer_id: customer.id,
})
)

const customerWithAddresses = await customerModuleService.retrieve(
customer.id,
{ relations: ["addresses"] }
)

expect(customerWithAddresses.addresses?.length).toEqual(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"

const env = { MEDUSA_FF_MEDUSA_V2: true }

describe("DELETE /store/customers/me/addresses/:address_id", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should delete a customer address", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
customerModuleService,
appContainer.resolve(ModuleRegistrationName.AUTH)
)

const address = await customerModuleService.addAddresses({
customer_id: customer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})

const api = useApi() as any
const response = await api.delete(
`/store/customers/me/addresses/${address.id}`,
{ headers: { authorization: `Bearer ${jwt}` } }
)

expect(response.status).toEqual(200)

const updatedCustomer = await customerModuleService.retrieve(customer.id, {
relations: ["addresses"],
})

expect(updatedCustomer.addresses?.length).toEqual(0)
})

it("should fail to delete another customer's address", async () => {
const { jwt } = await createAuthenticatedCustomer(
customerModuleService,
appContainer.resolve(ModuleRegistrationName.AUTH)
)

const otherCustomer = await customerModuleService.create({
first_name: "Jane",
last_name: "Doe",
})
const address = await customerModuleService.addAddresses({
customer_id: otherCustomer.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
})

const api = useApi() as any
const response = await api
.delete(`/store/customers/me/addresses/${address.id}`, {
headers: { authorization: `Bearer ${jwt}` },
})
.catch((e) => e.response)

expect(response.status).toEqual(404)
})
})
23 changes: 6 additions & 17 deletions integration-tests/plugins/__tests__/customer/store/get-me.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService, IAuthModuleService } from "@medusajs/types"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"

jest.setTimeout(50000)

Expand Down Expand Up @@ -43,22 +44,10 @@ describe("GET /store/customers", () => {
})

it("should retrieve auth user's customer", async () => {
const customer = await customerModuleService.create({
first_name: "John",
last_name: "Doe",
email: "[email protected]",
})

const authService: IAuthModuleService = appContainer.resolve(
ModuleRegistrationName.AUTH
const { customer, jwt } = await createAuthenticatedCustomer(
customerModuleService,
appContainer.resolve(ModuleRegistrationName.AUTH)
)
const authUser = await authService.createAuthUser({
entity_id: "store_user",
provider_id: "test",
app_metadata: { customer_id: customer.id },
})

const jwt = await authService.generateJwtToken(authUser.id, "store")

const api = useApi() as any
const response = await api.get(`/store/customers/me`, {
Expand All @@ -68,7 +57,7 @@ describe("GET /store/customers", () => {
expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
id: expect.any(String),
id: customer.id,
first_name: "John",
last_name: "Doe",
email: "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"

const env = { MEDUSA_FF_MEDUSA_V2: true }

describe("GET /store/customers/me/addresses", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should get all customer addresses and its count", async () => {
const { customer, jwt } = await createAuthenticatedCustomer(
customerModuleService,
appContainer.resolve(ModuleRegistrationName.AUTH)
)

await customerModuleService.addAddresses([
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 1",
customer_id: customer.id,
},
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 2",
customer_id: customer.id,
},
{
first_name: "Test",
last_name: "Test",
address_1: "Test street 3",
customer_id: customer.id,
},
])

await customerModuleService.create({
first_name: "Test Test",
last_name: "Test Test",
addresses: [
{
first_name: "Test TEST",
last_name: "Test TEST",
address_1: "NOT street 1",
},
],
})

const api = useApi() as any
const response = await api.get(`/store/customers/me/addresses`, {
headers: { authorization: `Bearer ${jwt}` },
})

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3)
expect(response.data.addresses).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 1",
}),
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 2",
}),
expect.objectContaining({
id: expect.any(String),
customer_id: customer.id,
address_1: "Test street 3",
}),
])
)
})
})
Loading

0 comments on commit a28822e

Please sign in to comment.