fix(medusa): Bulk create variant + pass transaction to the inventory service context methods (#3835)

* fix(medusa): Bulk create variant and pass transaction where needed

* Create fair-penguins-stare.md

* fix unit tests

* event

* transaction orchestration

* revert options

* Prevent isolated module to use the given transaction if any in the exposed service

* Use enum

* remove changeset to re do it

* Create thick-ants-tickle.md

* update event bus local

* remove changeset to re do it

* Create thick-kings-wonder.md

* remove changeset to re do it

* Create slimy-bees-eat.md

* Update packages/utils/src/event-bus/index.ts

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>

---------

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2023-04-19 09:27:48 +02:00
committed by GitHub
co-authored by Oliver Windall Juhl Philip Korsholm
parent 366b12fcea
commit af710f1b48
17 changed files with 504 additions and 275 deletions
@@ -1,7 +1,8 @@
import { SharedContext } from "@medusajs/types"
export function InjectEntityManager(
managerProperty = "manager_"
shouldForceTransaction: (target: any) => boolean = () => false,
managerProperty: string = "manager_"
): MethodDecorator {
return function (
target: any,
@@ -18,9 +19,10 @@ export function InjectEntityManager(
const argIndex = target.MedusaContextIndex_[propertyKey]
descriptor.value = async function (...args: any[]) {
const shouldForceTransactionRes = shouldForceTransaction(target)
const context: SharedContext = args[argIndex] ?? {}
if (context?.transactionManager) {
if (!shouldForceTransactionRes && context?.transactionManager) {
return await originalMethod.apply(this, args)
}
+37 -19
View File
@@ -23,24 +23,15 @@ export abstract class AbstractEventBusModuleService
): Promise<void>
abstract emit<T>(data: EventBusTypes.EmitData<T>[]): Promise<void>
public subscribe(
eventName: string | symbol,
subscriber: EventBusTypes.Subscriber,
context?: EventBusTypes.SubscriberContext
): this {
if (typeof subscriber !== `function`) {
throw new Error("Subscriber must be a function")
}
/**
* If context is provided, we use the subscriberId from it
* otherwise we generate a random using a ulid
*/
const randId = ulid()
const event = eventName.toString()
const subscriberId = context?.subscriberId ?? `${event}-${randId}`
protected storeSubscribers({
event,
subscriberId,
subscriber,
}: {
event: string | symbol
subscriberId: string
subscriber: EventBusTypes.Subscriber
}) {
const newSubscriberDescriptor = { subscriber, id: subscriberId }
const existingSubscribers = this.eventToSubscribersMap_.get(event) ?? []
@@ -57,6 +48,33 @@ export abstract class AbstractEventBusModuleService
...existingSubscribers,
newSubscriberDescriptor,
])
}
public retrieveSubscribers(event: string | symbol) {
return this.eventToSubscribersMap_.get(event)
}
public subscribe(
eventName: string | symbol,
subscriber: EventBusTypes.Subscriber,
context?: EventBusTypes.SubscriberContext
): this {
if (typeof subscriber !== `function`) {
throw new Error("Subscriber must be a function")
}
/**
* If context is provided, we use the subscriberId from it
* otherwise we generate a random using a ulid
*/
const randId = ulid()
const event = eventName.toString()
this.storeSubscribers({
event,
subscriberId: context?.subscriberId ?? `${event}-${randId}`,
subscriber,
})
return this
}
@@ -70,7 +88,7 @@ export abstract class AbstractEventBusModuleService
throw new Error("Subscriber must be a function")
}
const existingSubscribers = this.eventToSubscribersMap_.get(eventName)
const existingSubscribers = this.retrieveSubscribers(eventName)
if (existingSubscribers?.length) {
const subIndex = existingSubscribers?.findIndex(