fix: creates restock functionality

This commit is contained in:
Sebastian Rindom
2021-04-06 12:22:19 +02:00
parent 0a80b098a4
commit 2b2555004e
17 changed files with 2673 additions and 259 deletions

View File

@@ -0,0 +1,10 @@
import { Router } from "express"
import routes from "./routes"
export default (container) => {
const app = Router()
routes(app)
return app
}

View File

@@ -0,0 +1 @@
export default (fn) => (...args) => fn(...args).catch(args[2])

View File

@@ -0,0 +1,5 @@
import { default as wrap } from "./await-middleware"
export default {
wrap,
}

View File

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

View File

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