feat(modules-sdk, types, user, utils):init user module events (#6431)

* init user module events

* refactor utils

* undo test script update

* fix feedback

* add eventbus service to module test-runner

* add injected dependencies

* move events to utils

* use const eventname in tests

* rm withTransaction
This commit is contained in:
Philip Korsholm
2024-02-23 09:31:02 +08:00
committed by GitHub
parent 36a61658f9
commit 3fc2aea752
19 changed files with 289 additions and 11 deletions
@@ -1,7 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createInvites } from "../../../__fixtures__/invite"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
@@ -44,6 +46,7 @@ describe("UserModuleService - Invite", () => {
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
jest.clearAllMocks()
})
afterEach(async () => {
@@ -171,6 +174,30 @@ describe("UserModuleService - Invite", () => {
expect(error.message).toEqual('Invite with id "does-not-exist" not found')
})
it("should emit invite updated events", async () => {
await createInvites(testManager, defaultInviteData)
jest.clearAllMocks()
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.updateInvites([
{
id: "1",
accepted: true,
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_updated,
}),
])
})
})
describe("createInvitie", () => {
@@ -188,5 +215,26 @@ describe("UserModuleService - Invite", () => {
})
)
})
it("should emit invite created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createInvites(defaultInviteData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_created,
}),
])
})
})
})
@@ -1,7 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createUsers } from "../../../__fixtures__/user"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
@@ -41,6 +43,7 @@ describe("UserModuleService - User", () => {
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
jest.clearAllMocks()
})
afterAll(async () => {
@@ -182,6 +185,30 @@ describe("UserModuleService - User", () => {
expect(error.message).toEqual('User with id "does-not-exist" not found')
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
jest.clearAllMocks()
await service.update([
{
id: "1",
first_name: "John",
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.updated,
}),
])
})
})
describe("create", () => {
@@ -199,5 +226,26 @@ describe("UserModuleService - User", () => {
})
)
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.created,
}),
])
})
})
})
@@ -1,6 +1,7 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { DB_URL } from "./database"
import { MockEventBusService } from "medusa-test-utils"
export function getInitModuleConfig() {
const moduleOptions = {
@@ -13,7 +14,9 @@ export function getInitModuleConfig() {
jwt_secret: "test",
}
const injectedDependencies = {}
const injectedDependencies = {
eventBusModuleService: new MockEventBusService(),
}
const modulesConfig_ = {
[Modules.USER]: {
+63 -6
View File
@@ -5,13 +5,17 @@ import {
ModuleJoinerConfig,
UserTypes,
ModulesSdkTypes,
IEventBusModuleService,
} from "@medusajs/types"
import {
InjectManager,
EmitEvents,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
InjectManager,
buildEventMessages,
CommonEvents,
UserEvents,
} from "@medusajs/utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
@@ -22,6 +26,7 @@ type InjectedDependencies = {
baseRepository: DAL.RepositoryService
userService: ModulesSdkTypes.InternalModuleService<any>
inviteService: InviteService<any>
eventBusModuleService: IEventBusModuleService
}
const generateMethodForModels = [Invite]
@@ -81,7 +86,8 @@ export default class UserModuleService<
sharedContext?: Context
): Promise<UserTypes.UserDTO>
@InjectTransactionManager("baseRepository_")
@InjectManager("baseRepository_")
@EmitEvents()
async create(
data: UserTypes.CreateUserDTO[] | UserTypes.CreateUserDTO,
@MedusaContext() sharedContext: Context = {}
@@ -96,6 +102,18 @@ export default class UserModuleService<
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
users.map((user) => ({
eventName: UserEvents.created,
metadata: {
service: this.constructor.name,
action: CommonEvents.CREATED,
object: "user",
},
data: { id: user.id },
}))
)
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
}
@@ -108,7 +126,8 @@ export default class UserModuleService<
sharedContext?: Context
): Promise<UserTypes.UserDTO>
@InjectTransactionManager("baseRepository_")
@InjectManager("baseRepository_")
@EmitEvents()
async update(
data: UserTypes.UpdateUserDTO | UserTypes.UpdateUserDTO[],
@MedusaContext() sharedContext: Context = {}
@@ -123,6 +142,18 @@ export default class UserModuleService<
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
updatedUsers.map((user) => ({
eventName: UserEvents.updated,
metadata: {
service: this.constructor.name,
action: CommonEvents.UPDATED,
object: "user",
},
data: { id: user.id },
}))
)
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
}
@@ -135,7 +166,8 @@ export default class UserModuleService<
sharedContext?: Context
): Promise<UserTypes.InviteDTO>
@InjectTransactionManager("baseRepository_")
@InjectManager("baseRepository_")
@EmitEvents()
async createInvites(
data: UserTypes.CreateInviteDTO[] | UserTypes.CreateInviteDTO,
@MedusaContext() sharedContext: Context = {}
@@ -150,6 +182,18 @@ export default class UserModuleService<
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
invites.map((invite) => ({
eventName: UserEvents.invite_created,
metadata: {
service: this.constructor.name,
action: CommonEvents.CREATED,
object: "invite",
},
data: { id: invite.id },
}))
)
return Array.isArray(data) ? serializedInvites : serializedInvites[0]
}
@@ -178,7 +222,8 @@ export default class UserModuleService<
sharedContext?: Context
): Promise<UserTypes.InviteDTO>
@InjectTransactionManager("baseRepository_")
@InjectManager("baseRepository_")
@EmitEvents()
async updateInvites(
data: UserTypes.UpdateInviteDTO | UserTypes.UpdateInviteDTO[],
@MedusaContext() sharedContext: Context = {}
@@ -196,6 +241,18 @@ export default class UserModuleService<
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
serializedInvites.map((invite) => ({
eventName: UserEvents.invite_updated,
metadata: {
service: this.constructor.name,
action: CommonEvents.UPDATED,
object: "invite",
},
data: { id: invite.id },
}))
)
return Array.isArray(data) ? serializedInvites : serializedInvites[0]
}
}