Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(customer): store addresses #6283

Merged
merged 8 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
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: "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`, {
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: "john@me.com",
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
Loading