diff --git a/packages/medusa/src/api/routes/store/index.js b/packages/medusa/src/api/routes/store/index.js index 582f218335..24f7063c7c 100644 --- a/packages/medusa/src/api/routes/store/index.js +++ b/packages/medusa/src/api/routes/store/index.js @@ -11,6 +11,7 @@ import customerRoutes from "./customers" import shippingOptionRoutes from "./shipping-options" import regionRoutes from "./regions" import swapRoutes from "./swaps" +import variantRoutes from "./variants" const route = Router() @@ -35,6 +36,7 @@ export default (app, container, config) => { shippingOptionRoutes(route) regionRoutes(route) swapRoutes(route) + variantRoutes(route) return app } diff --git a/packages/medusa/src/api/routes/store/variants/get-variant.js b/packages/medusa/src/api/routes/store/variants/get-variant.js new file mode 100644 index 0000000000..b9f609353e --- /dev/null +++ b/packages/medusa/src/api/routes/store/variants/get-variant.js @@ -0,0 +1,31 @@ +import { Validator } from "medusa-core-utils" + +export default async (req, res) => { + const { variant_id } = req.params + + const schema = Validator.objectId() + const { value, error } = schema.validate(variant_id) + + if (error) { + throw error + } + + const variantService = req.scope.resolve("productVariantService") + let variant = await variantService.retrieve(value) + + let includeFields = [ + "title", + "prices", + "sku", + "ean", + "image", + "inventory_quantity", + "allow_backorder", + "manage_inventory", + ] + if ("fields" in req.query) { + includeFields = req.query.fields.split(",") + } + variant = await variantService.decorate(variant, includeFields) + res.json({ variant }) +} diff --git a/packages/medusa/src/api/routes/store/variants/index.js b/packages/medusa/src/api/routes/store/variants/index.js new file mode 100644 index 0000000000..7d728c7946 --- /dev/null +++ b/packages/medusa/src/api/routes/store/variants/index.js @@ -0,0 +1,13 @@ +import { Router } from "express" +import middlewares from "../../../middlewares" + +const route = Router() + +export default app => { + app.use("/variants", route) + + route.get("/", middlewares.wrap(require("./list-variants").default)) + route.get("/:variant_id", middlewares.wrap(require("./get-variant").default)) + + return app +} diff --git a/packages/medusa/src/api/routes/store/variants/list-variants.js b/packages/medusa/src/api/routes/store/variants/list-variants.js new file mode 100644 index 0000000000..8655de9cdf --- /dev/null +++ b/packages/medusa/src/api/routes/store/variants/list-variants.js @@ -0,0 +1,31 @@ +import { Validator } from "medusa-core-utils" + +export default async (req, res) => { + const selector = {} + + if ("ids" in req.query) { + selector["_id"] = { $in: req.query.ids.split(",") } + } + + const variantService = req.scope.resolve("productVariantService") + const variants = await variantService.list(selector) + + let includeFields = [ + "title", + "prices", + "sku", + "ean", + "image", + "inventory_quantity", + "allow_backorder", + "manage_inventory", + ] + if ("fields" in req.query) { + includeFields = req.query.fields.split(",") + } + + const data = await Promise.all( + variants.map(v => variantService.decorate(v, includeFields)) + ) + res.json({ variants: data }) +}