fix: creates restock functionality
This commit is contained in:
10
packages/medusa-plugin-restock-notification/src/api/index.js
Normal file
10
packages/medusa-plugin-restock-notification/src/api/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Router } from "express"
|
||||
import routes from "./routes"
|
||||
|
||||
export default (container) => {
|
||||
const app = Router()
|
||||
|
||||
routes(app)
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export default (fn) => (...args) => fn(...args).catch(args[2])
|
||||
@@ -0,0 +1,5 @@
|
||||
import { default as wrap } from "./await-middleware"
|
||||
|
||||
export default {
|
||||
wrap,
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { variant_id } = req.parmas
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
email: Validator.string().required(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
try {
|
||||
const restockNotificationService = req.scope.resolve(
|
||||
"restockNotificationService"
|
||||
)
|
||||
await restockNotificationService.addEmail(variant_id, value.email)
|
||||
res.sendStatus(200)
|
||||
} catch (err) {
|
||||
res.sendStatus(400).json({ message: err.message })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Router } from "express"
|
||||
import bodyParser from "body-parser"
|
||||
import middlewares from "../middleware"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app) => {
|
||||
app.use("/restock-notifications", route)
|
||||
|
||||
route.post(
|
||||
"/variants/:variant_id",
|
||||
bodyParser.raw({ type: "application/json" }),
|
||||
middlewares.wrap(require("./add-email").default)
|
||||
)
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user