diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index 9b5ceeb22b..71c036b72d 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -5,8 +5,10 @@ const { Region } = require("@medusajs/medusa"); const setupServer = require("../../../helpers/setup-server"); const { useApi } = require("../../../helpers/use-api"); const { initDb } = require("../../../helpers/use-db"); +const { expectRelations } = require("../../helpers/expect-relations"); const cartSeeder = require("../../helpers/cart-seeder"); +const productSeeder = require("../../helpers/product-seeder"); jest.setTimeout(30000); @@ -330,4 +332,56 @@ describe("/store/carts", () => { expect(response.status).toEqual(200); }); }); + + describe("get-cart returns expected relations", () => { + beforeEach(async () => { + try { + await productSeeder(dbConnection); + await cartSeeder(dbConnection); + } catch (err) { + console.log(err); + throw err; + } + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await doAfterEach(manager); + }); + + + it("returns default relations", async () => { + const api = useApi(); + + const expectedRelations = [ + "gift_cards", + "region", + "items.variant.prices", + "payment", + "shipping_address", + "billing_address", + "region.countries", + "region.payment_providers", + "payment_sessions", + "shipping_methods.shipping_option", + "discounts", + ] + + await api.post("/store/carts/test-cart/line-items", { + quantity: 1, + variant_id: "test-variant", + }) + + // await api.post("/store/carts/test-cart/shipping-methods", { + // option_id: "test-option" + // }) + + + const response = await api.get("/store/carts/test-cart") + + console.log(response.data.cart) + expectRelations(expectedRelations, response.data.cart) + + }); + }); }); diff --git a/integration-tests/api/helpers/expect-relations.js b/integration-tests/api/helpers/expect-relations.js new file mode 100644 index 0000000000..6d9a8850dc --- /dev/null +++ b/integration-tests/api/helpers/expect-relations.js @@ -0,0 +1,20 @@ +const { expect } = require("@jest/globals") + +export function expectRelations(expected, actual){ + expected.forEach(a => expectRelations_(a, actual)) +} + +function expectRelations_(expected, actual){ + const items = expected.split("."); + let data_ = actual; + + for(const i in items){ + data_ = data_[items[i]]; + + if(data_ instanceof Array && data_.length > 0) { + data_ = data_[0]; + } + + expect(data_).toBeDefined(); + } +} diff --git a/integration-tests/api/helpers/product-seeder.js b/integration-tests/api/helpers/product-seeder.js index 79e6f4eb97..e2cfda35a2 100644 --- a/integration-tests/api/helpers/product-seeder.js +++ b/integration-tests/api/helpers/product-seeder.js @@ -6,6 +6,7 @@ const { Product, ShippingProfile, ProductVariant, + MoneyAmount, Image, } = require("@medusajs/medusa"); @@ -74,7 +75,14 @@ module.exports = async (connection, data = {}) => { inventory_quantity: 10, title: "Test variant", product_id: "test-product", - prices: [{ id: "test-price", currency_code: "usd", amount: 100 }], + prices: [], options: [{ id: "test-variant-option", value: "Default variant" }], }); + + await manager.insert(MoneyAmount, { + variant_id: "test-variant", + id: "test-price", + currency_code: "usd", + amount: 100, + }); }; diff --git a/integration-tests/helpers/use-db.js b/integration-tests/helpers/use-db.js index c9be67e7ae..74e420ec24 100644 --- a/integration-tests/helpers/use-db.js +++ b/integration-tests/helpers/use-db.js @@ -36,8 +36,10 @@ module.exports = { ); const databaseName = "medusa-integration"; - await createDatabase({ databaseName }); + await createDatabase({ databaseName }); + + const connection = await createConnection({ type: "postgres", url: "postgres://localhost/medusa-integration", @@ -66,6 +68,7 @@ module.exports = { instance.setDb(dbConnection); return dbConnection; + }, useDb: function () { return instance; diff --git a/packages/medusa/src/api/routes/admin/draft-orders/index.js b/packages/medusa/src/api/routes/admin/draft-orders/index.js index 10b9e65f30..4bdacfde84 100644 --- a/packages/medusa/src/api/routes/admin/draft-orders/index.js +++ b/packages/medusa/src/api/routes/admin/draft-orders/index.js @@ -49,6 +49,8 @@ export const defaultRelations = ["order", "cart"] export const defaultCartRelations = [ "region", "items", + "items.variant", + "items.variant.prices", "payment", "shipping_address", "billing_address", diff --git a/packages/medusa/src/api/routes/store/carts/index.js b/packages/medusa/src/api/routes/store/carts/index.js index 0ec18e79fe..c87135e378 100644 --- a/packages/medusa/src/api/routes/store/carts/index.js +++ b/packages/medusa/src/api/routes/store/carts/index.js @@ -101,13 +101,15 @@ export const defaultRelations = [ "gift_cards", "region", "items", + "items.variant", + "items.variant.prices", "payment", "shipping_address", "billing_address", "region.countries", "region.payment_providers", - "shipping_methods", "payment_sessions", + "shipping_methods", "shipping_methods.shipping_option", "discounts", ] diff --git a/packages/medusa/src/api/routes/store/orders/index.js b/packages/medusa/src/api/routes/store/orders/index.js index 4405a86ba7..72e802c90e 100644 --- a/packages/medusa/src/api/routes/store/orders/index.js +++ b/packages/medusa/src/api/routes/store/orders/index.js @@ -33,6 +33,7 @@ export const defaultRelations = [ "items", "items.variant", "items.variant.product", + "items.variant.prices", "shipping_methods", "discounts", "customer", diff --git a/packages/medusa/src/models/line-item.ts b/packages/medusa/src/models/line-item.ts index 3060050646..e8d30aecff 100644 --- a/packages/medusa/src/models/line-item.ts +++ b/packages/medusa/src/models/line-item.ts @@ -99,7 +99,7 @@ export class LineItem { @Column({ nullable: true }) variant_id: string - @ManyToOne(() => ProductVariant, { eager: true }) + @ManyToOne(() => ProductVariant) @JoinColumn({ name: "variant_id" }) variant: ProductVariant diff --git a/packages/medusa/src/models/product-variant.ts b/packages/medusa/src/models/product-variant.ts index 7b97b6ea2a..0a3c569f9b 100644 --- a/packages/medusa/src/models/product-variant.ts +++ b/packages/medusa/src/models/product-variant.ts @@ -19,6 +19,9 @@ import { Product } from "./product" import { MoneyAmount } from "./money-amount" import { ProductOptionValue } from "./product-option-value" + + + @Entity() export class ProductVariant { @PrimaryColumn()