diff --git a/packages/medusa-plugin-add-ons/.gitignore b/packages/medusa-plugin-add-ons/.gitignore index e16f73f73a..718e6cc25c 100644 --- a/packages/medusa-plugin-add-ons/.gitignore +++ b/packages/medusa-plugin-add-ons/.gitignore @@ -11,4 +11,5 @@ node_modules /services /models /subscribers +/loaders diff --git a/packages/medusa-plugin-add-ons/CHANGELOG.md b/packages/medusa-plugin-add-ons/CHANGELOG.md index 8b2001e0ba..3c7fbe90ff 100644 --- a/packages/medusa-plugin-add-ons/CHANGELOG.md +++ b/packages/medusa-plugin-add-ons/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [1.0.4](https://github.com/medusajs/medusa/compare/medusa-plugin-add-ons@1.0.3...medusa-plugin-add-ons@1.0.4) (2020-10-09) + +**Note:** Version bump only for package medusa-plugin-add-ons + + + + + ## [1.0.3](https://github.com/medusajs/medusa/compare/medusa-plugin-add-ons@1.0.2...medusa-plugin-add-ons@1.0.3) (2020-10-05) diff --git a/packages/medusa-plugin-add-ons/package.json b/packages/medusa-plugin-add-ons/package.json index 22c6bd1579..ac66f38bf8 100644 --- a/packages/medusa-plugin-add-ons/package.json +++ b/packages/medusa-plugin-add-ons/package.json @@ -1,6 +1,6 @@ { "name": "medusa-plugin-add-ons", - "version": "1.0.3", + "version": "1.0.4", "description": "Add-on plugin for Medusa Commerce", "main": "index.js", "repository": { diff --git a/packages/medusa-plugin-add-ons/src/api/routes/store/create-line-item.js b/packages/medusa-plugin-add-ons/src/api/routes/store/create-line-item.js index 069fa8e353..2cd3a4d7c9 100644 --- a/packages/medusa-plugin-add-ons/src/api/routes/store/create-line-item.js +++ b/packages/medusa-plugin-add-ons/src/api/routes/store/create-line-item.js @@ -32,16 +32,6 @@ export default async (req, res) => { cart = await cartService.addLineItem(cart._id, lineItem) cart = await cartService.decorate(cart, [], ["region"]) - cart.items = await Promise.all( - cart.items.map((item) => - lineItemService.decorate( - item, - ["title", "quantity", "thumbnail", "content", "should_merge"], - ["add_ons"] - ) - ) - ) - res.status(200).json({ cart }) } catch (err) { throw err diff --git a/packages/medusa-plugin-add-ons/src/api/routes/store/update-line-item.js b/packages/medusa-plugin-add-ons/src/api/routes/store/update-line-item.js index 323f259179..b3717325a7 100644 --- a/packages/medusa-plugin-add-ons/src/api/routes/store/update-line-item.js +++ b/packages/medusa-plugin-add-ons/src/api/routes/store/update-line-item.js @@ -45,16 +45,6 @@ export default async (req, res) => { cart = await cartService.decorate(cart, [], ["region"]) - cart.items = await Promise.all( - cart.items.map((item) => - lineItemService.decorate( - item, - ["title", "quantity", "thumbnail", "content", "should_merge"], - ["add_ons"] - ) - ) - ) - res.status(200).json({ cart }) } catch (err) { throw err diff --git a/packages/medusa-plugin-add-ons/src/loaders/decorator.js b/packages/medusa-plugin-add-ons/src/loaders/decorator.js new file mode 100644 index 0000000000..2a94b4f2cc --- /dev/null +++ b/packages/medusa-plugin-add-ons/src/loaders/decorator.js @@ -0,0 +1,26 @@ +export default (container, config) => { + const cartService = container.resolve("cartService") + const addOnLineItemService = container.resolve("addOnLineItemService") + + cartService.addDecorator(async (cart) => { + try { + cart.items = await Promise.all( + cart.items.map((item) => { + if (item.metadata && item.metadata.add_ons) { + return addOnLineItemService.decorate( + item, + ["title", "quantity", "thumbnail", "content", "should_merge"], + ["add_ons"] + ) + } else { + return item + } + }) + ) + + return cart + } catch (error) { + return cart + } + }) +}