-
-
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(user): Init user module (#6293)
* init user module * add migration * update module with latest utils * pr ready * make interface types interfaces
- Loading branch information
Showing
43 changed files
with
1,005 additions
and
26 deletions.
There are no files selected for viewing
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
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,5 @@ | ||
export type UserDTO = { | ||
id: string | ||
} | ||
|
||
export type FilterableUserProps = {} |
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,3 @@ | ||
export * from "./common" | ||
export * from "./mutations" | ||
export * from "./service" |
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,6 @@ | ||
export interface CreateUserDTO { | ||
id?: string | ||
} | ||
export interface UpdateUserDTO { | ||
id: string | ||
} |
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,36 @@ | ||
import { CreateUserDTO, UpdateUserDTO } from "./mutations" | ||
import { FilterableUserProps, UserDTO } from "./common" | ||
|
||
import { Context } from "../shared-context" | ||
import { FindConfig } from "../common" | ||
import { IModuleService } from "../modules-sdk" | ||
|
||
export interface IUserModuleService extends IModuleService { | ||
retrieve( | ||
id: string, | ||
config?: FindConfig<UserDTO>, | ||
sharedContext?: Context | ||
): Promise<UserDTO> | ||
|
||
list( | ||
filters?: FilterableUserProps, | ||
config?: FindConfig<UserDTO>, | ||
sharedContext?: Context | ||
): Promise<UserDTO[]> | ||
|
||
listAndCount( | ||
filters?: FilterableUserProps, | ||
config?: FindConfig<UserDTO>, | ||
sharedContext?: Context | ||
): Promise<[UserDTO[], number]> | ||
|
||
create(data: CreateUserDTO[], sharedContext?: Context): Promise<UserDTO[]> | ||
|
||
create(data: CreateUserDTO, sharedContext?: Context): Promise<UserDTO> | ||
|
||
update(data: UpdateUserDTO[], sharedContext?: Context): Promise<UserDTO[]> | ||
|
||
update(data: UpdateUserDTO, sharedContext?: Context): Promise<UserDTO> | ||
|
||
delete(ids: string[], sharedContext?: Context): Promise<void> | ||
} |
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,6 @@ | ||
/dist | ||
node_modules | ||
.DS_store | ||
.env* | ||
.env | ||
*.sql |
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,3 @@ | ||
# User Module | ||
|
||
The User Module is Medusa’s user engine. |
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,16 @@ | ||
import { SqlEntityManager } from "@mikro-orm/postgresql" | ||
import { User } from "@models" | ||
|
||
export const createUsers = async ( | ||
manager: SqlEntityManager, | ||
userData = [{ id: "1" }] | ||
) => { | ||
const users: User[] = [] | ||
|
||
for (const user of userData) { | ||
const usr = manager.create(User, user) | ||
users.push(usr) | ||
} | ||
|
||
await manager.persistAndFlush(users) | ||
} |
197 changes: 197 additions & 0 deletions
197
packages/user/integration-tests/__tests__/services/module/user.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,197 @@ | ||
import { IUserModuleService } from "@medusajs/types/dist/user" | ||
import { MikroOrmWrapper } from "../../../utils" | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { SqlEntityManager } from "@mikro-orm/postgresql" | ||
import { createUsers } from "../../../__fixtures__/user" | ||
import { getInitModuleConfig } from "../../../utils/get-init-module-config" | ||
import { initModules } from "medusa-test-utils" | ||
|
||
jest.setTimeout(30000) | ||
|
||
describe("UserModuleService - User", () => { | ||
let service: IUserModuleService | ||
let testManager: SqlEntityManager | ||
let shutdownFunc: () => Promise<void> | ||
|
||
beforeAll(async () => { | ||
const initModulesConfig = getInitModuleConfig() | ||
|
||
const { medusaApp, shutdown } = await initModules(initModulesConfig) | ||
|
||
service = medusaApp.modules[Modules.USER] | ||
|
||
shutdownFunc = shutdown | ||
}) | ||
|
||
beforeEach(async () => { | ||
await MikroOrmWrapper.setupDatabase() | ||
testManager = MikroOrmWrapper.forkManager() | ||
|
||
await createUsers(testManager) | ||
}) | ||
|
||
afterEach(async () => { | ||
await MikroOrmWrapper.clearDatabase() | ||
}) | ||
|
||
afterAll(async () => { | ||
await shutdownFunc() | ||
}) | ||
|
||
describe("listUsers", () => { | ||
it("should list users", async () => { | ||
const users = await service.list() | ||
const serialized = JSON.parse(JSON.stringify(users)) | ||
|
||
expect(serialized).toEqual([ | ||
expect.objectContaining({ | ||
id: "1", | ||
}), | ||
]) | ||
}) | ||
|
||
it("should list users by id", async () => { | ||
const users = await service.list({ | ||
id: ["1"], | ||
}) | ||
|
||
expect(users).toEqual([ | ||
expect.objectContaining({ | ||
id: "1", | ||
}), | ||
]) | ||
}) | ||
}) | ||
|
||
describe("listAndCountUsers", () => { | ||
it("should list and count users", async () => { | ||
const [users, count] = await service.listAndCount() | ||
const serialized = JSON.parse(JSON.stringify(users)) | ||
|
||
expect(count).toEqual(1) | ||
expect(serialized).toEqual([ | ||
expect.objectContaining({ | ||
id: "1", | ||
}), | ||
]) | ||
}) | ||
|
||
it("should listAndCount Users by id", async () => { | ||
const [Users, count] = await service.listAndCount({ | ||
id: "1", | ||
}) | ||
|
||
expect(count).toEqual(1) | ||
expect(Users).toEqual([ | ||
expect.objectContaining({ | ||
id: "1", | ||
}), | ||
]) | ||
}) | ||
}) | ||
|
||
describe("retrieveUser", () => { | ||
const id = "1" | ||
|
||
it("should return an user for the given id", async () => { | ||
const user = await service.retrieve(id) | ||
|
||
expect(user).toEqual( | ||
expect.objectContaining({ | ||
id, | ||
}) | ||
) | ||
}) | ||
|
||
it("should throw an error when an user with the given id does not exist", async () => { | ||
let error | ||
|
||
try { | ||
await service.retrieve("does-not-exist") | ||
} catch (e) { | ||
error = e | ||
} | ||
|
||
expect(error.message).toEqual( | ||
"User with id: does-not-exist was not found" | ||
) | ||
}) | ||
|
||
it("should throw an error when a userId is not provided", async () => { | ||
let error | ||
|
||
try { | ||
await service.retrieve(undefined as unknown as string) | ||
} catch (e) { | ||
error = e | ||
} | ||
|
||
expect(error.message).toEqual("user - id must be defined") | ||
}) | ||
|
||
it("should return user based on config select param", async () => { | ||
const User = await service.retrieve(id, { | ||
select: ["id"], | ||
}) | ||
|
||
const serialized = JSON.parse(JSON.stringify(User)) | ||
|
||
expect(serialized).toEqual({ | ||
id, | ||
}) | ||
}) | ||
}) | ||
|
||
describe("deleteUser", () => { | ||
const id = "1" | ||
|
||
it("should delete the Users given an id successfully", async () => { | ||
await service.delete([id]) | ||
|
||
const users = await service.list({ | ||
id: [id], | ||
}) | ||
|
||
expect(users).toHaveLength(0) | ||
}) | ||
}) | ||
|
||
describe("updateUser", () => { | ||
it("should throw an error when a id does not exist", async () => { | ||
let error | ||
|
||
try { | ||
await service.update([ | ||
{ | ||
id: "does-not-exist", | ||
}, | ||
]) | ||
} catch (e) { | ||
error = e | ||
} | ||
|
||
expect(error.message).toEqual('User with id "does-not-exist" not found') | ||
}) | ||
}) | ||
|
||
describe("createUser", () => { | ||
it("should create a User successfully", async () => { | ||
await service.create([ | ||
{ | ||
id: "2", | ||
}, | ||
]) | ||
|
||
const [User, count] = await service.listAndCount({ | ||
id: ["2"], | ||
}) | ||
|
||
expect(count).toEqual(1) | ||
expect(User[0]).toEqual( | ||
expect.objectContaining({ | ||
id: "2", | ||
}) | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.