chore: local workflow proxying methods to pass context (#6263)

What:
- When calling a module's method inside a Local Workflow the MedusaContext is passed as the last argument to the method if not provided
- Add `requestId` to req
- A couple of fixes on Remote Joiner and the data fetcher for internal services

Why:
- The context used to initialize the workflow has to be shared with all modules. properties like transactionId will be used to emit events and requestId to trace logs for example.
This commit is contained in:
Carlos R. L. Rodrigues
2024-02-01 13:37:26 +00:00
committed by GitHub
parent a2bf6756ac
commit 45134e4d11
40 changed files with 576 additions and 170 deletions
@@ -8,14 +8,14 @@ import {
} from "@medusajs/types"
import {
InjectEntityManager,
isDefined,
MedusaContext,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { DeepPartial, EntityManager, FindManyOptions, In } from "typeorm"
import { InventoryItem } from "../models"
import { getListQuery } from "../utils/query"
import { buildQuery } from "../utils/build-query"
import { getListQuery } from "../utils/query"
type InjectedDependencies = {
eventBusService: IEventBusService
@@ -47,7 +47,7 @@ export default class InventoryItemService {
async list(
selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryItemDTO[]> {
const queryBuilder = getListQuery(
context.transactionManager ?? this.manager_,
@@ -68,7 +68,7 @@ export default class InventoryItemService {
async retrieve(
inventoryItemId: string,
config: FindConfig<InventoryItem> = {},
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryItem> {
if (!isDefined(inventoryItemId)) {
throw new MedusaError(
@@ -102,7 +102,7 @@ export default class InventoryItemService {
async listAndCount(
selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> {
const queryBuilder = getListQuery(
context.transactionManager ?? this.manager_,
@@ -7,9 +7,9 @@ import {
} from "@medusajs/types"
import {
InjectEntityManager,
isDefined,
MedusaContext,
MedusaError,
isDefined,
} from "@medusajs/utils"
import { DeepPartial, EntityManager, FindManyOptions, In } from "typeorm"
import { InventoryLevel } from "../models"
@@ -46,7 +46,7 @@ export default class InventoryLevelService {
async list(
selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryLevel[]> {
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
@@ -65,7 +65,7 @@ export default class InventoryLevelService {
async listAndCount(
selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[InventoryLevel[], number]> {
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
@@ -85,7 +85,7 @@ export default class InventoryLevelService {
async retrieve(
inventoryLevelId: string,
config: FindConfig<InventoryLevel> = {},
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryLevel> {
if (!isDefined(inventoryLevelId)) {
throw new MedusaError(
@@ -319,7 +319,7 @@ export default class InventoryLevelService {
async getStockedQuantity(
inventoryItemId: string,
locationIds: string[] | string,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
@@ -348,7 +348,7 @@ export default class InventoryLevelService {
async getAvailableQuantity(
inventoryItemId: string,
locationIds: string[] | string,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
@@ -377,7 +377,7 @@ export default class InventoryLevelService {
async getReservedQuantity(
inventoryItemId: string,
locationIds: string[] | string,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
+11 -11
View File
@@ -74,7 +74,7 @@ export default class InventoryService implements IInventoryService {
async listInventoryItems(
selector: FilterableInventoryItemProps,
config: FindConfig<InventoryItemDTO> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> {
return await this.inventoryItemService_.listAndCount(
selector,
@@ -85,7 +85,7 @@ export default class InventoryService implements IInventoryService {
async list(
selector: FilterableInventoryItemProps,
config: FindConfig<InventoryItemDTO> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryItemDTO[]> {
return await this.inventoryItemService_.list(selector, config, context)
}
@@ -104,7 +104,7 @@ export default class InventoryService implements IInventoryService {
skip: 0,
take: 10,
},
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[InventoryLevelDTO[], number]> {
return await this.inventoryLevelService_.listAndCount(
selector,
@@ -127,7 +127,7 @@ export default class InventoryService implements IInventoryService {
skip: 0,
take: 10,
},
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[ReservationItemDTO[], number]> {
return await this.reservationItemService_.listAndCount(
selector,
@@ -146,7 +146,7 @@ export default class InventoryService implements IInventoryService {
async retrieveInventoryItem(
inventoryItemId: string,
config?: FindConfig<InventoryItemDTO>,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryItemDTO> {
const inventoryItem = await this.inventoryItemService_.retrieve(
inventoryItemId,
@@ -166,7 +166,7 @@ export default class InventoryService implements IInventoryService {
async retrieveInventoryLevel(
inventoryItemId: string,
locationId: string,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryLevelDTO> {
const [inventoryLevel] = await this.inventoryLevelService_.list(
{ inventory_item_id: inventoryItemId, location_id: locationId },
@@ -191,7 +191,7 @@ export default class InventoryService implements IInventoryService {
*/
async retrieveReservationItem(
reservationId: string,
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<ReservationItemDTO> {
return await this.reservationItemService_.retrieve(
reservationId,
@@ -202,7 +202,7 @@ export default class InventoryService implements IInventoryService {
private async ensureInventoryLevels(
data: { location_id: string; inventory_item_id: string }[],
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<InventoryLevelDTO[]> {
const inventoryLevels = await this.inventoryLevelService_.list(
{
@@ -629,7 +629,7 @@ export default class InventoryService implements IInventoryService {
async retrieveAvailableQuantity(
inventoryItemId: string,
locationIds: string[],
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -665,7 +665,7 @@ export default class InventoryService implements IInventoryService {
async retrieveStockedQuantity(
inventoryItemId: string,
locationIds: string[],
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -701,7 +701,7 @@ export default class InventoryService implements IInventoryService {
async retrieveReservedQuantity(
inventoryItemId: string,
locationIds: string[],
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -8,9 +8,9 @@ import {
} from "@medusajs/types"
import {
InjectEntityManager,
isDefined,
MedusaContext,
MedusaError,
isDefined,
promiseAll,
} from "@medusajs/utils"
import { EntityManager, FindManyOptions, In } from "typeorm"
@@ -55,7 +55,7 @@ export default class ReservationItemService {
async list(
selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<ReservationItem[]> {
const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem)
@@ -75,7 +75,7 @@ export default class ReservationItemService {
async listAndCount(
selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<[ReservationItem[], number]> {
const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem)
@@ -96,7 +96,7 @@ export default class ReservationItemService {
async retrieve(
reservationItemId: string,
config: FindConfig<ReservationItem> = {},
context: SharedContext = {}
@MedusaContext() context: SharedContext = {}
): Promise<ReservationItem> {
if (!isDefined(reservationItemId)) {
throw new MedusaError(