feat(customer): store addresses (#6283)
- 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
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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)
|
||||
|
||||
@@ -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: "john@me.com",
|
||||
})
|
||||
|
||||
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`, {
|
||||
@@ -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: "john@me.com",
|
||||
|
||||
@@ -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",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,99 @@
|
||||
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("POST /store/customers/:id/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 update 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.post(
|
||||
`/store/customers/me/addresses/${address.id}`,
|
||||
{
|
||||
first_name: "Jane",
|
||||
},
|
||||
{ headers: { authorization: `Bearer ${jwt}` } }
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.address).toEqual(
|
||||
expect.objectContaining({
|
||||
id: address.id,
|
||||
first_name: "Jane",
|
||||
last_name: "Doe",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to update 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
|
||||
.post(
|
||||
`/store/customers/me/addresses/${address.id}`,
|
||||
{ first_name: "Jane" },
|
||||
{ headers: { authorization: `Bearer ${jwt}` } }
|
||||
)
|
||||
.catch((e) => e.response)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
import { ICustomerModuleService, IAuthModuleService } from "@medusajs/types"
|
||||
|
||||
export const createAuthenticatedCustomer = async (
|
||||
customerModuleService: ICustomerModuleService,
|
||||
authService: IAuthModuleService
|
||||
) => {
|
||||
const customer = await customerModuleService.create({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
email: "john@me.com",
|
||||
})
|
||||
|
||||
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")
|
||||
|
||||
return { customer, authUser, jwt }
|
||||
}
|
||||
Reference in New Issue
Block a user