-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
823 additions
and
20 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
integration-tests/plugins/__tests__/customer/store/create-customer-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
93 changes: 93 additions & 0 deletions
93
integration-tests/plugins/__tests__/customer/store/delete-customer-address.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
@@ -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`, { | ||
|
@@ -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]", | ||
|
105 changes: 105 additions & 0 deletions
105
integration-tests/plugins/__tests__/customer/store/list-customer-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}), | ||
]) | ||
) | ||
}) | ||
}) |
Oops, something went wrong.