chore(order): link order and payment collection (#7334)

This commit is contained in:
Carlos R. L. Rodrigues
2024-05-14 20:37:27 +00:00
committed by GitHub
parent 7a8937fcba
commit 70fd355e46
12 changed files with 161 additions and 80 deletions
@@ -5,6 +5,7 @@ import {
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { linkOrderAndPaymentCollectionsStep } from "../../../order/steps"
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
import { reserveInventoryStep } from "../steps/reserve-inventory"
@@ -70,6 +71,19 @@ export const completeCartWorkflow = createWorkflow(
list: false,
}).config({ name: "final-cart" })
return createOrderFromCartStep({ cart: finalCart })
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,
},
],
}))
linkOrderAndPaymentCollectionsStep(linkOrderPaymentCollection)
return order
}
)
@@ -1,4 +1,5 @@
export * from "./create-orders"
export * from "./get-item-tax-lines"
export * from "./link-order-payment-collection"
export * from "./set-tax-lines-for-items"
export * from "./update-tax-lines"
@@ -0,0 +1,42 @@
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)
}
)
@@ -160,7 +160,7 @@ export class RemoteJoiner {
// add aliases
const isReadOnlyDefinition =
service.serviceName === undefined || service.isReadOnlyLink
!isDefined(service.serviceName) || service.isReadOnlyLink
if (!isReadOnlyDefinition) {
service.alias ??= []
@@ -354,7 +354,7 @@ export class RemoteJoiner {
uniqueIds = Array.from(new Set(uniqueIds.flat()))
}
uniqueIds = uniqueIds.filter((id) => id !== undefined)
uniqueIds = uniqueIds.filter((id) => isDefined(id))
}
if (relationship) {
@@ -478,7 +478,7 @@ export class RemoteJoiner {
const curPath: string[] = [BASE_PATH].concat(alias.location)
for (const prop of propPath) {
if (currentItems === undefined) {
if (!isDefined(currentItems)) {
break
}
@@ -500,7 +500,9 @@ export class RemoteJoiner {
}
} else {
locationItem[alias.property] = alias.isList
? [currentItems]
? isDefined(currentItems)
? [currentItems]
: []
: currentItems
}
@@ -645,18 +647,18 @@ export class RemoteJoiner {
if (Array.isArray(item[field])) {
item[relationship.alias] = item[field].map((id) => {
if (relationship.isList && !Array.isArray(relatedDataMap[id])) {
relatedDataMap[id] =
relatedDataMap[id] !== undefined ? [relatedDataMap[id]] : []
relatedDataMap[id] = isDefined(relatedDataMap[id])
? [relatedDataMap[id]]
: []
}
return relatedDataMap[id]
})
} else {
if (relationship.isList && !Array.isArray(relatedDataMap[itemKey])) {
relatedDataMap[itemKey] =
relatedDataMap[itemKey] !== undefined
? [relatedDataMap[itemKey]]
: []
relatedDataMap[itemKey] = isDefined(relatedDataMap[itemKey])
? [relatedDataMap[itemKey]]
: []
}
item[relationship.alias] = relatedDataMap[itemKey]
+6 -8
View File
@@ -68,18 +68,16 @@ export const LINKS = {
Modules.SALES_CHANNEL,
"sales_channel_id"
),
// Internal services
ProductShippingProfile: composeLinkName(
Modules.PRODUCT,
"variant_id",
"shippingProfileService",
"profile_id"
),
ProductSalesChannel: composeLinkName(
Modules.PRODUCT,
"product_id",
Modules.SALES_CHANNEL,
"sales_channel_id"
),
OrderPaymentCollection: composeLinkName(
Modules.ORDER,
"order_id",
Modules.PAYMENT,
"payment_collection_id"
),
}