chore: Make packages/medusa/src/api/routes/admin/customers pass linting (#681)

This commit is contained in:
Luca Pizzini authored and GitHub committed 2021-10-26 19:55:08 +02:00
1 parent 68c2a4fe3a
commit 748270a75d
6 files changed
+49 -67

No files matched your search

-1
View File
@@ -26,7 +26,6 @@
/packages/medusa/src/api/routes/admin/apps /packages/medusa/src/api/routes/admin/apps
/packages/medusa/src/api/routes/admin/auth /packages/medusa/src/api/routes/admin/auth
/packages/medusa/src/api/routes/admin/collections /packages/medusa/src/api/routes/admin/collections
/packages/medusa/src/api/routes/admin/customers
/packages/medusa/src/api/routes/admin/draft-orders /packages/medusa/src/api/routes/admin/draft-orders
/packages/medusa/src/api/routes/admin/notes /packages/medusa/src/api/routes/admin/notes
/packages/medusa/src/api/routes/admin/orders /packages/medusa/src/api/routes/admin/orders
@@ -24,9 +24,7 @@ import { Validator, MedusaError } from "medusa-core-utils"
*/ */
export default async (req, res) => { export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
email: Validator.string() email: Validator.string().email().required(),
.email()
.required(),
first_name: Validator.string().required(), first_name: Validator.string().required(),
last_name: Validator.string().required(), last_name: Validator.string().required(),
password: Validator.string().required(), password: Validator.string().required(),
@@ -37,11 +35,8 @@ export default async (req, res) => {
if (error) { if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
} }
try {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
const customer = await customerService.create(value) const customer = await customerService.create(value)
res.status(201).json({ customer }) res.status(201).json({ customer })
} catch (err) {
throw err
}
} }
@@ -19,14 +19,10 @@
*/ */
export default async (req, res) => { export default async (req, res) => {
const { id } = req.params const { id } = req.params
try { const customerService = req.scope.resolve("customerService")
const customerService = req.scope.resolve("customerService") const customer = await customerService.retrieve(id, {
const customer = await customerService.retrieve(id, { relations: ["orders", "shipping_addresses"],
relations: ["orders", "shipping_addresses"], })
})
res.json({ customer }) res.json({ customer })
} catch (err) {
throw err
}
} }
@@ -3,7 +3,7 @@ import middlewares from "../../../middlewares"
const route = Router() const route = Router()
export default app => { export default (app) => {
app.use("/customers", route) app.use("/customers", route)
route.get("/", middlewares.wrap(require("./list-customers").default)) route.get("/", middlewares.wrap(require("./list-customers").default))
@@ -16,36 +16,32 @@
* $ref: "#/components/schemas/customer" * $ref: "#/components/schemas/customer"
*/ */
export default async (req, res) => { export default async (req, res) => {
try { const customerService = req.scope.resolve("customerService")
const customerService = req.scope.resolve("customerService")
const limit = parseInt(req.query.limit) || 50 const limit = parseInt(req.query.limit) || 50
const offset = parseInt(req.query.offset) || 0 const offset = parseInt(req.query.offset) || 0
const selector = {} const selector = {}
if ("q" in req.query) { if ("q" in req.query) {
selector.q = req.query.q selector.q = req.query.q
}
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
}
const listConfig = {
relations: expandFields.length ? expandFields : [],
skip: offset,
take: limit,
}
const [customers, count] = await customerService.listAndCount(
selector,
listConfig
)
res.json({ customers, count, offset, limit })
} catch (error) {
throw error
} }
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
}
const listConfig = {
relations: expandFields.length ? expandFields : [],
skip: offset,
take: limit,
}
const [customers, count] = await customerService.listAndCount(
selector,
listConfig
)
res.json({ customers, count, offset, limit })
} }
@@ -52,25 +52,21 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
} }
try { const customerService = req.scope.resolve("customerService")
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id) let customer = await customerService.retrieve(id)
if (value.email && customer.has_account) { if (value.email && customer.has_account) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
"Email cannot be changed when the user has registered their account" "Email cannot be changed when the user has registered their account"
) )
}
await customerService.update(id, value)
customer = await customerService.retrieve(id, {
relations: ["orders"],
})
res.status(200).json({ customer })
} catch (err) {
throw err
} }
await customerService.update(id, value)
customer = await customerService.retrieve(id, {
relations: ["orders"],
})
res.status(200).json({ customer })
} }