feat(user, types): add invite and user properties + migration (#6327)

**What**
- Add invite model 
- Populate user model
This commit is contained in:
Philip Korsholm
2024-02-14 13:58:14 +00:00
committed by GitHub
parent 16927469eb
commit 4d51f095b3
45 changed files with 1751 additions and 100 deletions
@@ -0,0 +1,192 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createInvites } from "../../../__fixtures__/invite"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
jest.setTimeout(30000)
const today = new Date()
const expireDate = new Date(today.setDate(today.getDate() + 10))
const defaultInviteData = [
{
id: "1",
email: "user_1@test.com",
token: "test",
expires_at: expireDate,
},
{
id: "2",
email: "user_2@test.com",
token: "test",
expires_at: expireDate,
},
]
describe("UserModuleService - Invite", () => {
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()
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
afterAll(async () => {
await shutdownFunc()
})
describe("listInvites", () => {
it("should list invites", async () => {
await createInvites(testManager, defaultInviteData)
const invites = await service.listInvites()
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list invites by id", async () => {
await createInvites(testManager, defaultInviteData)
const invites = await service.listInvites({
id: ["1"],
})
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("listAndCountInvites", () => {
it("should list and count invites", async () => {
await createInvites(testManager, defaultInviteData)
const [invites, count] = await service.listAndCountInvites()
expect(count).toEqual(2)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should listAndCount invites by id", async () => {
await createInvites(testManager, defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: "1",
})
expect(count).toEqual(1)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieveInvite", () => {
const id = "1"
it("should return an invite for the given id", async () => {
await createInvites(testManager, defaultInviteData)
const invite = await service.retrieveInvite(id)
expect(invite).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when an invite with the given id does not exist", async () => {
const error = await service
.retrieveInvite("does-not-exist")
.catch((e) => e)
expect(error.message).toEqual(
"Invite with id: does-not-exist was not found"
)
})
it("should throw an error when inviteId is not provided", async () => {
const error = await service
.retrieveInvite(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("invite - id must be defined")
})
it("should return invite based on config select param", async () => {
await createInvites(testManager, defaultInviteData)
const invite = await service.retrieveInvite(id, {
select: ["id"],
})
expect(invite).toEqual({
id,
})
})
})
describe("updateInvite", () => {
it("should throw an error when an id does not exist", async () => {
const error = await service
.updateInvites([
{
id: "does-not-exist",
},
])
.catch((e) => e)
expect(error.message).toEqual('Invite with id "does-not-exist" not found')
})
})
describe("createInvitie", () => {
it("should create an invite successfully", async () => {
await service.createInvites(defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: ["1"],
})
expect(count).toEqual(1)
expect(invites[0]).toEqual(
expect.objectContaining({
id: "1",
})
)
})
})
})
@@ -8,6 +8,17 @@ import { initModules } from "medusa-test-utils"
jest.setTimeout(30000)
const defaultUserData = [
{
id: "1",
email: "user_1@test.com",
},
{
id: "2",
email: "user_2@test.com",
},
]
describe("UserModuleService - User", () => {
let service: IUserModuleService
let testManager: SqlEntityManager
@@ -26,8 +37,6 @@ describe("UserModuleService - User", () => {
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
await createUsers(testManager)
})
afterEach(async () => {
@@ -38,19 +47,24 @@ describe("UserModuleService - User", () => {
await shutdownFunc()
})
describe("listUsers", () => {
describe("list", () => {
it("should list users", async () => {
const users = await service.list()
const serialized = JSON.parse(JSON.stringify(users))
await createUsers(testManager, defaultUserData)
expect(serialized).toEqual([
const users = await service.list()
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list users by id", async () => {
await createUsers(testManager, defaultUserData)
const users = await service.list({
id: ["1"],
})
@@ -63,20 +77,24 @@ describe("UserModuleService - User", () => {
})
})
describe("listAndCountUsers", () => {
describe("listAndCount", () => {
it("should list and count users", async () => {
await createUsers(testManager, defaultUserData)
const [users, count] = await service.listAndCount()
const serialized = JSON.parse(JSON.stringify(users))
expect(count).toEqual(1)
expect(serialized).toEqual([
expect(count).toEqual(2)
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should listAndCount Users by id", async () => {
it("should list and count users by id", async () => {
await createUsers(testManager, defaultUserData)
const [Users, count] = await service.listAndCount({
id: "1",
})
@@ -90,10 +108,12 @@ describe("UserModuleService - User", () => {
})
})
describe("retrieveUser", () => {
describe("retrieve", () => {
const id = "1"
it("should return an user for the given id", async () => {
await createUsers(testManager, defaultUserData)
const user = await service.retrieve(id)
expect(user).toEqual(
@@ -104,13 +124,7 @@ describe("UserModuleService - User", () => {
})
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
}
const error = await service.retrieve("does-not-exist").catch((e) => e)
expect(error.message).toEqual(
"User with id: does-not-exist was not found"
@@ -118,18 +132,16 @@ describe("UserModuleService - User", () => {
})
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
}
const error = await service
.retrieve(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("user - id must be defined")
})
it("should return user based on config select param", async () => {
await createUsers(testManager, defaultUserData)
const User = await service.retrieve(id, {
select: ["id"],
})
@@ -142,10 +154,12 @@ describe("UserModuleService - User", () => {
})
})
describe("deleteUser", () => {
describe("delete", () => {
const id = "1"
it("should delete the Users given an id successfully", async () => {
it("should delete the users given an id successfully", async () => {
await createUsers(testManager, defaultUserData)
await service.delete([id])
const users = await service.list({
@@ -156,40 +170,32 @@ describe("UserModuleService - User", () => {
})
})
describe("updateUser", () => {
describe("update", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
const error = await service
.update([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
.catch((e) => 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",
},
])
describe("create", () => {
it("should create a user successfully", async () => {
await service.create(defaultUserData)
const [User, count] = await service.listAndCount({
id: ["2"],
id: ["1"],
})
expect(count).toEqual(1)
expect(User[0]).toEqual(
expect.objectContaining({
id: "2",
id: "1",
})
)
})