From 25ed7dc0568cbd2df25a4ab5afe0c1e300c6127a Mon Sep 17 00:00:00 2001 From: Nick Ni <32442881+bbpm99@users.noreply.github.com> Date: Mon, 25 Oct 2021 11:48:11 -0700 Subject: [PATCH] chore: Make packages/medusa/src/api/routes/admin/notifications pass linting (#675) Co-authored-by: Nicholas Ni --- .eslintignore | 1 - .../api/routes/admin/notifications/index.js | 2 +- .../admin/notifications/list-notifications.js | 106 +++++++++--------- .../notifications/resend-notification.js | 30 +++-- 4 files changed, 65 insertions(+), 74 deletions(-) diff --git a/.eslintignore b/.eslintignore index 1103372084..e379ab377c 100644 --- a/.eslintignore +++ b/.eslintignore @@ -30,7 +30,6 @@ /packages/medusa/src/api/routes/admin/discounts /packages/medusa/src/api/routes/admin/draft-orders /packages/medusa/src/api/routes/admin/notes -/packages/medusa/src/api/routes/admin/notifications /packages/medusa/src/api/routes/admin/orders /packages/medusa/src/api/routes/admin/return-reasons /packages/medusa/src/api/routes/admin/returns diff --git a/packages/medusa/src/api/routes/admin/notifications/index.js b/packages/medusa/src/api/routes/admin/notifications/index.js index a442e895ba..b6549833f4 100644 --- a/packages/medusa/src/api/routes/admin/notifications/index.js +++ b/packages/medusa/src/api/routes/admin/notifications/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/notifications", route) /** diff --git a/packages/medusa/src/api/routes/admin/notifications/list-notifications.js b/packages/medusa/src/api/routes/admin/notifications/list-notifications.js index 513f0e6b88..8b9799656a 100644 --- a/packages/medusa/src/api/routes/admin/notifications/list-notifications.js +++ b/packages/medusa/src/api/routes/admin/notifications/list-notifications.js @@ -21,63 +21,59 @@ import { defaultRelations, defaultFields } from "./" * $ref: "#/components/schemas/notification" */ export default async (req, res) => { - try { - const notificationService = req.scope.resolve("notificationService") + const notificationService = req.scope.resolve("notificationService") - 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 - let selector = {} + const selector = {} - let includeFields = [] - if ("fields" in req.query) { - includeFields = req.query.fields.split(",") - } - - let expandFields = [] - if ("expand" in req.query) { - expandFields = req.query.expand.split(",") - } - - if ("event_name" in req.query) { - const values = req.query.event_name.split(",") - selector.event_name = values.length > 1 ? values : values[0] - } - - if ("resource_type" in req.query) { - const values = req.query.resource_type.split(",") - selector.resource_type = values.length > 1 ? values : values[0] - } - - if ("resource_id" in req.query) { - const values = req.query.resource_id.split(",") - selector.resource_id = values.length > 1 ? values : values[0] - } - - if ("to" in req.query) { - const values = req.query.to.split(",") - selector.to = values.length > 1 ? values : values[0] - } - - if (!("include_resends" in req.query)) { - selector.parent_id = null - } - - const listConfig = { - select: includeFields.length ? includeFields : defaultFields, - relations: expandFields.length ? expandFields : defaultRelations, - skip: offset, - take: limit, - order: { created_at: "DESC" }, - } - - const notifications = await notificationService.list(selector, listConfig) - - const fields = [...listConfig.select, ...listConfig.relations] - const data = notifications.map(o => _.pick(o, fields)) - - res.json({ notifications: data, offset, limit }) - } catch (error) { - throw error + let includeFields = [] + if ("fields" in req.query) { + includeFields = req.query.fields.split(",") } + + let expandFields = [] + if ("expand" in req.query) { + expandFields = req.query.expand.split(",") + } + + if ("event_name" in req.query) { + const values = req.query.event_name.split(",") + selector.event_name = values.length > 1 ? values : values[0] + } + + if ("resource_type" in req.query) { + const values = req.query.resource_type.split(",") + selector.resource_type = values.length > 1 ? values : values[0] + } + + if ("resource_id" in req.query) { + const values = req.query.resource_id.split(",") + selector.resource_id = values.length > 1 ? values : values[0] + } + + if ("to" in req.query) { + const values = req.query.to.split(",") + selector.to = values.length > 1 ? values : values[0] + } + + if (!("include_resends" in req.query)) { + selector.parent_id = null + } + + const listConfig = { + select: includeFields.length ? includeFields : defaultFields, + relations: expandFields.length ? expandFields : defaultRelations, + skip: offset, + take: limit, + order: { created_at: "DESC" }, + } + + const notifications = await notificationService.list(selector, listConfig) + + const fields = [...listConfig.select, ...listConfig.relations] + const data = notifications.map((o) => _.pick(o, fields)) + + res.json({ notifications: data, offset, limit }) } diff --git a/packages/medusa/src/api/routes/admin/notifications/resend-notification.js b/packages/medusa/src/api/routes/admin/notifications/resend-notification.js index 92f94220c6..a701ae5725 100644 --- a/packages/medusa/src/api/routes/admin/notifications/resend-notification.js +++ b/packages/medusa/src/api/routes/admin/notifications/resend-notification.js @@ -32,24 +32,20 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const notificationService = req.scope.resolve("notificationService") + const notificationService = req.scope.resolve("notificationService") - const config = {} + const config = {} - if (value.to) { - config.to = value.to - } - - await notificationService.resend(id, config) - - const notification = await notificationService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) - - res.json({ notification }) - } catch (error) { - throw error + if (value.to) { + config.to = value.to } + + await notificationService.resend(id, config) + + const notification = await notificationService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) + + res.json({ notification }) }