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

This commit is contained in:
Carlos R. L. Rodrigues
2024-05-14 17:37:27 -03:00
committed by GitHub
parent 7a8937fcba
commit 70fd355e46
12 changed files with 161 additions and 80 deletions

View File

@@ -1897,6 +1897,23 @@ medusaIntegrationTestRunner({
line_item_id: cart.items[0].id,
})
)
const fullOrder = await api.get(
`/admin/orders/${response.data.order.id}`,
adminHeaders
)
const fullOrderDetail = fullOrder.data.order
expect(fullOrderDetail).toEqual(
expect.objectContaining({
payment_collections: [
expect.objectContaining({
currency_code: "usd",
amount: 106,
}),
],
})
)
})
it("should throw an error when payment collection isn't created", async () => {

View File

@@ -154,6 +154,7 @@ medusaIntegrationTestRunner({
status: "pending",
version: 1,
display_id: 1,
payment_collections: [],
summary: expect.objectContaining({
// TODO: add all summary fields
}),

View File

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

View File

@@ -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"

View File

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

View File

@@ -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]

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"
),
}

View File

@@ -47,6 +47,7 @@ export const defaultAdminRetrieveOrderFields = [
"*shipping_methods",
"*shipping_methods.tax_lines",
"*shipping_methods.adjustments",
"*payment_collections",
]
export const retrieveTransformQueryConfig = {

View File

@@ -48,6 +48,7 @@ export const defaultStoreRetrieveOrderFields = [
"*shipping_methods",
"*shipping_methods.tax_lines",
"*shipping_methods.adjustments",
"*payment_collections",
]
export const retrieveTransformQueryConfig = {

View File

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

View File

@@ -0,0 +1,64 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { LINKS } from "@medusajs/utils"
export const OrderPaymentCollection: ModuleJoinerConfig = {
serviceName: LINKS.OrderPaymentCollection,
isLink: true,
databaseConfig: {
tableName: "order_payment_collection",
idPrefix: "capaycol",
},
alias: [
{
name: ["order_payment_collection", "order_payment_collections"],
args: {
entity: "LinkOrderPaymentCollection",
},
},
],
primaryKeys: ["id", "order_id", "payment_collection_id"],
relationships: [
{
serviceName: Modules.ORDER,
primaryKey: "id",
foreignKey: "order_id",
alias: "order",
},
{
serviceName: Modules.PAYMENT,
primaryKey: "id",
foreignKey: "payment_collection_id",
alias: "payment_collection",
},
],
extends: [
{
serviceName: Modules.ORDER,
fieldAlias: {
payment_collections: {
path: "payment_collections_link.payment_collection",
isList: true,
},
},
relationship: {
serviceName: LINKS.OrderPaymentCollection,
primaryKey: "order_id",
foreignKey: "id",
alias: "payment_collections_link",
},
},
{
serviceName: Modules.PAYMENT,
fieldAlias: {
order: "order_link.order",
},
relationship: {
serviceName: LINKS.OrderPaymentCollection,
primaryKey: "payment_collection_id",
foreignKey: "id",
alias: "order_link",
},
},
],
}

View File

@@ -1,60 +0,0 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { LINKS } from "@medusajs/utils"
export const ProductShippingProfile: ModuleJoinerConfig = {
serviceName: LINKS.ProductShippingProfile,
isLink: true,
databaseConfig: {
tableName: "product_shipping_profile",
idPrefix: "psprof",
},
alias: [
{
name: "product_shipping_profile",
args: {
entity: "LinkProductShippingProfile",
},
},
],
primaryKeys: ["id", "product_id", "profile_id"],
relationships: [
{
serviceName: Modules.PRODUCT,
primaryKey: "id",
foreignKey: "product_id",
alias: "product",
},
{
serviceName: "shippingProfileService",
isInternalService: true,
primaryKey: "id",
foreignKey: "profile_id",
alias: "profile",
},
],
extends: [
{
serviceName: Modules.PRODUCT,
fieldAlias: {
profile: "shipping_profile.profile",
},
relationship: {
serviceName: LINKS.ProductShippingProfile,
primaryKey: "product_id",
foreignKey: "id",
alias: "shipping_profile",
},
},
{
serviceName: "shippingProfileService",
relationship: {
serviceName: LINKS.ProductShippingProfile,
isInternalService: true,
primaryKey: "profile_id",
foreignKey: "id",
alias: "product_link",
},
},
],
}