diff --git a/.eslintignore b/.eslintignore index 3a80da02c6..545854965a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -26,7 +26,6 @@ /packages/medusa/src/api/routes/admin/apps /packages/medusa/src/api/routes/admin/auth /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/notes /packages/medusa/src/api/routes/admin/orders diff --git a/packages/medusa/src/api/routes/admin/customers/create-customer.js b/packages/medusa/src/api/routes/admin/customers/create-customer.js index e21d078275..359ddc7b75 100644 --- a/packages/medusa/src/api/routes/admin/customers/create-customer.js +++ b/packages/medusa/src/api/routes/admin/customers/create-customer.js @@ -24,9 +24,7 @@ import { Validator, MedusaError } from "medusa-core-utils" */ export default async (req, res) => { const schema = Validator.object().keys({ - email: Validator.string() - .email() - .required(), + email: Validator.string().email().required(), first_name: Validator.string().required(), last_name: Validator.string().required(), password: Validator.string().required(), @@ -37,11 +35,8 @@ export default async (req, res) => { if (error) { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const customerService = req.scope.resolve("customerService") - const customer = await customerService.create(value) - res.status(201).json({ customer }) - } catch (err) { - throw err - } + + const customerService = req.scope.resolve("customerService") + const customer = await customerService.create(value) + res.status(201).json({ customer }) } diff --git a/packages/medusa/src/api/routes/admin/customers/get-customer.js b/packages/medusa/src/api/routes/admin/customers/get-customer.js index 285b815808..7b0d0ce9b0 100644 --- a/packages/medusa/src/api/routes/admin/customers/get-customer.js +++ b/packages/medusa/src/api/routes/admin/customers/get-customer.js @@ -19,14 +19,10 @@ */ export default async (req, res) => { const { id } = req.params - try { - const customerService = req.scope.resolve("customerService") - const customer = await customerService.retrieve(id, { - relations: ["orders", "shipping_addresses"], - }) + const customerService = req.scope.resolve("customerService") + const customer = await customerService.retrieve(id, { + relations: ["orders", "shipping_addresses"], + }) - res.json({ customer }) - } catch (err) { - throw err - } + res.json({ customer }) } diff --git a/packages/medusa/src/api/routes/admin/customers/index.js b/packages/medusa/src/api/routes/admin/customers/index.js index 9b0ad6e5a6..c91f7bbab0 100644 --- a/packages/medusa/src/api/routes/admin/customers/index.js +++ b/packages/medusa/src/api/routes/admin/customers/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/customers", route) route.get("/", middlewares.wrap(require("./list-customers").default)) diff --git a/packages/medusa/src/api/routes/admin/customers/list-customers.js b/packages/medusa/src/api/routes/admin/customers/list-customers.js index 1d7c64d9f2..ae28413b5a 100644 --- a/packages/medusa/src/api/routes/admin/customers/list-customers.js +++ b/packages/medusa/src/api/routes/admin/customers/list-customers.js @@ -16,36 +16,32 @@ * $ref: "#/components/schemas/customer" */ 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 offset = parseInt(req.query.offset) || 0 + const limit = parseInt(req.query.limit) || 50 + const offset = parseInt(req.query.offset) || 0 - const selector = {} + const selector = {} - if ("q" in req.query) { - 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 + if ("q" in req.query) { + 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 }) } diff --git a/packages/medusa/src/api/routes/admin/customers/update-customer.js b/packages/medusa/src/api/routes/admin/customers/update-customer.js index bc3dd3ff67..601840e099 100644 --- a/packages/medusa/src/api/routes/admin/customers/update-customer.js +++ b/packages/medusa/src/api/routes/admin/customers/update-customer.js @@ -52,25 +52,21 @@ export default async (req, res) => { 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) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - "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 + if (value.email && customer.has_account) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + "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 }) }