Adds customer routes to admin

This commit is contained in:
oliverheaps
2020-07-09 10:28:58 +02:00
parent ceac726412
commit 782869a46b
12 changed files with 111 additions and 19 deletions
@@ -0,0 +1,25 @@
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
email: Validator.string()
.email()
.required(),
first_name: Validator.string().required(),
last_name: Validator.string().required(),
password: Validator.string().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")
const customer = await customerService.create(value)
const data = await customerService.decorate(customer, ["_id", "email"])
res.status(201).json({ customer: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,16 @@
export default async (req, res) => {
const { id } = req.params
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id)
customer = customerService.decorate(customer, [
"email",
"first_name",
"last_name",
])
console.log(customer)
res.json({ customer })
} catch (err) {
throw err
}
}
@@ -0,0 +1,18 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/customers", route)
route.get("/", middlewares.wrap(require("./list-customers").default))
route.get("/:id", middlewares.wrap(require("./get-customer").default))
route.post("/", middlewares.wrap(require("./create-customer").default))
route.post(
"/:id/password",
middlewares.wrap(require("./update-password").default)
)
return app
}
@@ -0,0 +1,12 @@
export default async (req, res) => {
const selector = {}
try {
const customerService = req.scope.resolve("customerService")
const customers = await customerService.list(selector)
res.json({ customers })
} catch (error) {
throw error
}
}
@@ -0,0 +1,27 @@
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
first_name: Validator.string(),
last_name: Validator.string(),
password: Validator.string(),
})
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")
await customerService.update(id, value)
const customer = await customerService.retrieve(id)
const data = await customerService.decorate(customer)
res.status(200).json({ customer: data })
} catch (err) {
throw err
}
}
@@ -12,6 +12,7 @@ import discountRoutes from "./discounts"
import orderRoutes from "./orders"
import storeRoutes from "./store"
import uploadRoutes from "./uploads"
import customerRoutes from "./customers"
const route = Router()
@@ -48,6 +49,7 @@ export default (app, container, config) => {
orderRoutes(route)
storeRoutes(route)
uploadRoutes(route)
customerRoutes(route)
return app
}
@@ -3,6 +3,11 @@ export default async (req, res) => {
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id)
customer = customerService.decorate(customer._id, [
"email",
"first_name",
"last_name",
])
res.json({ customer })
} catch (err) {
throw err
@@ -6,7 +6,11 @@ const route = Router()
export default app => {
app.use("/customers", route)
route.get("/", middlewares.wrap(require("./list-customers").default))
route.get("/:id", middlewares.wrap(require("./get-customer").default))
route.post("/", middlewares.wrap(require("./create-customer").default))
route.post(
"/password-reset",
middlewares.wrap(require("./reset-password").default)