Feat: add product default relations support for variant prices (#359)

Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
Vilfred Sikker Dreijer
2021-09-10 14:53:12 +02:00
committed by GitHub
co-authored by olivermrbl
parent cf66f97758
commit 682741041a
17 changed files with 1311 additions and 453 deletions
@@ -1,382 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/products GET /admin/products returns a list of products with child entities 1`] = `
Array [
Object {
"collection": Object {
"created_at": Any<String>,
"deleted_at": null,
"handle": "test-collection",
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"title": "Test collection",
"updated_at": Any<String>,
},
"collection_id": "test-collection",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product",
"height": null,
"hs_code": null,
"id": StringMatching /\\^test-\\*/,
"images": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"updated_at": Any<String>,
"url": "test-image.png",
},
],
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"product_id": StringMatching /\\^test-\\*/,
"title": "test-option",
"updated_at": Any<String>,
},
],
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"subtitle": null,
"tags": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^tag\\*/,
"metadata": null,
"updated_at": Any<String>,
"value": "123",
},
],
"thumbnail": null,
"title": "Test product",
"type": Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"updated_at": Any<String>,
"value": "test-type",
},
"type_id": "test-type",
"updated_at": Any<String>,
"variants": Array [
Object {
"allow_backorder": false,
"barcode": "test-barcode",
"created_at": 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,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-variant-option\\*/,
"metadata": null,
"option_id": StringMatching /\\^test-opt\\*/,
"updated_at": Any<String>,
"value": "Default variant",
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": StringMatching /\\^test-price\\*/,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"product_id": StringMatching /\\^test-\\*/,
"sku": "test-sku",
"title": "Test variant",
"upc": "test-upc",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean2",
"height": null,
"hs_code": null,
"id": "test-variant_2",
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-variant-option\\*/,
"metadata": null,
"option_id": StringMatching /\\^test-opt\\*/,
"updated_at": Any<String>,
"value": "Default variant 2",
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": StringMatching /\\^test-price\\*/,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"product_id": StringMatching /\\^test-\\*/,
"sku": "test-sku2",
"title": "Test variant rank (2)",
"upc": "test-upc2",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"allow_backorder": false,
"barcode": "test-barcode 1",
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean1",
"height": null,
"hs_code": null,
"id": "test-variant_1",
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-variant-option\\*/,
"metadata": null,
"option_id": StringMatching /\\^test-opt\\*/,
"updated_at": Any<String>,
"value": "Default variant 1",
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": StringMatching /\\^test-price\\*/,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"product_id": StringMatching /\\^test-\\*/,
"sku": "test-sku1",
"title": "Test variant rank (1)",
"upc": "test-upc1",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"weight": null,
"width": null,
},
Object {
"collection": Object {
"created_at": Any<String>,
"deleted_at": null,
"handle": "test-collection",
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"title": "Test collection",
"updated_at": Any<String>,
},
"collection_id": "test-collection",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description1",
"discountable": true,
"handle": "test-product1",
"height": null,
"hs_code": null,
"id": StringMatching /\\^test-\\*/,
"images": Array [],
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [],
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"subtitle": null,
"tags": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^tag\\*/,
"metadata": null,
"updated_at": Any<String>,
"value": "123",
},
],
"thumbnail": null,
"title": "Test product1",
"type": Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-\\*/,
"metadata": null,
"updated_at": Any<String>,
"value": "test-type",
},
"type_id": "test-type",
"updated_at": Any<String>,
"variants": Array [
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean4",
"height": null,
"hs_code": null,
"id": "test-variant_4",
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-variant-option\\*/,
"metadata": null,
"option_id": StringMatching /\\^test-opt\\*/,
"updated_at": Any<String>,
"value": "Default variant 3",
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": StringMatching /\\^test-price\\*/,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"product_id": StringMatching /\\^test-\\*/,
"sku": "test-sku4",
"title": "Test variant rank (2)",
"upc": "test-upc4",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean3",
"height": null,
"hs_code": null,
"id": "test-variant_3",
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": StringMatching /\\^test-variant-option\\*/,
"metadata": null,
"option_id": StringMatching /\\^test-opt\\*/,
"updated_at": Any<String>,
"value": "Default variant 3",
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": StringMatching /\\^test-price\\*/,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": StringMatching /\\^test-variant\\*/,
},
],
"product_id": StringMatching /\\^test-\\*/,
"sku": "test-sku3",
"title": "Test variant rank (2)",
"upc": "test-upc3",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"weight": null,
"width": null,
},
]
`;
@@ -16,7 +16,7 @@ describe("/admin/products", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd, verbose: true })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
@@ -0,0 +1,244 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/store/products /store/products/:id includes default relations 1`] = `
Object {
"product": Object {
"collection": Object {
"created_at": Any<String>,
"deleted_at": null,
"handle": "test-collection",
"id": "test-collection",
"metadata": null,
"title": "Test collection",
"updated_at": Any<String>,
},
"collection_id": "test-collection",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product",
"height": null,
"hs_code": null,
"id": "test-product",
"images": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-image",
"metadata": null,
"updated_at": Any<String>,
"url": "test-image.png",
},
],
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-option",
"metadata": null,
"product_id": "test-product",
"title": "test-option",
"updated_at": Any<String>,
"values": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant",
"variant_id": "test-variant",
},
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option-1",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant 1",
"variant_id": "test-variant_1",
},
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option-2",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant 2",
"variant_id": "test-variant_2",
},
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option-3",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant 3",
"variant_id": "test-variant_3",
},
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-variant-option-4",
"metadata": null,
"option_id": "test-option",
"updated_at": Any<String>,
"value": "Default variant 4",
"variant_id": "test-variant_4",
},
],
},
],
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"subtitle": null,
"tags": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "tag1",
"metadata": null,
"updated_at": Any<String>,
"value": "123",
},
],
"thumbnail": null,
"title": "Test product",
"type": Object {
"created_at": Any<String>,
"deleted_at": null,
"id": "test-type",
"metadata": null,
"updated_at": Any<String>,
"value": "test-type",
},
"type_id": "test-type",
"updated_at": Any<String>,
"variants": Array [
Object {
"allow_backorder": false,
"barcode": "test-barcode",
"created_at": 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,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": "test-price",
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": "test-variant",
},
],
"product_id": "test-product",
"sku": "test-sku",
"title": "Test variant",
"upc": "test-upc",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean2",
"height": null,
"hs_code": null,
"id": "test-variant_2",
"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<String>,
"currency_code": "usd",
"deleted_at": null,
"id": "test-price2",
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": "test-variant_2",
},
],
"product_id": "test-product",
"sku": "test-sku2",
"title": "Test variant rank (2)",
"upc": "test-upc2",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"allow_backorder": false,
"barcode": "test-barcode 1",
"created_at": Any<String>,
"deleted_at": null,
"ean": "test-ean1",
"height": null,
"hs_code": null,
"id": "test-variant_1",
"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<String>,
"currency_code": "usd",
"deleted_at": null,
"id": "test-price1",
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": "test-variant_1",
},
],
"product_id": "test-product",
"sku": "test-sku1",
"title": "Test variant rank (1)",
"upc": "test-upc1",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"weight": null,
"width": null,
},
}
`;
@@ -0,0 +1,265 @@
const { Product } = require("@medusajs/medusa")
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/products", () => {
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()
})
describe("/store/products/:id", () => {
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/products/test-product")
expect(response.data).toMatchSnapshot({
product: {
id: "test-product",
variants: [
{
id: "test-variant",
inventory_quantity: 10,
allow_backorder: false,
title: "Test variant",
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
length: null,
manage_inventory: true,
material: null,
metadata: null,
mid_code: null,
height: null,
hs_code: null,
origin_country: null,
barcode: "test-barcode",
product_id: "test-product",
created_at: expect.any(String),
updated_at: expect.any(String),
prices: [
{
id: "test-money-amount",
created_at: expect.any(String),
updated_at: expect.any(String),
amount: 100,
created_at: expect.any(String),
currency_code: "usd",
deleted_at: null,
id: "test-price",
region_id: null,
sale_amount: null,
updated_at: expect.any(String),
variant_id: "test-variant",
},
],
},
{
id: "test-variant_2",
inventory_quantity: 10,
allow_backorder: false,
title: "Test variant rank (2)",
sku: "test-sku2",
ean: "test-ean2",
upc: "test-upc2",
length: null,
manage_inventory: true,
material: null,
metadata: null,
mid_code: null,
height: null,
hs_code: null,
origin_country: null,
barcode: null,
product_id: "test-product",
created_at: expect.any(String),
updated_at: expect.any(String),
prices: [
{
id: "test-money-amount",
created_at: expect.any(String),
updated_at: expect.any(String),
amount: 100,
created_at: expect.any(String),
currency_code: "usd",
deleted_at: null,
id: "test-price2",
region_id: null,
sale_amount: null,
updated_at: expect.any(String),
variant_id: "test-variant_2",
},
],
},
{
id: "test-variant_1",
inventory_quantity: 10,
allow_backorder: false,
title: "Test variant rank (1)",
sku: "test-sku1",
ean: "test-ean1",
upc: "test-upc1",
length: null,
manage_inventory: true,
material: null,
metadata: null,
mid_code: null,
height: null,
hs_code: null,
origin_country: null,
barcode: "test-barcode 1",
product_id: "test-product",
created_at: expect.any(String),
updated_at: expect.any(String),
prices: [
{
id: "test-money-amount",
created_at: expect.any(String),
updated_at: expect.any(String),
amount: 100,
created_at: expect.any(String),
currency_code: "usd",
deleted_at: null,
id: "test-price1",
region_id: null,
sale_amount: null,
updated_at: expect.any(String),
variant_id: "test-variant_1",
},
],
},
],
images: [
{
id: "test-image",
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
handle: "test-product",
title: "Test product",
profile_id: expect.stringMatching(/^sp_*/),
description: "test-product-description",
collection_id: "test-collection",
collection: {
id: "test-collection",
created_at: expect.any(String),
updated_at: expect.any(String),
},
type: {
id: "test-type",
created_at: expect.any(String),
updated_at: expect.any(String),
},
tags: [
{
id: "tag1",
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
options: [
{
id: "test-option",
values: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
variant_id: "test-variant",
metadata: null,
deleted_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
id: "test-variant-option-1",
value: "Default variant 1",
option_id: "test-option",
variant_id: "test-variant_1",
metadata: null,
deleted_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
id: "test-variant-option-2",
value: "Default variant 2",
option_id: "test-option",
variant_id: "test-variant_2",
metadata: null,
deleted_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
id: "test-variant-option-3",
value: "Default variant 3",
option_id: "test-option",
variant_id: "test-variant_3",
metadata: null,
deleted_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
id: "test-variant-option-4",
value: "Default variant 4",
option_id: "test-option",
variant_id: "test-variant_4",
metadata: null,
deleted_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
it("Fetching variant options without additional relation fails", async () => {
const api = useApi()
const response = await api.get("/store/products/test-product")
const product = response.data.product
expect(product.variants.some((variant) => variant.options)).toEqual(false)
})
})
})
@@ -192,7 +192,7 @@ module.exports = async (connection, data = {}) => {
options: [
{
id: "test-variant-option-4",
value: "Default variant 3",
value: "Default variant 4",
option_id: "test-option",
},
],
+2 -2
View File
@@ -8,7 +8,7 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.1.36",
"@medusajs/medusa": "1.1.40-dev-1631178030541",
"medusa-interfaces": "1.1.21",
"typeorm": "^0.2.31"
},
@@ -19,4 +19,4 @@
"babel-preset-medusa-package": "1.1.13",
"jest": "^26.6.3"
}
}
}
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -43,4 +43,4 @@
"oas-normalize": "^2.3.1",
"swagger-inline": "^3.2.2"
}
}
}
@@ -1,6 +1,8 @@
import { request } from "../../../../../helpers/test-request"
import { IdMap } from "medusa-test-utils"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { defaultRelations } from ".."
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
describe("Get product by id", () => {
describe("get product by id successfull", () => {
@@ -20,7 +22,7 @@ describe("Get product by id", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("product1"),
{ relations: ["images", "variants", "options"] }
{ relations: defaultRelations }
)
})
@@ -28,4 +30,37 @@ describe("Get product by id", () => {
expect(subject.body.product.id).toEqual(IdMap.getId("product1"))
})
})
describe("Query products with relations", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/store/products/${IdMap.getId("variantsWithPrices")}`
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls retrieve() once", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
})
it("endpoint called with defaultRelations", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("variantsWithPrices"),
{ relations: defaultRelations }
)
})
it("returns product with variant prices", () => {
expect(
subject.body.product.variants.some(variant => variant.prices)
).toEqual(true)
expect(subject.body.product.variants[0].prices[0].amount).toEqual(100)
})
})
})
@@ -1,4 +1,5 @@
import { IdMap } from "medusa-test-utils"
import { defaultRelations } from ".."
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
@@ -18,7 +19,7 @@ describe("GET /store/products", () => {
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.list).toHaveBeenCalledWith(
{},
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
{ relations: defaultRelations, skip: 0, take: 100 }
)
})
@@ -43,7 +44,7 @@ describe("GET /store/products", () => {
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.list).toHaveBeenCalledWith(
{ is_giftcard: true },
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
{ relations: defaultRelations, skip: 0, take: 100 }
)
})
})
@@ -1,3 +1,5 @@
import { defaultRelations } from "."
/**
* @oas [get] /products/{id}
* operationId: GetProductsProduct
@@ -22,7 +24,7 @@ export default async (req, res) => {
const productService = req.scope.resolve("productService")
let product = await productService.retrieve(id, {
relations: ["images", "variants", "options"],
relations: defaultRelations,
})
res.json({ product })
@@ -11,3 +11,14 @@ export default app => {
return app
}
export const defaultRelations = [
"variants",
"variants.prices",
"options",
"options.values",
"images",
"tags",
"collection",
"type",
]
@@ -1,3 +1,5 @@
import { defaultRelations } from "."
/**
* @oas [get] /products
* operationId: GetProducts
@@ -40,7 +42,7 @@ export default async (req, res) => {
}
const listConfig = {
relations: ["variants", "options", "images"],
relations: defaultRelations,
skip: offset,
take: limit,
}
@@ -1,3 +1,4 @@
import { IdMap } from "../../../../../../../medusa-test-utils/dist"
import { request } from "../../../../../helpers/test-request"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
@@ -23,4 +24,18 @@ describe("Get variant by id", () => {
expect(subject.body.variant.id).toEqual("1")
})
})
describe("get variant with prices", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/store/variants/${IdMap.getId("variantWithPrices")}`
)
})
it("successfully retrieves variants with prices", async () => {
expect(subject.status).toEqual(200)
expect(subject.body.variant.prices[0].amount).toEqual(100)
})
})
})
@@ -90,6 +90,35 @@ const invalidVariant = {
],
}
const variantWithPrices = {
id: "variant_with_prices",
title: "Variant with prices",
prices: [
{
id: "price_1",
currency_code: "usd",
amount: 100,
sale_amount: null,
variant_id: "variant_with_prices",
region_id: null,
created_at: "2021-03-16T21:24:13.657Z",
updated_at: "2021-03-16T21:24:13.657Z",
deleted_at: null,
},
{
id: "price_2",
currency_code: "dk",
amount: 100,
sale_amount: null,
variant_id: "variant_with_prices",
region_id: null,
created_at: "2021-03-16T21:24:13.657Z",
updated_at: "2021-03-16T21:24:13.657Z",
deleted_at: null,
},
],
}
const testVariant = {
id: IdMap.getId("testVariant"),
title: "test variant",
@@ -161,6 +190,7 @@ export const variants = {
eur10us12: eur10us12,
testVariant: testVariant,
giftCard: giftCardVar,
variantWithPrices: variantWithPrices,
}
export const ProductVariantServiceMock = {
@@ -193,6 +223,9 @@ export const ProductVariantServiceMock = {
if (variantId === "4") {
return Promise.resolve(variant4)
}
if (variantId === IdMap.getId("variantWithPrices")) {
return Promise.resolve(variantWithPrices)
}
if (variantId === IdMap.getId("validId")) {
return Promise.resolve(variant5)
}
@@ -26,6 +26,40 @@ export const products = {
},
],
},
variantsWithPrices: {
id: IdMap.getId("variantsWithPrices"),
title: "Variant with prices",
variants: [
{
id: IdMap.getId("variant_with_prices"),
title: "Variant with prices",
prices: [
{
id: "price_1",
currency_code: "usd",
amount: 100,
sale_amount: null,
variant_id: "variant_with_prices",
region_id: null,
created_at: "2021-03-16T21:24:13.657Z",
updated_at: "2021-03-16T21:24:13.657Z",
deleted_at: null,
},
{
id: "price_2",
currency_code: "dk",
amount: 100,
sale_amount: null,
variant_id: "variant_with_prices",
region_id: null,
created_at: "2021-03-16T21:24:13.657Z",
updated_at: "2021-03-16T21:24:13.657Z",
deleted_at: null,
},
],
},
],
},
}
export const ProductServiceMock = {
@@ -93,6 +127,9 @@ export const ProductServiceMock = {
if (productId === IdMap.getId("productWithOptions")) {
return Promise.resolve(products.productWithOptions)
}
if (productId === IdMap.getId("variantsWithPrices")) {
return Promise.resolve(products.variantsWithPrices)
}
return Promise.resolve(undefined)
}),
update: jest.fn().mockImplementation((product, data) => {
+1 -1
View File
@@ -15858,4 +15858,4 @@ zen-observable@0.8.15:
zwitch@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-1.0.5.tgz#d11d7381ffed16b742f6af7b3f223d5cd9fe9920"
integrity sha512-V50KMwwzqJV0NpZIZFwfOD5/lyny3WlSzRiXgA0G7VUnRlqttta1L6UQIHzd6EuBY/cHGfwTIck7w1yH6Q5zUw==
integrity sha512-V50KMwwzqJV0NpZIZFwfOD5/lyny3WlSzRiXgA0G7VUnRlqttta1L6UQIHzd6EuBY/cHGfwTIck7w1yH6Q5zUw==