fix: add default relations to variants to include prices (#394)
This commit is contained in:
@@ -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<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": Any<Object>,
|
||||||
|
"product_id": "test-product",
|
||||||
|
"sku": "test-sku",
|
||||||
|
"title": "Test variant",
|
||||||
|
"upc": "test-upc",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"weight": null,
|
||||||
|
"width": null,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`/store/variants includes default relations 1`] = `
|
||||||
|
Object {
|
||||||
|
"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": Any<Object>,
|
||||||
|
"product_id": "test-product",
|
||||||
|
"sku": "test-sku",
|
||||||
|
"title": "Test variant",
|
||||||
|
"upc": "test-upc",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"weight": null,
|
||||||
|
"width": null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
||||||
@@ -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),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { defaultRelations } from "."
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /variants/{variant_id}
|
* @oas [get] /variants/{variant_id}
|
||||||
* operationId: GetVariantsVariant
|
* operationId: GetVariantsVariant
|
||||||
@@ -22,7 +24,10 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const variantService = req.scope.resolve("productVariantService")
|
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 })
|
res.json({ variant })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -11,3 +11,5 @@ export default app => {
|
|||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const defaultRelations = ["prices"]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { defaultRelations } from "."
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /variants
|
* @oas [get] /variants
|
||||||
* operationId: GetVariants
|
* operationId: GetVariants
|
||||||
@@ -23,10 +25,14 @@ export default async (req, res) => {
|
|||||||
const limit = parseInt(req.query.limit) || 100
|
const limit = parseInt(req.query.limit) || 100
|
||||||
const offset = parseInt(req.query.offset) || 0
|
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 = {
|
const listConfig = {
|
||||||
relations: [],
|
relations: expandFields.length ? expandFields : defaultRelations,
|
||||||
skip: offset,
|
skip: offset,
|
||||||
take: limit,
|
take: limit,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user