adds wishlist plugin

This commit is contained in:
Sebastian Rindom committed 2020-07-20 13:04:29 +02:00
1 parent 3710f7f80a
commit 0b8f1a5c31
15 files changed
+5776 -4

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
{
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-instanceof",
"@babel/plugin-transform-classes"
],
"presets": ["@babel/preset-env"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
@@ -0,0 +1,9 @@
{
"plugins": ["prettier"],
"extends": ["prettier"],
"rules": {
"prettier/prettier": "error",
"semi": "error",
"no-unused-expressions": "true"
}
}
@@ -0,0 +1,15 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
!jest.config.js
/dist
/api
/services
/models
/subscribers
@@ -0,0 +1,9 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock
@@ -0,0 +1,7 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}
+1
View File
@@ -0,0 +1 @@
// noop
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: "node",
}
@@ -0,0 +1,41 @@
{
"name": "medusa-plugin-wishlist",
"version": "0.3.0",
"description": "Provides /customers/:id/wishlist to add items to a customr's wishlist",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-wishlist"
},
"author": "Sebastian Rindom",
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/node": "^7.7.4",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-classes": "^7.9.5",
"@babel/plugin-transform-instanceof": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"@babel/register": "^7.7.4",
"@babel/runtime": "^7.9.6",
"cross-env": "^5.2.1",
"eslint": "^6.8.0",
"jest": "^25.5.2"
},
"scripts": {
"build": "babel src -d .",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__",
"test": "jest"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^0.3.0",
"medusa-interfaces": "^0.3.0",
"medusa-test-utils": "^0.3.0"
}
}
Whitespace-only changes.
@@ -0,0 +1,85 @@
import { Router } from "express"
import bodyParser from "body-parser"
import { Validator, MedusaError } from "medusa-core-utils"
export default () => {
const app = Router();
app.delete("/:id/wishlist", bodyParser.json(), async (req, res) => {
const schema = Validator.object().keys({
index: Validator.number().required(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(req.params.id)
const wishlist = customer.metadata && customer.metadata.wishlist || []
const newWishlist = [...wishlist]
newWishlist.splice(value.index, 1)
customer = await customerService.setMetadata(customer._id, "wishlist", newWishlist)
const data = await customerService.decorate(
customer,
["email", "first_name", "last_name", "shipping_addresses"],
["orders"]
)
res.json({ customer: data })
} catch (err) {
throw err
}
})
app.post("/:id/wishlist", bodyParser.json(), async (req, res) => {
const schema = Validator.object().keys({
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const lineItemService = req.scope.resolve("lineItemService")
const customerService = req.scope.resolve("customerService")
const regionService = req.scope.resolve("regionService")
let customer = await customerService.retrieve(req.params.id)
const regions = await regionService.list()
if (regions.length) {
const lineItem = await lineItemService.generate(
value.variant_id,
regions[0]._id,
value.quantity
)
const wishlist = customer.metadata && customer.metadata.wishlist || []
customer = await customerService.setMetadata(customer._id, "wishlist", [...wishlist, lineItem])
}
const data = await customerService.decorate(
customer,
["email", "first_name", "last_name", "shipping_addresses"],
["orders"]
)
res.json({ customer: data })
} catch (err) {
throw err
}
})
return app
}
File diff suppressed because it is too large. Load diff