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:
@@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user