From 24ef496bbeb5f78a016bf65334550aae58379375 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Mon, 9 Dec 2024 14:15:47 +0100 Subject: [PATCH 1/2] wip --- .../modules/__tests__/prices/price.query.ts | 112 ++++++++++++++++++ .../list-shipping-options-for-cart.ts | 2 + .../types/src/http/fulfillment/store/index.ts | 9 +- packages/core/types/src/http/pricing/index.ts | 1 + .../types/src/http/pricing/store/entities.ts | 56 +++++++++ .../types/src/http/pricing/store/index.ts | 1 + .../store/shipping-options/query-config.ts | 8 +- 7 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 integration-tests/modules/__tests__/prices/price.query.ts create mode 100644 packages/core/types/src/http/pricing/store/entities.ts create mode 100644 packages/core/types/src/http/pricing/store/index.ts diff --git a/integration-tests/modules/__tests__/prices/price.query.ts b/integration-tests/modules/__tests__/prices/price.query.ts new file mode 100644 index 0000000000000..4be91c3d5148e --- /dev/null +++ b/integration-tests/modules/__tests__/prices/price.query.ts @@ -0,0 +1,112 @@ +import { medusaIntegrationTestRunner } from "@medusajs/test-utils" +import { ContainerRegistrationKeys } from "@medusajs/utils" +import { createAdminUser } from "../../../helpers/create-admin-user" +import { + getPricelistFixture, + getProductFixture, +} from "../../../helpers/fixtures" + +jest.setTimeout(50000) + +const env = { MEDUSA_FF_MEDUSA_V2: true } +const adminHeaders = { + headers: { "x-medusa-access-token": "test_token" }, +} + +medusaIntegrationTestRunner({ + env, + testSuite: ({ dbConnection, getContainer, api }) => { + describe("Query - Price", () => { + let appContainer, query + + beforeAll(async () => { + appContainer = getContainer() + }) + + beforeEach(async () => { + await createAdminUser(dbConnection, adminHeaders, appContainer) + }) + + describe("Query - price", () => { + let product, priceList + + beforeEach(async () => { + query = appContainer.resolve(ContainerRegistrationKeys.QUERY) + + product = ( + await api.post( + "/admin/products", + getProductFixture({ + title: "Base product", + variants: [ + { + title: "Test variant", + prices: [ + { + currency_code: "usd", + amount: 100, + }, + { + currency_code: "eur", + amount: 45, + }, + { + currency_code: "dkk", + amount: 30, + }, + ], + options: { + size: "large", + color: "green", + }, + }, + ], + }), + adminHeaders + ) + ).data.product + + priceList = ( + await api.post( + "/admin/price-lists", + getPricelistFixture({ + title: "New sale", + prices: [ + { + amount: 100, + currency_code: "usd", + variant_id: product.variants[0].id, + min_quantity: 1, + max_quantity: 100, + }, + { + amount: 80, + currency_code: "usd", + variant_id: product.variants[0].id, + min_quantity: 101, + max_quantity: 500, + }, + ], + }), + adminHeaders + ) + ).data.price_list + }) + + it("should query prices filtered by price list", async () => { + const { data } = await query.graph({ + entity: "price", + fields: ["*"], + filters: { + price_list_id: priceList.id, + // correct one + price_set_id: [], + }, + }) + + console.log("data -- ", data) + }) + }) + }) + }, +}) diff --git a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts index c2a72e172e3fb..0f048364138d7 100644 --- a/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/list-shipping-options-for-cart.ts @@ -127,6 +127,8 @@ export const listShippingOptionsForCartWorkflow = createWorkflow( "rules.operator", "calculated_price.*", + "prices.*", + "prices.price_rules.*", ], variables: queryVariables, }).config({ name: "shipping-options-query" }) diff --git a/packages/core/types/src/http/fulfillment/store/index.ts b/packages/core/types/src/http/fulfillment/store/index.ts index 39e6c365c0d15..0789ad743e694 100644 --- a/packages/core/types/src/http/fulfillment/store/index.ts +++ b/packages/core/types/src/http/fulfillment/store/index.ts @@ -1,4 +1,5 @@ import { ShippingOptionPriceType } from "../../../fulfillment" +import { StoreCalculatedPrice, StorePrice } from "../../pricing/store/entities" // TODO: The way the cart shipping options are listed now differs from most other endpoints as it is fetched in a workflow. // We should consider refactoring this to be more consistent with other endpoints. @@ -13,7 +14,7 @@ export interface StoreCartShippingOption { name: string /** * The type of the shipping option's price. `flat` means the price - * is fixed, whereas `calculated` means the price is calculated by the + * is fixed, whereas `calculated` means the price is calculated by the * associated fulfillment provider. */ price_type: ShippingOptionPriceType @@ -31,7 +32,7 @@ export interface StoreCartShippingOption { provider_id: string /** * The data useful for the fulfillment provider when handling the shipment and fulfillment. - * + * * Learn more in [this documentation](https://docs.medusajs.com/resources/commerce-modules/fulfillment/shipping-option#data-property). */ data: Record | null @@ -73,4 +74,8 @@ export interface StoreCartShippingOption { * The shipping option's amount. */ amount: number + + prices: StorePrice[] + + calculated_price: StoreCalculatedPrice } diff --git a/packages/core/types/src/http/pricing/index.ts b/packages/core/types/src/http/pricing/index.ts index 26b8eb9dadfe8..3bd2bd2cc018f 100644 --- a/packages/core/types/src/http/pricing/index.ts +++ b/packages/core/types/src/http/pricing/index.ts @@ -1 +1,2 @@ export * from "./admin" +export * from "./store" diff --git a/packages/core/types/src/http/pricing/store/entities.ts b/packages/core/types/src/http/pricing/store/entities.ts new file mode 100644 index 0000000000000..6054e0f922e12 --- /dev/null +++ b/packages/core/types/src/http/pricing/store/entities.ts @@ -0,0 +1,56 @@ +import { PricingRuleOperatorValues } from "../../../pricing" +import { BaseCalculatedPriceSet } from "../common" + +export interface StorePrice { + /** + * The price's ID. + */ + id: string + /** + * The price's currency code. + * + * @example + * usd + */ + currency_code: string + /** + * The price's amount. + */ + amount: number + /** + * The minimum quantity that must be available in the cart for the price to be applied. + */ + min_quantity: number | null + /** + * The maximum quantity allowed to be available in the cart for the price to be applied. + */ + max_quantity: number | null + + /** + * The rules enabled to enable the current price + */ + price_rules?: StorePriceRule[] +} + +export interface StorePriceRule { + /** + * The ID of the price rule. + */ + id: string + /** + * The attribute of the price rule + */ + attribute: string + + /** + * The operator of the price rule + */ + operator: PricingRuleOperatorValues + + /** + * The value of the price rule. + */ + value: string +} + +export interface StoreCalculatedPrice extends BaseCalculatedPriceSet {} diff --git a/packages/core/types/src/http/pricing/store/index.ts b/packages/core/types/src/http/pricing/store/index.ts new file mode 100644 index 0000000000000..8270e0b265e19 --- /dev/null +++ b/packages/core/types/src/http/pricing/store/index.ts @@ -0,0 +1 @@ +export * from "./entities" diff --git a/packages/medusa/src/api/store/shipping-options/query-config.ts b/packages/medusa/src/api/store/shipping-options/query-config.ts index 07cb17ccfc3f1..d62a4ee58ac8b 100644 --- a/packages/medusa/src/api/store/shipping-options/query-config.ts +++ b/packages/medusa/src/api/store/shipping-options/query-config.ts @@ -4,8 +4,12 @@ export const defaultStoreShippingOptionsFields = [ "price_type", "service_zone_id", "shipping_profile_id", - "fulfillment_provider_id", - "shipping_option_type_id", + "provider_id", + "*type", + "*provider", + "*rules", + "*rules", + "*calculated_price", ] export const listTransformQueryConfig = { From 25a3e28ef6bfa7dc1b55de0df3bfc5b9a17c46a6 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Mon, 9 Dec 2024 14:30:09 +0100 Subject: [PATCH 2/2] chore(types): add price types for shipping option endpoints --- .../modules/__tests__/prices/price.query.ts | 112 ------------------ .../types/src/http/fulfillment/store/index.ts | 6 + .../store/shipping-options/query-config.ts | 8 +- 3 files changed, 8 insertions(+), 118 deletions(-) delete mode 100644 integration-tests/modules/__tests__/prices/price.query.ts diff --git a/integration-tests/modules/__tests__/prices/price.query.ts b/integration-tests/modules/__tests__/prices/price.query.ts deleted file mode 100644 index 4be91c3d5148e..0000000000000 --- a/integration-tests/modules/__tests__/prices/price.query.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { medusaIntegrationTestRunner } from "@medusajs/test-utils" -import { ContainerRegistrationKeys } from "@medusajs/utils" -import { createAdminUser } from "../../../helpers/create-admin-user" -import { - getPricelistFixture, - getProductFixture, -} from "../../../helpers/fixtures" - -jest.setTimeout(50000) - -const env = { MEDUSA_FF_MEDUSA_V2: true } -const adminHeaders = { - headers: { "x-medusa-access-token": "test_token" }, -} - -medusaIntegrationTestRunner({ - env, - testSuite: ({ dbConnection, getContainer, api }) => { - describe("Query - Price", () => { - let appContainer, query - - beforeAll(async () => { - appContainer = getContainer() - }) - - beforeEach(async () => { - await createAdminUser(dbConnection, adminHeaders, appContainer) - }) - - describe("Query - price", () => { - let product, priceList - - beforeEach(async () => { - query = appContainer.resolve(ContainerRegistrationKeys.QUERY) - - product = ( - await api.post( - "/admin/products", - getProductFixture({ - title: "Base product", - variants: [ - { - title: "Test variant", - prices: [ - { - currency_code: "usd", - amount: 100, - }, - { - currency_code: "eur", - amount: 45, - }, - { - currency_code: "dkk", - amount: 30, - }, - ], - options: { - size: "large", - color: "green", - }, - }, - ], - }), - adminHeaders - ) - ).data.product - - priceList = ( - await api.post( - "/admin/price-lists", - getPricelistFixture({ - title: "New sale", - prices: [ - { - amount: 100, - currency_code: "usd", - variant_id: product.variants[0].id, - min_quantity: 1, - max_quantity: 100, - }, - { - amount: 80, - currency_code: "usd", - variant_id: product.variants[0].id, - min_quantity: 101, - max_quantity: 500, - }, - ], - }), - adminHeaders - ) - ).data.price_list - }) - - it("should query prices filtered by price list", async () => { - const { data } = await query.graph({ - entity: "price", - fields: ["*"], - filters: { - price_list_id: priceList.id, - // correct one - price_set_id: [], - }, - }) - - console.log("data -- ", data) - }) - }) - }) - }, -}) diff --git a/packages/core/types/src/http/fulfillment/store/index.ts b/packages/core/types/src/http/fulfillment/store/index.ts index 0789ad743e694..23a6e449e5cc8 100644 --- a/packages/core/types/src/http/fulfillment/store/index.ts +++ b/packages/core/types/src/http/fulfillment/store/index.ts @@ -75,7 +75,13 @@ export interface StoreCartShippingOption { */ amount: number + /** + * All the prices for this shipping option + */ prices: StorePrice[] + /** + * Calculated price for the shipping option + */ calculated_price: StoreCalculatedPrice } diff --git a/packages/medusa/src/api/store/shipping-options/query-config.ts b/packages/medusa/src/api/store/shipping-options/query-config.ts index d62a4ee58ac8b..07cb17ccfc3f1 100644 --- a/packages/medusa/src/api/store/shipping-options/query-config.ts +++ b/packages/medusa/src/api/store/shipping-options/query-config.ts @@ -4,12 +4,8 @@ export const defaultStoreShippingOptionsFields = [ "price_type", "service_zone_id", "shipping_profile_id", - "provider_id", - "*type", - "*provider", - "*rules", - "*rules", - "*calculated_price", + "fulfillment_provider_id", + "shipping_option_type_id", ] export const listTransformQueryConfig = {