feat(customer): list customer (#6206)

**What**

- GET /admin/customers
- GET /admin/customer-groups
This commit is contained in:
Sebastian Rindom
2024-01-29 16:56:39 +00:00
committed by GitHub
parent 90cff0777f
commit 87390704be
17 changed files with 737 additions and 4 deletions
@@ -0,0 +1,63 @@
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 adminSeeder from "../../../../helpers/admin-seeder"
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/customer-groups", () => {
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()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get all customer groups and its count", async () => {
await customerModuleService.createCustomerGroup({
name: "Test",
})
const api = useApi() as any
const response = await api.get(`/admin/customer-groups`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.groups).toEqual([
expect.objectContaining({
id: expect.any(String),
name: "Test",
}),
])
})
})
@@ -0,0 +1,119 @@
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 adminSeeder from "../../../../helpers/admin-seeder"
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/customers", () => {
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()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get all customers and its count", async () => {
await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
email: "test@me.com",
},
])
const api = useApi() as any
const response = await api.get(`/admin/customers`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.customers).toEqual([
expect.objectContaining({
id: expect.any(String),
first_name: "Test",
last_name: "Test",
email: "test@me.com",
}),
])
})
it("should filter customers by last name", async () => {
await customerModuleService.create([
{
first_name: "Jane",
last_name: "Doe",
email: "jane@me.com",
},
{
first_name: "John",
last_name: "Doe",
email: "john@me.com",
},
{
first_name: "LeBron",
last_name: "James",
email: "lebron@me.com",
},
{
first_name: "John",
last_name: "Silver",
email: "johns@me.com",
},
])
const api = useApi() as any
const response = await api.get(
`/admin/customers?last_name=Doe`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(2)
expect(response.data.customers).toContainEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "Jane",
last_name: "Doe",
email: "jane@me.com",
})
)
expect(response.data.customers).toContainEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
last_name: "Doe",
email: "john@me.com",
})
)
})
})
@@ -71,6 +71,11 @@ module.exports = {
resources: "shared",
resolve: "@medusajs/promotion",
},
[Modules.CUSTOMER]: {
scope: "internal",
resources: "shared",
resolve: "@medusajs/customer",
},
[Modules.SALES_CHANNEL]: {
scope: "internal",
resources: "shared",
+1
View File
@@ -10,6 +10,7 @@
},
"dependencies": {
"@medusajs/cache-inmemory": "workspace:*",
"@medusajs/customer": "workspace:^",
"@medusajs/event-bus-local": "workspace:*",
"@medusajs/inventory": "workspace:^",
"@medusajs/medusa": "workspace:*",