From 70fd355e46701f45d2b53b0ae1e8728232de7946 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 14 May 2024 17:37:27 -0300 Subject: [PATCH] chore(order): link order and payment collection (#7334) --- .../__tests__/cart/store/carts.spec.ts | 17 +++++ .../modules/__tests__/order/order.spec.ts | 1 + .../cart/workflows/complete-cart.ts | 16 ++++- .../core/core-flows/src/order/steps/index.ts | 1 + .../steps/link-order-payment-collection.ts | 42 ++++++++++++ .../orchestration/src/joiner/remote-joiner.ts | 22 ++++--- packages/core/utils/src/link/links.ts | 14 ++-- .../src/api-v2/admin/orders/query-config.ts | 1 + .../src/api-v2/store/orders/query-config.ts | 1 + .../link-modules/src/definitions/index.ts | 2 +- .../definitions/order-payment-collection.ts | 64 +++++++++++++++++++ .../definitions/product-shipping-profile.ts | 60 ----------------- 12 files changed, 161 insertions(+), 80 deletions(-) create mode 100644 packages/core/core-flows/src/order/steps/link-order-payment-collection.ts create mode 100644 packages/modules/link-modules/src/definitions/order-payment-collection.ts delete mode 100644 packages/modules/link-modules/src/definitions/product-shipping-profile.ts diff --git a/integration-tests/modules/__tests__/cart/store/carts.spec.ts b/integration-tests/modules/__tests__/cart/store/carts.spec.ts index f7647c4286..55ececc285 100644 --- a/integration-tests/modules/__tests__/cart/store/carts.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/carts.spec.ts @@ -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 () => { diff --git a/integration-tests/modules/__tests__/order/order.spec.ts b/integration-tests/modules/__tests__/order/order.spec.ts index 8fef73445a..6dd029f77f 100644 --- a/integration-tests/modules/__tests__/order/order.spec.ts +++ b/integration-tests/modules/__tests__/order/order.spec.ts @@ -154,6 +154,7 @@ medusaIntegrationTestRunner({ status: "pending", version: 1, display_id: 1, + payment_collections: [], summary: expect.objectContaining({ // TODO: add all summary fields }), diff --git a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts index 94a0330528..f9453fc2c3 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts @@ -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 } ) diff --git a/packages/core/core-flows/src/order/steps/index.ts b/packages/core/core-flows/src/order/steps/index.ts index 5694ff1539..dd30b3c0ca 100644 --- a/packages/core/core-flows/src/order/steps/index.ts +++ b/packages/core/core-flows/src/order/steps/index.ts @@ -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" diff --git a/packages/core/core-flows/src/order/steps/link-order-payment-collection.ts b/packages/core/core-flows/src/order/steps/link-order-payment-collection.ts new file mode 100644 index 0000000000..88df681e77 --- /dev/null +++ b/packages/core/core-flows/src/order/steps/link-order-payment-collection.ts @@ -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) + } +) diff --git a/packages/core/orchestration/src/joiner/remote-joiner.ts b/packages/core/orchestration/src/joiner/remote-joiner.ts index d6935f7eea..c0714a3228 100644 --- a/packages/core/orchestration/src/joiner/remote-joiner.ts +++ b/packages/core/orchestration/src/joiner/remote-joiner.ts @@ -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] diff --git a/packages/core/utils/src/link/links.ts b/packages/core/utils/src/link/links.ts index 7057d8c5a8..745da8aa52 100644 --- a/packages/core/utils/src/link/links.ts +++ b/packages/core/utils/src/link/links.ts @@ -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" + ), } diff --git a/packages/medusa/src/api-v2/admin/orders/query-config.ts b/packages/medusa/src/api-v2/admin/orders/query-config.ts index 7f8d5e1953..df08f01b4f 100644 --- a/packages/medusa/src/api-v2/admin/orders/query-config.ts +++ b/packages/medusa/src/api-v2/admin/orders/query-config.ts @@ -47,6 +47,7 @@ export const defaultAdminRetrieveOrderFields = [ "*shipping_methods", "*shipping_methods.tax_lines", "*shipping_methods.adjustments", + "*payment_collections", ] export const retrieveTransformQueryConfig = { diff --git a/packages/medusa/src/api-v2/store/orders/query-config.ts b/packages/medusa/src/api-v2/store/orders/query-config.ts index aa6f2fedec..e8538cf822 100644 --- a/packages/medusa/src/api-v2/store/orders/query-config.ts +++ b/packages/medusa/src/api-v2/store/orders/query-config.ts @@ -48,6 +48,7 @@ export const defaultStoreRetrieveOrderFields = [ "*shipping_methods", "*shipping_methods.tax_lines", "*shipping_methods.adjustments", + "*payment_collections", ] export const retrieveTransformQueryConfig = { diff --git a/packages/modules/link-modules/src/definitions/index.ts b/packages/modules/link-modules/src/definitions/index.ts index a6ce650479..288b03e548 100644 --- a/packages/modules/link-modules/src/definitions/index.ts +++ b/packages/modules/link-modules/src/definitions/index.ts @@ -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" diff --git a/packages/modules/link-modules/src/definitions/order-payment-collection.ts b/packages/modules/link-modules/src/definitions/order-payment-collection.ts new file mode 100644 index 0000000000..f309498ec9 --- /dev/null +++ b/packages/modules/link-modules/src/definitions/order-payment-collection.ts @@ -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", + }, + }, + ], +} diff --git a/packages/modules/link-modules/src/definitions/product-shipping-profile.ts b/packages/modules/link-modules/src/definitions/product-shipping-profile.ts deleted file mode 100644 index 62e5a3d84b..0000000000 --- a/packages/modules/link-modules/src/definitions/product-shipping-profile.ts +++ /dev/null @@ -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", - }, - }, - ], -}