-
-
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): list customer (#6206)
**What** - GET /admin/customers - GET /admin/customer-groups
- Loading branch information
Showing
17 changed files
with
737 additions
and
4 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
integration-tests/plugins/__tests__/customer/admin/list-customer-groups.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,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", | ||
}), | ||
]) | ||
}) | ||
}) |
119 changes: 119 additions & 0 deletions
119
integration-tests/plugins/__tests__/customer/admin/list-customers.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,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: "[email protected]", | ||
}, | ||
]) | ||
|
||
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: "[email protected]", | ||
}), | ||
]) | ||
}) | ||
|
||
it("should filter customers by last name", async () => { | ||
await customerModuleService.create([ | ||
{ | ||
first_name: "Jane", | ||
last_name: "Doe", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
first_name: "John", | ||
last_name: "Doe", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
first_name: "LeBron", | ||
last_name: "James", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
first_name: "John", | ||
last_name: "Silver", | ||
email: "[email protected]", | ||
}, | ||
]) | ||
|
||
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: "[email protected]", | ||
}) | ||
) | ||
expect(response.data.customers).toContainEqual( | ||
expect.objectContaining({ | ||
id: expect.any(String), | ||
first_name: "John", | ||
last_name: "Doe", | ||
email: "[email protected]", | ||
}) | ||
) | ||
}) | ||
}) |
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
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
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
52 changes: 52 additions & 0 deletions
52
packages/medusa/src/api-v2/admin/customer-groups/middlewares.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,52 @@ | ||
import { MedusaV2Flag } from "@medusajs/utils" | ||
|
||
import { | ||
isFeatureFlagEnabled, | ||
transformBody, | ||
transformQuery, | ||
} from "../../../api/middlewares" | ||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import * as QueryConfig from "./query-config" | ||
import { | ||
AdminGetCustomerGroupsParams, | ||
AdminGetCustomerGroupsGroupParams, | ||
AdminPostCustomerGroupsReq, | ||
AdminPostCustomerGroupsGroupReq, | ||
} from "./validators" | ||
|
||
export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
matcher: "/admin/customer-groups*", | ||
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/admin/customer-groups", | ||
middlewares: [ | ||
transformQuery( | ||
AdminGetCustomerGroupsParams, | ||
QueryConfig.listTransformQueryConfig | ||
), | ||
], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/admin/customer-groups/:id", | ||
middlewares: [ | ||
transformQuery( | ||
AdminGetCustomerGroupsGroupParams, | ||
QueryConfig.retrieveTransformQueryConfig | ||
), | ||
], | ||
}, | ||
{ | ||
method: ["POST"], | ||
matcher: "/admin/customer-groups", | ||
middlewares: [transformBody(AdminPostCustomerGroupsReq)], | ||
}, | ||
{ | ||
method: ["POST"], | ||
matcher: "/admin/customer-groups/:id", | ||
middlewares: [transformBody(AdminPostCustomerGroupsGroupReq)], | ||
}, | ||
] |
21 changes: 21 additions & 0 deletions
21
packages/medusa/src/api-v2/admin/customer-groups/query-config.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,21 @@ | ||
export const defaultAdminCustomerGroupRelations = [] | ||
export const allowedAdminCustomerGroupRelations = ["customers"] | ||
export const defaultAdminCustomerGroupFields = [ | ||
"id", | ||
"name", | ||
"created_at", | ||
"updated_at", | ||
"deleted_at", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaultFields: defaultAdminCustomerGroupFields, | ||
defaultRelations: defaultAdminCustomerGroupRelations, | ||
allowedRelations: allowedAdminCustomerGroupRelations, | ||
isList: false, | ||
} | ||
|
||
export const listTransformQueryConfig = { | ||
...retrieveTransformQueryConfig, | ||
isList: true, | ||
} |
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,24 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { ICustomerModuleService } from "@medusajs/types" | ||
import { MedusaRequest, MedusaResponse } from "../../../types/routing" | ||
|
||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { | ||
const customerModuleService = req.scope.resolve<ICustomerModuleService>( | ||
ModuleRegistrationName.CUSTOMER | ||
) | ||
|
||
const [groups, count] = | ||
await customerModuleService.listAndCountCustomerGroups( | ||
req.filterableFields, | ||
req.listConfig | ||
) | ||
|
||
const { offset, limit } = req.validatedQuery | ||
|
||
res.json({ | ||
count, | ||
groups, | ||
offset, | ||
limit, | ||
}) | ||
} |
Oops, something went wrong.