add error catching and response for customer

This commit is contained in:
Vilfred Sikker committed 2021-08-27 11:10:59 +02:00
1 parent b1132eec23
commit af59095d85
2 files changed
+46 -16

No files matched your search

@@ -1,25 +1,55 @@
import { Router } from "express" import { Router } from "express"
import bodyParser from "body-parser"
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken"
import cors from "cors"
import express from "express"
import { getConfigFile, MedusaError } from "medusa-core-utils"
const app = Router() const app = Router()
export default () => { export default (rootDirectory) => {
app.get("/wishlists/:token", bodyParser.json(), async (req, res) => { const { configModule } = getConfigFile(rootDirectory, "medusa-config")
const { token } = req.params const { projectConfig } = configModule
const corsOptions = {
origin: projectConfig.store_cors.split(","),
credentials: true,
}
const JWT_SECRET = process.env.JWT_SECRET || "" const JWT_SECRET = process.env.JWT_SECRET || ""
// decorde token with decode = jwt.decode(token, secret) app.use("/wishlists/:token", cors(corsOptions))
const decode = jwt.decode(token, JWT_SECRET) app.get(
console.log(decode) "/wishlists/:token",
// fetch customer.retrieve(decode.customer_id) cors(corsOptions),
// get customer.metadata.wishlist express.json(),
async (req, res) => {
const customerService = req.scope.resolve("customerService")
const { token } = req.params
let decode
// respond with try {
// wishlist decode = jwt.decode(token, JWT_SECRET)
// first_name
res.sendStatus(200) if (!decode || !decode.customer_id) {
}) throw new MedusaError(
MedusaError.Types.NOT_FOUND,
"Invalid token",
400
)
}
} catch (err) {
res.status(400).json(err)
}
try {
const customer = await customerService.retrieve(decode.customer_id)
const response = {
wishlist: customer.metadata.wishlist,
first_name: customer.first_name,
}
res.status(200).json({ response })
} catch (err) {
res.status(400).json(err)
}
}
)
return app return app
} }
@@ -93,7 +93,7 @@ export default () => {
throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400) throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400)
} }
const token = jwt.sign( const token = await jwt.sign(
{ {
customer_id: customer.id, customer_id: customer.id,
}, },