adds wishlist plugin

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

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"]
}
}
}

View File

@@ -0,0 +1,9 @@
{
"plugins": ["prettier"],
"extends": ["prettier"],
"rules": {
"prettier/prettier": "error",
"semi": "error",
"no-unused-expressions": "true"
}
}

View File

@@ -0,0 +1,15 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
!jest.config.js
/dist
/api
/services
/models
/subscribers

View File

@@ -0,0 +1,9 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock

View File

@@ -0,0 +1,7 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}

View File

@@ -0,0 +1 @@
// noop

View File

@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: "node",
}

View File

@@ -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"
}
}

View File

@@ -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

View File

@@ -3,8 +3,17 @@ import middlewares from "../../../middlewares"
const route = Router()
export default app => {
export default (app, container) => {
const middlewareService = container.resolve("middlewareService")
app.use("/customers", route)
route.param("id", middlewares.wrap(require("./authorize-customer").default))
// Inject plugin routes
const routers = middlewareService.getRouters("store/customers")
for (const router of routers) {
route.use("/", router)
}
route.post("/", middlewares.wrap(require("./create-customer").default))
@@ -21,8 +30,6 @@ export default app => {
// Authenticated endpoints
route.use(middlewares.authenticate())
route.param("id", middlewares.wrap(require("./authorize-customer").default))
route.get("/:id", middlewares.wrap(require("./get-customer").default))
route.post("/:id", middlewares.wrap(require("./update-customer").default))

View File

@@ -27,7 +27,7 @@ export default (app, container, config) => {
route.use(middlewares.authenticateCustomer())
authRoutes(route)
customerRoutes(route)
customerRoutes(route, container)
productRoutes(route)
orderRoutes(route)
cartRoutes(route)

View File

@@ -52,6 +52,7 @@ export default ({ rootDirectory, container, app }) => {
registerServices(pluginDetails, container)
registerMedusaApi(pluginDetails, container)
registerApi(pluginDetails, app)
registerCoreRouters(pluginDetails, container)
registerSubscribers(pluginDetails, container)
})
}
@@ -84,6 +85,29 @@ function registerMedusaMiddleware(pluginDetails, container) {
}
}
function registerCoreRouters(pluginDetails, container) {
const middlewareService = container.resolve("middlewareService")
const { resolve } = pluginDetails
const adminFiles = glob.sync(`${resolve}/api/admin/[!__]*.js`, {})
const storeFiles = glob.sync(`${resolve}/api/store/[!__]*.js`, {})
adminFiles.forEach(fn => {
const descriptor = fn.split(".")[0]
const splat = descriptor.split("/")
const path = `${splat[splat.length - 2]}/${splat[splat.length - 1]}`
const loaded = require(fn).default
middlewareService.addRouter(path, loaded())
})
storeFiles.forEach(fn => {
const descriptor = fn.split(".")[0]
const splat = descriptor.split("/")
const path = `${splat[splat.length - 2]}/${splat[splat.length - 1]}`
const loaded = require(fn).default
middlewareService.addRouter(path, loaded())
})
}
/**
* Registers the plugin's api routes.
*/

View File

@@ -7,6 +7,17 @@ class MiddlewareService {
constructor(container) {
this.postAuthentication_ = []
this.preAuthentication_ = []
this.routers = {}
}
addRouter(path, router) {
const existing = this.routers[path] || []
this.routers[path] = [...existing, router]
}
getRouters(path) {
const routers = this.routers[path] || []
return routers
}
/**