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

fix(medusa): Implement listAndCount for UserService and update list endpoint #6190

Merged
merged 15 commits into from
Jan 24, 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
9 changes: 9 additions & 0 deletions .changeset/lucky-snails-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@medusajs/medusa": patch
"@medusajs/medusa-js": patch
"medusa-react": patch
---

fix(medusa): Implements `listAndCount` method for UserService, and updates list endpoint to accept the expected params.
fix(medusa-js): Update `admin.users.list` to accept query params.
fix(medusa-react): Update `useAdminUsers` hook to accept query params.

This file was deleted.

163 changes: 109 additions & 54 deletions integration-tests/api/__tests__/admin/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ describe("/admin/users", () => {
const response = await api.get("/admin/users/admin_user", adminReqConfig)

expect(response.status).toEqual(200)
expect(response.data.user).toMatchSnapshot({
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
created_at: expect.any(String),
updated_at: expect.any(String),
})
expect(response.data.user).toEqual(
expect.objectContaining({
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
created_at: expect.any(String),
updated_at: expect.any(String),
})
)
})

it("lists users", async () => {
Expand All @@ -75,25 +77,72 @@ describe("/admin/users", () => {

expect(response.status).toEqual(200)

expect(response.data.users).toMatchSnapshot([
{
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
id: "member-user",
role: "member",
email: "member@test.com",
first_name: "member",
last_name: "user",
created_at: expect.any(String),
updated_at: expect.any(String),
},
])
expect(response.data.users).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
expect.objectContaining({
id: "member-user",
role: "member",
email: "member@test.com",
first_name: "member",
last_name: "user",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
])
)
})

it("lists users that match the free text search", async () => {
const api = useApi()

const response = await api.get("/admin/users?q=member", adminReqConfig)

expect(response.status).toEqual(200)

expect(response.data.users.length).toEqual(1)
expect(response.data.users).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "member-user",
role: "member",
email: "member@test.com",
first_name: "member",
last_name: "user",
created_at: expect.any(String),
updated_at: expect.any(String),
}),
])
)
})

it("orders users by created_at", async () => {
const api = useApi()

const response = await api.get(
"/admin/users?order=created_at",
adminReqConfig
)

expect(response.status).toEqual(200)
expect(response.data.users.length).toBeGreaterThan(0)

for (let i = 0; i < response.data.users.length - 1; i++) {
const user1 = response.data.users[i]
const user2 = response.data.users[i + 1]

const date1 = new Date(user1.created_at)
const date2 = new Date(user2.created_at)

expect(date1.getTime()).toBeLessThanOrEqual(date2.getTime())
}
})
})

Expand Down Expand Up @@ -138,13 +187,15 @@ describe("/admin/users", () => {
.catch((err) => console.log(err))

expect(response.status).toEqual(200)
expect(response.data.user).toMatchSnapshot({
id: expect.stringMatching(/^usr_*/),
created_at: expect.any(String),
updated_at: expect.any(String),
role: "member",
email: "test@test123.com",
})
expect(response.data.user).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^usr_*/),
created_at: expect.any(String),
updated_at: expect.any(String),
role: "member",
email: "test@test123.com",
})
)
})

it("updates a user", async () => {
Expand All @@ -159,15 +210,17 @@ describe("/admin/users", () => {
.catch((err) => console.log(err.response.data.message))

expect(updateResponse.status).toEqual(200)
expect(updateResponse.data.user).toMatchSnapshot({
id: "member-user",
created_at: expect.any(String),
updated_at: expect.any(String),
role: "member",
email: "member@test.com",
first_name: "karl",
last_name: "user",
})
expect(updateResponse.data.user).toEqual(
expect.objectContaining({
id: "member-user",
created_at: expect.any(String),
updated_at: expect.any(String),
role: "member",
email: "member@test.com",
first_name: "karl",
last_name: "user",
})
)
})

describe("Password reset", () => {
Expand Down Expand Up @@ -417,17 +470,19 @@ describe("[MEDUSA_FF_ANALYTICS] /admin/analytics-config", () => {
`SELECT * FROM public.analytics_config WHERE user_id = '${userId}'`
)

expect(configs).toMatchSnapshot([
{
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: expect.any(Date),
id: expect.any(String),
user_id: userId,
opt_out: false,
anonymize: false,
},
])
expect(configs).toEqual(
expect.arrayContaining([
expect.objectContaining({
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: expect.any(Date),
id: expect.any(String),
user_id: userId,
opt_out: false,
anonymize: false,
}),
])
)
})
})
})
Loading