feat: Fix subscribers loading + add order <> cart link (#7617)

This commit is contained in:
Oli Juhl
2024-06-09 12:31:28 +02:00
committed by GitHub
parent fbb00f3863
commit 3f661c917b
10 changed files with 99 additions and 76 deletions
@@ -1,12 +1,12 @@
import { OrderDTO } from "@medusajs/types" import { OrderDTO } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import { linkOrderAndPaymentCollectionsStep } from "../../../order/steps"
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session" import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps" import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
import { reserveInventoryStep } from "../steps/reserve-inventory" import { reserveInventoryStep } from "../steps/reserve-inventory"
@@ -75,16 +75,18 @@ export const completeCartWorkflow = createWorkflow(
const order = createOrderFromCartStep({ cart: finalCart }) const order = createOrderFromCartStep({ cart: finalCart })
const linkOrderPaymentCollection = transform({ order, cart }, (data) => ({ createRemoteLinkStep([
links: [ {
{ [Modules.ORDER]: { order_id: order.id },
order_id: data.order.id, [Modules.CART]: { cart_id: finalCart.id },
payment_collection_id: data.cart.payment_collection.id, },
{
[Modules.ORDER]: { order_id: order.id },
[Modules.PAYMENT]: {
payment_collection_id: cart.payment_collection.id,
}, },
], },
})) ])
linkOrderAndPaymentCollectionsStep(linkOrderPaymentCollection)
return order return order
} }
@@ -3,7 +3,6 @@ export * from "./cancel-orders"
export * from "./complete-orders" export * from "./complete-orders"
export * from "./create-orders" export * from "./create-orders"
export * from "./get-item-tax-lines" export * from "./get-item-tax-lines"
export * from "./link-order-payment-collection"
export * from "./register-fulfillment" export * from "./register-fulfillment"
export * from "./register-shipment" export * from "./register-shipment"
export * from "./set-tax-lines-for-items" export * from "./set-tax-lines-for-items"
@@ -1,42 +0,0 @@
import { Modules } from "@medusajs/modules-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type StepInput = {
links: {
order_id: string
payment_collection_id: string
}[]
}
export const linkOrderAndPaymentCollectionsStepId =
"link-order-payment-collection"
export const linkOrderAndPaymentCollectionsStep = createStep(
linkOrderAndPaymentCollectionsStepId,
async (data: StepInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = data.links.map((d) => ({
[Modules.ORDER]: { order_id: d.order_id },
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
}))
await remoteLink.create(links)
return new StepResponse(void 0, data)
},
async (data, { container }) => {
if (!data) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = data.links.map((d) => ({
[Modules.ORDER]: { order_id: d.order_id },
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
}))
await remoteLink.dismiss(links)
}
)
+6
View File
@@ -56,6 +56,12 @@ export const LINKS = {
Modules.PROMOTION, Modules.PROMOTION,
"promotion_id" "promotion_id"
), ),
OrderCart: composeLinkName(
Modules.ORDER,
"order_id",
Modules.CART,
"cart_id"
),
OrderSalesChannel: composeLinkName( OrderSalesChannel: composeLinkName(
Modules.ORDER, Modules.ORDER,
"order_id", "order_id",
@@ -1,11 +1,11 @@
import { completeCartWorkflow } from "@medusajs/core-flows" import { completeCartWorkflow } from "@medusajs/core-flows"
import { MedusaError } from "@medusajs/utils" import { MedusaError } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { prepareRetrieveQuery } from "../../../../../utils/get-query-config"
import { refetchOrder } from "../../../orders/helpers" import { refetchOrder } from "../../../orders/helpers"
import { refetchCart } from "../../helpers" import { refetchCart } from "../../helpers"
import { defaultStoreCartFields } from "../../query-config" import { defaultStoreCartFields } from "../../query-config"
import { StoreCompleteCartType } from "../../validators" import { StoreCompleteCartType } from "../../validators"
import { prepareRetrieveQuery } from "../../../../../utils/get-query-config"
export const POST = async ( export const POST = async (
req: MedusaRequest<StoreCompleteCartType>, req: MedusaRequest<StoreCompleteCartType>,
@@ -24,7 +24,7 @@ export class SubscriberLoader {
protected rootDir_: string protected rootDir_: string
protected excludes: RegExp[] = [ protected excludes: RegExp[] = [
/\.DS_Store/, /\.DS_Store/,
/(\.ts\.map|\.js\.map|\.d\.ts)/, /(\.ts\.map|\.js\.map|\.d\.ts|\.md)/,
/^_[^/\\]*(\.[^/\\]+)?$/, /^_[^/\\]*(\.[^/\\]+)?$/,
] ]
@@ -216,6 +216,7 @@ export class SubscriberLoader {
await readdir(this.rootDir_) await readdir(this.rootDir_)
hasSubscriberDir = true hasSubscriberDir = true
} catch (err) { } catch (err) {
logger.debug(`No subscriber directory found in ${this.rootDir_}`)
hasSubscriberDir = false hasSubscriberDir = false
} }
+10 -15
View File
@@ -1,10 +1,12 @@
import { createDefaultsWorkflow } from "@medusajs/core-flows" import { createDefaultsWorkflow } from "@medusajs/core-flows"
import { ConfigModule, MedusaContainer, PluginDetails } from "@medusajs/types" import { ConfigModule, MedusaContainer, PluginDetails } from "@medusajs/types"
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils" import {
ContainerRegistrationKeys,
createMedusaContainer,
promiseAll,
} from "@medusajs/utils"
import { asValue } from "awilix" import { asValue } from "awilix"
import { Express, NextFunction, Request, Response } from "express" import { Express, NextFunction, Request, Response } from "express"
import glob from "glob"
import { createMedusaContainer } from "@medusajs/utils"
import path from "path" import path from "path"
import requestIp from "request-ip" import requestIp from "request-ip"
import { v4 } from "uuid" import { v4 } from "uuid"
@@ -15,11 +17,11 @@ import expressLoader from "./express"
import featureFlagsLoader from "./feature-flags" import featureFlagsLoader from "./feature-flags"
import { registerWorkflows } from "./helpers/register-workflows" import { registerWorkflows } from "./helpers/register-workflows"
import { getResolvedPlugins } from "./helpers/resolve-plugins" import { getResolvedPlugins } from "./helpers/resolve-plugins"
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
import { SubscriberLoader } from "./helpers/subscribers" import { SubscriberLoader } from "./helpers/subscribers"
import Logger from "./logger" import Logger from "./logger"
import loadMedusaApp from "./medusa-app" import loadMedusaApp from "./medusa-app"
import registerPgConnection from "./pg-connection" import registerPgConnection from "./pg-connection"
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
type Options = { type Options = {
directory: string directory: string
@@ -47,17 +49,10 @@ async function subscribersLoader(
*/ */
await Promise.all( await Promise.all(
plugins.map(async (pluginDetails) => { plugins.map(async (pluginDetails) => {
const files = glob.sync( await new SubscriberLoader(
`${pluginDetails.resolve}/subscribers/*.{ts,js,mjs,mts}`, path.join(pluginDetails.resolve, "subscribers"),
{ container
ignore: ["**/*.d.ts", "**/*.map"], ).load()
}
)
return await Promise.all(
files.map(
async (file) => await new SubscriberLoader(file, container).load()
)
)
}) })
) )
} }
@@ -1,6 +1,8 @@
export * from "./cart-payment-collection" export * from "./cart-payment-collection"
export * from "./cart-promotion" export * from "./cart-promotion"
export * from "./fulfillment-set-location" export * from "./fulfillment-set-location"
export * from "./order-cart"
export * from "./order-fulfillment"
export * from "./order-payment-collection" export * from "./order-payment-collection"
export * from "./order-promotion" export * from "./order-promotion"
export * from "./product-sales-channel" export * from "./product-sales-channel"
@@ -8,8 +10,7 @@ export * from "./product-variant-inventory-item"
export * from "./product-variant-price-set" export * from "./product-variant-price-set"
export * from "./publishable-api-key-sales-channel" export * from "./publishable-api-key-sales-channel"
export * from "./readonly" export * from "./readonly"
export * from "./order-fulfillment"
export * from "./region-payment-provider" export * from "./region-payment-provider"
export * from "./sales-channel-location" export * from "./sales-channel-location"
export * from "./shipping-option-price-set" export * from "./shipping-option-price-set"
export * from "./order-fulfillment"
@@ -0,0 +1,61 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { LINKS } from "@medusajs/utils"
export const OrderCart: ModuleJoinerConfig = {
serviceName: LINKS.OrderCart,
isLink: true,
databaseConfig: {
tableName: "order_cart",
idPrefix: "ordercart",
},
alias: [
{
name: ["order_cart", "order_carts"],
args: {
entity: "LinkOrderCart",
},
},
],
primaryKeys: ["id", "order_id", "cart_id"],
relationships: [
{
serviceName: Modules.ORDER,
primaryKey: "id",
foreignKey: "order_id",
alias: "order",
},
{
serviceName: Modules.CART,
primaryKey: "id",
foreignKey: "cart_id",
alias: "cart",
},
],
extends: [
{
serviceName: Modules.ORDER,
fieldAlias: {
cart: "cart_link.cart",
},
relationship: {
serviceName: LINKS.OrderCart,
primaryKey: "order_id",
foreignKey: "id",
alias: "cart_link",
},
},
{
serviceName: Modules.CART,
fieldAlias: {
order: "order_link.order",
},
relationship: {
serviceName: LINKS.OrderCart,
primaryKey: "cart_id",
foreignKey: "id",
alias: "order_link",
},
},
],
}
@@ -5,12 +5,12 @@ import {
TransactionStep, TransactionStep,
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types" import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
import { InjectSharedContext, isString, MedusaContext } from "@medusajs/utils" import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils"
import { import {
type FlowRunOptions,
MedusaWorkflow, MedusaWorkflow,
resolveValue,
ReturnWorkflow, ReturnWorkflow,
resolveValue,
type FlowRunOptions,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { ulid } from "ulid" import { ulid } from "ulid"
import { InMemoryDistributedTransactionStorage } from "../utils" import { InMemoryDistributedTransactionStorage } from "../utils"