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

View File

@@ -1,12 +1,12 @@
import { OrderDTO } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { linkOrderAndPaymentCollectionsStep } from "../../../order/steps"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
import { reserveInventoryStep } from "../steps/reserve-inventory"
@@ -75,16 +75,18 @@ export const completeCartWorkflow = createWorkflow(
const order = createOrderFromCartStep({ cart: finalCart })
const linkOrderPaymentCollection = transform({ order, cart }, (data) => ({
links: [
{
order_id: data.order.id,
payment_collection_id: data.cart.payment_collection.id,
createRemoteLinkStep([
{
[Modules.ORDER]: { order_id: order.id },
[Modules.CART]: { cart_id: finalCart.id },
},
{
[Modules.ORDER]: { order_id: order.id },
[Modules.PAYMENT]: {
payment_collection_id: cart.payment_collection.id,
},
],
}))
linkOrderAndPaymentCollectionsStep(linkOrderPaymentCollection)
},
])
return order
}

View File

@@ -3,7 +3,6 @@ export * from "./cancel-orders"
export * from "./complete-orders"
export * from "./create-orders"
export * from "./get-item-tax-lines"
export * from "./link-order-payment-collection"
export * from "./register-fulfillment"
export * from "./register-shipment"
export * from "./set-tax-lines-for-items"

View File

@@ -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)
}
)

View File

@@ -56,6 +56,12 @@ export const LINKS = {
Modules.PROMOTION,
"promotion_id"
),
OrderCart: composeLinkName(
Modules.ORDER,
"order_id",
Modules.CART,
"cart_id"
),
OrderSalesChannel: composeLinkName(
Modules.ORDER,
"order_id",

View File

@@ -1,11 +1,11 @@
import { completeCartWorkflow } from "@medusajs/core-flows"
import { MedusaError } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { prepareRetrieveQuery } from "../../../../../utils/get-query-config"
import { refetchOrder } from "../../../orders/helpers"
import { refetchCart } from "../../helpers"
import { defaultStoreCartFields } from "../../query-config"
import { StoreCompleteCartType } from "../../validators"
import { prepareRetrieveQuery } from "../../../../../utils/get-query-config"
export const POST = async (
req: MedusaRequest<StoreCompleteCartType>,

View File

@@ -24,7 +24,7 @@ export class SubscriberLoader {
protected rootDir_: string
protected excludes: RegExp[] = [
/\.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_)
hasSubscriberDir = true
} catch (err) {
logger.debug(`No subscriber directory found in ${this.rootDir_}`)
hasSubscriberDir = false
}

View File

@@ -1,10 +1,12 @@
import { createDefaultsWorkflow } from "@medusajs/core-flows"
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 { Express, NextFunction, Request, Response } from "express"
import glob from "glob"
import { createMedusaContainer } from "@medusajs/utils"
import path from "path"
import requestIp from "request-ip"
import { v4 } from "uuid"
@@ -15,11 +17,11 @@ import expressLoader from "./express"
import featureFlagsLoader from "./feature-flags"
import { registerWorkflows } from "./helpers/register-workflows"
import { getResolvedPlugins } from "./helpers/resolve-plugins"
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
import { SubscriberLoader } from "./helpers/subscribers"
import Logger from "./logger"
import loadMedusaApp from "./medusa-app"
import registerPgConnection from "./pg-connection"
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
type Options = {
directory: string
@@ -47,17 +49,10 @@ async function subscribersLoader(
*/
await Promise.all(
plugins.map(async (pluginDetails) => {
const files = glob.sync(
`${pluginDetails.resolve}/subscribers/*.{ts,js,mjs,mts}`,
{
ignore: ["**/*.d.ts", "**/*.map"],
}
)
return await Promise.all(
files.map(
async (file) => await new SubscriberLoader(file, container).load()
)
)
await new SubscriberLoader(
path.join(pluginDetails.resolve, "subscribers"),
container
).load()
})
)
}

View File

@@ -1,6 +1,8 @@
export * from "./cart-payment-collection"
export * from "./cart-promotion"
export * from "./fulfillment-set-location"
export * from "./order-cart"
export * from "./order-fulfillment"
export * from "./order-payment-collection"
export * from "./order-promotion"
export * from "./product-sales-channel"
@@ -8,8 +10,7 @@ export * from "./product-variant-inventory-item"
export * from "./product-variant-price-set"
export * from "./publishable-api-key-sales-channel"
export * from "./readonly"
export * from "./order-fulfillment"
export * from "./region-payment-provider"
export * from "./sales-channel-location"
export * from "./shipping-option-price-set"
export * from "./order-fulfillment"

View File

@@ -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",
},
},
],
}

View File

@@ -5,12 +5,12 @@ import {
TransactionStep,
} from "@medusajs/orchestration"
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
import { InjectSharedContext, isString, MedusaContext } from "@medusajs/utils"
import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils"
import {
type FlowRunOptions,
MedusaWorkflow,
resolveValue,
ReturnWorkflow,
resolveValue,
type FlowRunOptions,
} from "@medusajs/workflows-sdk"
import { ulid } from "ulid"
import { InMemoryDistributedTransactionStorage } from "../utils"