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

View File

@@ -35,7 +35,6 @@
"@babel/runtime": "^7.9.6",
"@hapi/joi": "^16.1.8",
"chalk": "^4.0.0",
"clipboardy": "^2.3.0",
"core-js": "^3.6.5",
"fs-exists-cached": "^1.0.0",
"joi-objectid": "^3.0.1",
@@ -46,4 +45,4 @@
"yargs": "^15.3.1"
},
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
}
}

View File

@@ -3,9 +3,7 @@ const resolveCwd = require(`resolve-cwd`)
const yargs = require(`yargs`)
const { getLocalMedusaVersion } = require(`./util/version`)
const { didYouMean } = require(`./did-you-mean`)
const envinfo = require(`envinfo`)
const existsSync = require(`fs-exists-cached`).sync
const clipboardy = require(`clipboardy`)
const handlerP = fn => (...args) => {
Promise.resolve(fn(...args)).then(

View File

@@ -1247,11 +1247,6 @@ anymatch@^3.0.3:
normalize-path "^3.0.0"
picomatch "^2.0.4"
arch@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/arch/-/arch-2.1.1.tgz#8f5c2731aa35a30929221bb0640eed65175ec84e"
integrity sha512-BLM56aPo9vLLFVa8+/+pJLnrZ7QGGTVHWsCwieAWT9o9K8UeGaQbzZbGoabWLOo2ksBCztoXdqBZBplqLDDCSg==
argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@@ -1616,15 +1611,6 @@ cli-width@^2.0.0:
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.1.tgz#b0433d0b4e9c847ef18868a4ef16fd5fc8271c48"
integrity sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw==
clipboardy@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/clipboardy/-/clipboardy-2.3.0.tgz#3c2903650c68e46a91b388985bc2774287dba290"
integrity sha512-mKhiIL2DrQIsuXMgBgnfEHOZOryC7kY7YO//TN6c63wlEm3NG5tz+YgY5rVi29KCmq/QQjKYvM7a19+MDOTHOQ==
dependencies:
arch "^2.1.1"
execa "^1.0.0"
is-wsl "^2.1.1"
cliui@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -255,7 +255,7 @@ class CustomerService extends BaseService {
* @param {string[]} expandFields - fields to expand.
* @return {Customer} return the decorated customer.
*/
async decorate(customer, fields, expandFields = []) {
decorate(customer, fields, expandFields = []) {
const requiredFields = ["_id", "metadata"]
const decorated = _.pick(customer, fields.concat(requiredFields))
return decorated