chore: Treat internal event differently, primarely do not display info logs for those events (#8767)
* chore: Treat internal event differently, primarely do not display info log for those events * revert doc * add few tests * only set internal option if present * revert to previous condition * start including feedback after discussion * include feedback * fix modules integration tests * fix modules integration tests * fix event bus local
This commit is contained in:
committed by
GitHub
parent
6cfe9bd874
commit
5bec38538a
@@ -0,0 +1,96 @@
|
||||
import { EmitEvents } from "../emit-events"
|
||||
import { MedusaContext } from "../context-parameter"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { InjectSharedContext } from "../inject-shared-context"
|
||||
|
||||
describe("EmitEvents", () => {
|
||||
it(`should call the emit event method from the base service including the messages and their options`, async () => {
|
||||
const mock = jest.fn()
|
||||
|
||||
class FakeService {
|
||||
async emitEvents_(...args) {
|
||||
return mock(...args)
|
||||
}
|
||||
|
||||
@InjectSharedContext()
|
||||
@EmitEvents({ internal: true })
|
||||
async method(@MedusaContext() sharedContext: Context = {}) {
|
||||
sharedContext.messageAggregator?.saveRawMessageData({
|
||||
data: { id: 1 },
|
||||
object: "test",
|
||||
action: "create",
|
||||
source: "test",
|
||||
eventName: "test-event",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const service = new FakeService()
|
||||
await service.method()
|
||||
|
||||
expect(mock).toHaveBeenCalledTimes(1)
|
||||
expect(mock).toHaveBeenCalledWith({
|
||||
default: [
|
||||
{
|
||||
name: "test-event",
|
||||
metadata: {
|
||||
source: "test",
|
||||
object: "test",
|
||||
action: "create",
|
||||
},
|
||||
data: {
|
||||
id: 1,
|
||||
},
|
||||
options: {
|
||||
internal: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it(`should call the emit event method from the base service with grouped messages`, async () => {
|
||||
const mock = jest.fn()
|
||||
|
||||
class FakeService {
|
||||
async emitEvents_(...args) {
|
||||
return mock(...args)
|
||||
}
|
||||
|
||||
@InjectSharedContext()
|
||||
@EmitEvents({ internal: true, groupBy: ["name"] })
|
||||
async method(@MedusaContext() sharedContext: Context = {}) {
|
||||
sharedContext.messageAggregator?.saveRawMessageData({
|
||||
data: { id: 1 },
|
||||
object: "test",
|
||||
action: "create",
|
||||
source: "test",
|
||||
eventName: "test-event",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const service = new FakeService()
|
||||
await service.method()
|
||||
|
||||
expect(mock).toHaveBeenCalledTimes(1)
|
||||
expect(mock).toHaveBeenCalledWith({
|
||||
"test-event": [
|
||||
{
|
||||
name: "test-event",
|
||||
metadata: {
|
||||
source: "test",
|
||||
object: "test",
|
||||
action: "create",
|
||||
},
|
||||
data: {
|
||||
id: 1,
|
||||
},
|
||||
options: {
|
||||
internal: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,6 +2,12 @@ import { MessageAggregator } from "../../event-bus"
|
||||
import { InjectIntoContext } from "./inject-into-context"
|
||||
import { MessageAggregatorFormat } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* @internal this decorator is not meant to be used except by the internal team for now
|
||||
*
|
||||
* @param options
|
||||
* @constructor
|
||||
*/
|
||||
export function EmitEvents(
|
||||
options: MessageAggregatorFormat = {} as MessageAggregatorFormat
|
||||
) {
|
||||
|
||||
@@ -11,11 +11,11 @@ import {
|
||||
SoftDeleteReturn,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
MapToConfig,
|
||||
isString,
|
||||
kebabCase,
|
||||
lowerCaseFirst,
|
||||
mapObjectTo,
|
||||
MapToConfig,
|
||||
pluralize,
|
||||
upperCaseFirst,
|
||||
} from "../common"
|
||||
@@ -271,7 +271,10 @@ export function MedusaService<
|
||||
? { id: primaryKeyValue }
|
||||
: primaryKeyValue,
|
||||
metadata: { source: "", action: "", object: "" },
|
||||
}))
|
||||
})),
|
||||
{
|
||||
internal: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -302,7 +305,10 @@ export function MedusaService<
|
||||
name: `${kebabCase(modelName)}.deleted`,
|
||||
metadata: { source: "", action: "", object: "" },
|
||||
data: { id },
|
||||
}))
|
||||
})),
|
||||
{
|
||||
internal: true,
|
||||
}
|
||||
)
|
||||
|
||||
// Map internal table/column names to their respective external linkable keys
|
||||
@@ -392,6 +398,11 @@ export function MedusaService<
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal this method is not meant to be used except by the internal team for now
|
||||
* @param groupedEvents
|
||||
* @protected
|
||||
*/
|
||||
protected async emitEvents_(groupedEvents) {
|
||||
if (!this.eventBusModuleService_ || !groupedEvents) {
|
||||
return
|
||||
@@ -399,7 +410,11 @@ export function MedusaService<
|
||||
|
||||
const promises: Promise<void>[] = []
|
||||
for (const group of Object.keys(groupedEvents)) {
|
||||
promises.push(this.eventBusModuleService_.emit(groupedEvents[group]))
|
||||
promises.push(
|
||||
this.eventBusModuleService_.emit(groupedEvents[group], {
|
||||
internal: true,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
Reference in New Issue
Block a user