From e3beaf5a2971a1125d7ee0cbc24e7d6589ff255b Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 23 May 2024 16:54:09 +0200 Subject: [PATCH] chore: add cart links to promotions/product (#7428) --- .../core/types/src/http/promotion/common.ts | 12 ++++++++++++ .../core/types/src/http/promotion/index.ts | 1 + .../core/types/src/http/promotion/store.ts | 3 +++ .../medusa/src/api/store/carts/query-config.ts | 11 +++++++++++ .../src/definitions/cart-promotion.ts | 15 +++++---------- .../src/definitions/readonly/index.ts | 1 + .../readonly/line-item-adjustment-promotion.ts | 18 ++++++++++++++++++ 7 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 packages/core/types/src/http/promotion/common.ts create mode 100644 packages/core/types/src/http/promotion/store.ts create mode 100644 packages/modules/link-modules/src/definitions/readonly/line-item-adjustment-promotion.ts diff --git a/packages/core/types/src/http/promotion/common.ts b/packages/core/types/src/http/promotion/common.ts new file mode 100644 index 0000000000..85d98909fc --- /dev/null +++ b/packages/core/types/src/http/promotion/common.ts @@ -0,0 +1,12 @@ +import { ApplicationMethodTypeValues } from "../../promotion" + +export interface BasePromotion { + id: string + code?: string + is_automatic?: boolean + application_method?: { + type?: ApplicationMethodTypeValues + value?: number + currency_code?: string + } +} diff --git a/packages/core/types/src/http/promotion/index.ts b/packages/core/types/src/http/promotion/index.ts index 26b8eb9dad..3bd2bd2cc0 100644 --- a/packages/core/types/src/http/promotion/index.ts +++ b/packages/core/types/src/http/promotion/index.ts @@ -1 +1,2 @@ export * from "./admin" +export * from "./store" diff --git a/packages/core/types/src/http/promotion/store.ts b/packages/core/types/src/http/promotion/store.ts new file mode 100644 index 0000000000..9dcc73350d --- /dev/null +++ b/packages/core/types/src/http/promotion/store.ts @@ -0,0 +1,3 @@ +import { BasePromotion } from "./common" + +export interface StorePromotion extends BasePromotion {} diff --git a/packages/medusa/src/api/store/carts/query-config.ts b/packages/medusa/src/api/store/carts/query-config.ts index 1e65ea0ec0..e3344c8e0f 100644 --- a/packages/medusa/src/api/store/carts/query-config.ts +++ b/packages/medusa/src/api/store/carts/query-config.ts @@ -23,9 +23,19 @@ export const defaultStoreCartFields = [ "original_shipping_tax_total", "original_shipping_tax_subtotal", "original_shipping_total", + "promotions.id", + "promotions.code", + "promotions.is_automatic", + "promotions.application_method.value", + "promotions.application_method.type", + "promotions.application_method.currency_code", "items.id", "items.variant_id", "items.product_id", + "items.product.categories.id", + "items.product.tags.id", + "items.product.collection_id", + "items.product.type_id", "items.product_title", "items.product_description", "items.product_subtitle", @@ -47,6 +57,7 @@ export const defaultStoreCartFields = [ "items.tax_lines.provider_id", "items.adjustments.id", "items.adjustments.code", + "items.adjustments.promotion_id", "items.adjustments.amount", "customer.id", "customer.email", diff --git a/packages/modules/link-modules/src/definitions/cart-promotion.ts b/packages/modules/link-modules/src/definitions/cart-promotion.ts index a2f1b160ef..2b595f64f5 100644 --- a/packages/modules/link-modules/src/definitions/cart-promotion.ts +++ b/packages/modules/link-modules/src/definitions/cart-promotion.ts @@ -29,26 +29,21 @@ export const CartPromotion: ModuleJoinerConfig = { serviceName: Modules.PROMOTION, primaryKey: "id", foreignKey: "promotion_id", - alias: "promotion", + alias: "promotions", }, ], extends: [ { serviceName: Modules.CART, + fieldAlias: { + promotions: "cart_link.promotions", + }, relationship: { serviceName: LINKS.CartPromotion, primaryKey: "cart_id", foreignKey: "id", alias: "cart_link", - }, - }, - { - serviceName: Modules.PROMOTION, - relationship: { - serviceName: LINKS.CartPromotion, - primaryKey: "promotion_id", - foreignKey: "id", - alias: "promotion_link", + isList: true, }, }, ], diff --git a/packages/modules/link-modules/src/definitions/readonly/index.ts b/packages/modules/link-modules/src/definitions/readonly/index.ts index f1df878f68..e10dd70ec0 100644 --- a/packages/modules/link-modules/src/definitions/readonly/index.ts +++ b/packages/modules/link-modules/src/definitions/readonly/index.ts @@ -3,6 +3,7 @@ export * from "./cart-product" export * from "./cart-region" export * from "./cart-sales-channel" export * from "./inventory-level-stock-location" +export * from "./line-item-adjustment-promotion" export * from "./order-customer" export * from "./order-product" export * from "./order-sales-channel" diff --git a/packages/modules/link-modules/src/definitions/readonly/line-item-adjustment-promotion.ts b/packages/modules/link-modules/src/definitions/readonly/line-item-adjustment-promotion.ts new file mode 100644 index 0000000000..b6df51fb14 --- /dev/null +++ b/packages/modules/link-modules/src/definitions/readonly/line-item-adjustment-promotion.ts @@ -0,0 +1,18 @@ +import { Modules } from "@medusajs/modules-sdk" +import { ModuleJoinerConfig } from "@medusajs/types" + +export const LineItemAdjustmentPromotion: ModuleJoinerConfig = { + isLink: true, + isReadOnlyLink: true, + extends: [ + { + serviceName: Modules.CART, + relationship: { + serviceName: Modules.PROMOTION, + primaryKey: "id", + foreignKey: "promotion_id", + alias: "promotion", + }, + }, + ], +}