From d477ca9842eb58cc561e9e0fa9d01e2d0d2f6756 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Dreijer Date: Tue, 14 Sep 2021 16:54:39 +0200 Subject: [PATCH] fix: add default relations to variants to include prices (#394) --- .../__snapshots__/product-variants.js.snap | 89 ++++++++++++ .../api/__tests__/store/product-variants.js | 133 ++++++++++++++++++ .../api/routes/store/variants/get-variant.js | 7 +- .../src/api/routes/store/variants/index.js | 2 + .../routes/store/variants/list-variants.js | 10 +- 5 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap create mode 100644 integration-tests/api/__tests__/store/product-variants.js diff --git a/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap b/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap new file mode 100644 index 0000000000..073655bb2a --- /dev/null +++ b/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`/store/variants /test-variant 1`] = ` +Object { + "variant": Object { + "allow_backorder": false, + "barcode": "test-barcode", + "created_at": Any, + "deleted_at": null, + "ean": "test-ean", + "height": null, + "hs_code": null, + "id": "test-variant", + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": "test-price", + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": "test-variant", + }, + ], + "product": Any, + "product_id": "test-product", + "sku": "test-sku", + "title": "Test variant", + "upc": "test-upc", + "updated_at": Any, + "weight": null, + "width": null, + }, +} +`; + +exports[`/store/variants includes default relations 1`] = ` +Object { + "variants": Array [ + Object { + "allow_backorder": false, + "barcode": "test-barcode", + "created_at": Any, + "deleted_at": null, + "ean": "test-ean", + "height": null, + "hs_code": null, + "id": "test-variant", + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": "test-price", + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": "test-variant", + }, + ], + "product": Any, + "product_id": "test-product", + "sku": "test-sku", + "title": "Test variant", + "upc": "test-upc", + "updated_at": Any, + "weight": null, + "width": null, + }, + ], +} +`; diff --git a/integration-tests/api/__tests__/store/product-variants.js b/integration-tests/api/__tests__/store/product-variants.js new file mode 100644 index 0000000000..e7e7ab1960 --- /dev/null +++ b/integration-tests/api/__tests__/store/product-variants.js @@ -0,0 +1,133 @@ +const path = require("path") +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") + +const productSeeder = require("../../helpers/product-seeder") +jest.setTimeout(30000) +describe("/store/variants", () => { + let medusaProcess + let dbConnection + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) + + afterAll(async () => { + const db = useDb() + await db.shutdown() + medusaProcess.kill() + }) + + beforeEach(async () => { + try { + await productSeeder(dbConnection) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("includes default relations", async () => { + const api = useApi() + + const response = await api.get("/store/variants?ids=test-variant") + + expect(response.data).toMatchSnapshot({ + variants: [ + { + allow_backorder: false, + barcode: "test-barcode", + created_at: expect.any(String), + deleted_at: null, + ean: "test-ean", + height: null, + hs_code: null, + id: "test-variant", + inventory_quantity: 10, + length: null, + manage_inventory: true, + material: null, + metadata: null, + mid_code: null, + origin_country: null, + product_id: "test-product", + sku: "test-sku", + title: "Test variant", + upc: "test-upc", + updated_at: expect.any(String), + weight: null, + width: null, + prices: [ + { + created_at: expect.any(String), + updated_at: expect.any(String), + amount: 100, + currency_code: "usd", + deleted_at: null, + id: "test-price", + region_id: null, + sale_amount: null, + variant_id: "test-variant", + }, + ], + product: expect.any(Object), + }, + ], + }) + }) + + it("/test-variant", async () => { + const api = useApi() + + const response = await api.get("/store/variants/test-variant") + + expect(response.data).toMatchSnapshot({ + variant: { + allow_backorder: false, + barcode: "test-barcode", + created_at: expect.any(String), + deleted_at: null, + ean: "test-ean", + height: null, + hs_code: null, + id: "test-variant", + inventory_quantity: 10, + length: null, + manage_inventory: true, + material: null, + metadata: null, + mid_code: null, + origin_country: null, + product_id: "test-product", + sku: "test-sku", + title: "Test variant", + upc: "test-upc", + updated_at: expect.any(String), + weight: null, + width: null, + prices: [ + { + created_at: expect.any(String), + updated_at: expect.any(String), + amount: 100, + currency_code: "usd", + deleted_at: null, + id: "test-price", + region_id: null, + sale_amount: null, + variant_id: "test-variant", + }, + ], + product: expect.any(Object), + }, + }) + }) +}) diff --git a/packages/medusa/src/api/routes/store/variants/get-variant.js b/packages/medusa/src/api/routes/store/variants/get-variant.js index 31a6b90a5a..de50e27555 100644 --- a/packages/medusa/src/api/routes/store/variants/get-variant.js +++ b/packages/medusa/src/api/routes/store/variants/get-variant.js @@ -1,3 +1,5 @@ +import { defaultRelations } from "." + /** * @oas [get] /variants/{variant_id} * operationId: GetVariantsVariant @@ -22,7 +24,10 @@ export default async (req, res) => { try { const variantService = req.scope.resolve("productVariantService") - let variant = await variantService.retrieve(id, { relations: ["prices"] }) + let variant = await variantService.retrieve(id, { + relations: defaultRelations, + }) + res.json({ variant }) } catch (error) { throw error diff --git a/packages/medusa/src/api/routes/store/variants/index.js b/packages/medusa/src/api/routes/store/variants/index.js index 00c697e0f1..bc26ec1f55 100644 --- a/packages/medusa/src/api/routes/store/variants/index.js +++ b/packages/medusa/src/api/routes/store/variants/index.js @@ -11,3 +11,5 @@ export default app => { return app } + +export const defaultRelations = ["prices"] diff --git a/packages/medusa/src/api/routes/store/variants/list-variants.js b/packages/medusa/src/api/routes/store/variants/list-variants.js index b6376e7e69..cf227b28c6 100644 --- a/packages/medusa/src/api/routes/store/variants/list-variants.js +++ b/packages/medusa/src/api/routes/store/variants/list-variants.js @@ -1,3 +1,5 @@ +import { defaultRelations } from "." + /** * @oas [get] /variants * operationId: GetVariants @@ -23,10 +25,14 @@ export default async (req, res) => { const limit = parseInt(req.query.limit) || 100 const offset = parseInt(req.query.offset) || 0 - let selector = {} + let expandFields = [] + if ("expand" in req.query) { + expandFields = req.query.expand.split(",") + } + let selector = {} const listConfig = { - relations: [], + relations: expandFields.length ? expandFields : defaultRelations, skip: offset, take: limit, }