-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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): list customer #6206
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c37b269
feat: list customers and groups
srindom 73aa023
fix: add filter test and prepare for remote query
srindom 252aa09
Merge branch 'develop' into feat/customer-endpoints
srindom a63119d
Merge branch 'develop' into feat/customer-endpoints
srindom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", | ||
}), | ||
]) | ||
}) | ||
}) |
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
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]", | ||
}), | ||
]) | ||
}) | ||
}) |
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, | ||
}) | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/medusa/src/api-v2/admin/customer-groups/validators.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,114 @@ | ||
import { OperatorMap } from "@medusajs/types" | ||
import { Type } from "class-transformer" | ||
import { | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
ValidateNested, | ||
} from "class-validator" | ||
import { FindParams, extendedFindParamsMixin } from "../../../types/common" | ||
import { OperatorMapValidator } from "../../../types/validators/operator-map" | ||
|
||
export class AdminGetCustomerGroupsGroupParams extends FindParams {} | ||
|
||
class FilterableCustomerPropsValidator { | ||
@IsOptional() | ||
@IsString({ each: true }) | ||
id?: string | string[] | ||
|
||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => OperatorMapValidator) | ||
email?: string | string[] | OperatorMap<string> | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
default_billing_address_id?: string | string[] | null | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
default_shipping_address_id?: string | string[] | null | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
company_name?: string | string[] | OperatorMap<string> | null | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
first_name?: string | string[] | OperatorMap<string> | null | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
last_name?: string | string[] | OperatorMap<string> | null | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
created_by?: string | string[] | null | ||
|
||
@IsOptional() | ||
@ValidateNested() | ||
@Type(() => OperatorMapValidator) | ||
created_at?: OperatorMap<string> | ||
|
||
@IsOptional() | ||
@ValidateNested() | ||
@Type(() => OperatorMapValidator) | ||
updated_at?: OperatorMap<string> | ||
} | ||
|
||
export class AdminGetCustomerGroupsParams extends extendedFindParamsMixin({ | ||
limit: 100, | ||
offset: 0, | ||
}) { | ||
@IsOptional() | ||
@IsString({ each: true }) | ||
id?: string | string[] | ||
|
||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => OperatorMapValidator) | ||
name?: string | OperatorMap<string> | ||
|
||
@IsOptional() | ||
@ValidateNested() | ||
@Type(() => FilterableCustomerPropsValidator) | ||
customers?: FilterableCustomerPropsValidator | string | string[] | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
created_by?: string | string[] | null | ||
|
||
@IsOptional() | ||
@ValidateNested() | ||
@Type(() => OperatorMapValidator) | ||
created_at?: OperatorMap<string> | ||
|
||
@IsOptional() | ||
@ValidateNested() | ||
@Type(() => OperatorMapValidator) | ||
updated_at?: OperatorMap<string> | ||
|
||
// Additional filters from BaseFilterable | ||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => AdminGetCustomerGroupsParams) | ||
$and?: AdminGetCustomerGroupsParams[] | ||
|
||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => AdminGetCustomerGroupsParams) | ||
$or?: AdminGetCustomerGroupsParams[] | ||
} | ||
|
||
export class AdminPostCustomerGroupsReq { | ||
@IsNotEmpty() | ||
@IsString() | ||
name: string | ||
} | ||
|
||
export class AdminPostCustomerGroupsGroupReq { | ||
@IsNotEmpty() | ||
@IsString() | ||
@IsOptional() | ||
name?: string | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can align how we want to build the GET endpoints.
I believe we should use the remoteQuery to be able to easily expand relations from here.
We have 2 utils that can help on that:
remoteQueryObjectFromString and remoteQueryObjectToString
we can discuss it on Monday and come up with a pattern to follow in all the api-v2 endpoints. cc: @adrien2p @olivermrbl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree remote query should be the way to go - do we have an example implementation of the approach somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have mixed examples that we need to normalize from now on.
I believe this test is a good starting point, where we have a list of all default fields and relations to be listed. We need to think about and implement how to have a list of "allowed" relations to be expanded, and how to customize this list, adding custom relations to it.
from there it is used like this:
variables are handled here before calling
module.list(filters, options)
We can send all the filters and options needed to the service from here.
As this will be replicate in most GET endpoints, I think it worth spending some time in it, even if in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will need to see an example with filters and expands applied - I wasn't able to get this to work for these.
I suggest that we merge PRs without RemoteQuery until we have its feature set more standardized.