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:
@@ -30,6 +30,9 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { result, errors } = await createCampaigns.run({
|
||||
input: { campaignsData },
|
||||
throwOnError: false,
|
||||
context: {
|
||||
requestId: req.requestId,
|
||||
},
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
|
||||
@@ -13,9 +13,9 @@ import { asValue } from "awilix"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import { track } from "medusa-telemetry"
|
||||
import { EOL } from "os"
|
||||
import path from "path"
|
||||
import requestIp from "request-ip"
|
||||
import { Connection } from "typeorm"
|
||||
import { v4 } from "uuid"
|
||||
import { MedusaContainer } from "../types/global"
|
||||
import apiLoader from "./api"
|
||||
import loadConfig from "./config"
|
||||
@@ -192,7 +192,8 @@ export default async ({
|
||||
// Add the registered services to the request scope
|
||||
expressApp.use((req: Request, res: Response, next: NextFunction) => {
|
||||
container.register({ manager: asValue(dataSource.manager) })
|
||||
;(req as any).scope = container.createScope()
|
||||
req.scope = container.createScope() as MedusaContainer
|
||||
req.requestId = (req.headers["x-request-id"] as string) ?? v4()
|
||||
next()
|
||||
})
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ declare global {
|
||||
allowedProperties: string[]
|
||||
includes?: Record<string, boolean>
|
||||
errors: string[]
|
||||
requestId?: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { MedusaContainer } from "./global"
|
||||
export interface MedusaRequest extends Request {
|
||||
user?: (User | Customer) & { customer_id?: string; userId?: string }
|
||||
scope: MedusaContainer
|
||||
requestId?: string
|
||||
auth_user?: { id: string; app_metadata: Record<string, any>; scope: string }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
import { MedusaModule, RemoteQuery } from "@medusajs/modules-sdk"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
|
||||
function hasPagination(options: { [attr: string]: unknown }): boolean {
|
||||
if (!options) {
|
||||
return false
|
||||
}
|
||||
|
||||
const attrs = ["skip"]
|
||||
return Object.keys(options).some((key) => attrs.includes(key))
|
||||
}
|
||||
|
||||
function buildPagination(options, count) {
|
||||
return {
|
||||
skip: options.skip,
|
||||
take: options.take,
|
||||
count,
|
||||
}
|
||||
}
|
||||
|
||||
export function remoteQueryFetchData(container: MedusaContainer) {
|
||||
return async (expand, keyField, ids, relationship) => {
|
||||
const serviceConfig = expand.serviceConfig
|
||||
@@ -12,11 +29,35 @@ export function remoteQueryFetchData(container: MedusaContainer) {
|
||||
return
|
||||
}
|
||||
|
||||
const filters = {}
|
||||
let filters = {}
|
||||
const options = {
|
||||
...RemoteQuery.getAllFieldsAndRelations(expand),
|
||||
}
|
||||
|
||||
const availableOptions = [
|
||||
"skip",
|
||||
"take",
|
||||
"limit",
|
||||
"offset",
|
||||
"order",
|
||||
"sort",
|
||||
"withDeleted",
|
||||
]
|
||||
const availableOptionsAlias = new Map([
|
||||
["limit", "take"],
|
||||
["offset", "skip"],
|
||||
["sort", "order"],
|
||||
])
|
||||
for (const arg of expand.args || []) {
|
||||
if (arg.name === "filters" && arg.value) {
|
||||
filters = { ...arg.value }
|
||||
} else if (availableOptions.includes(arg.name)) {
|
||||
const argName = availableOptionsAlias.has(arg.name)
|
||||
? availableOptionsAlias.get(arg.name)
|
||||
: arg.name
|
||||
options[argName] = arg.value
|
||||
}
|
||||
}
|
||||
const expandRelations = Object.keys(expand.expands ?? {})
|
||||
|
||||
// filter out links from relations because TypeORM will throw if the relation doesn't exist
|
||||
@@ -33,11 +74,9 @@ export function remoteQueryFetchData(container: MedusaContainer) {
|
||||
filters[keyField] = ids
|
||||
}
|
||||
|
||||
const hasPagination = Object.keys(options).some((key) =>
|
||||
["skip"].includes(key)
|
||||
)
|
||||
const hasPagination_ = hasPagination(options)
|
||||
|
||||
let methodName = hasPagination ? "listAndCount" : "list"
|
||||
let methodName = hasPagination_ ? "listAndCount" : "list"
|
||||
|
||||
if (relationship?.args?.methodSuffix) {
|
||||
methodName += relationship.args.methodSuffix
|
||||
@@ -47,12 +86,12 @@ export function remoteQueryFetchData(container: MedusaContainer) {
|
||||
|
||||
const result = await service[methodName](filters, options)
|
||||
|
||||
if (hasPagination) {
|
||||
if (hasPagination_) {
|
||||
const [data, count] = result
|
||||
return {
|
||||
data: {
|
||||
rows: data,
|
||||
metadata: {},
|
||||
metadata: buildPagination(options, count),
|
||||
},
|
||||
path: "rows",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user