Fix linting errors in routes admin product (#656)

This commit is contained in:
Daniel Tiringer authored and GitHub committed 2021-10-25 20:01:43 +02:00
1 parent 623d5f2b6d
commit e97367d26e
15 files changed
+55 -201

No files matched your search

-1
View File
@@ -35,7 +35,6 @@
/packages/medusa/src/api/routes/admin/notes /packages/medusa/src/api/routes/admin/notes
/packages/medusa/src/api/routes/admin/notifications /packages/medusa/src/api/routes/admin/notifications
/packages/medusa/src/api/routes/admin/orders /packages/medusa/src/api/routes/admin/orders
/packages/medusa/src/api/routes/admin/products
/packages/medusa/src/api/routes/admin/regions /packages/medusa/src/api/routes/admin/regions
/packages/medusa/src/api/routes/admin/return-reasons /packages/medusa/src/api/routes/admin/return-reasons
/packages/medusa/src/api/routes/admin/returns /packages/medusa/src/api/routes/admin/returns
@@ -39,7 +39,6 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
await productService.addOption(id, value.title) await productService.addOption(id, value.title)
@@ -49,7 +48,4 @@ export default async (req, res) => {
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }
@@ -188,9 +188,7 @@ export default async (req, res) => {
description: Validator.string().allow(""), description: Validator.string().allow(""),
is_giftcard: Validator.boolean().default(false), is_giftcard: Validator.boolean().default(false),
discountable: Validator.boolean().default(true), discountable: Validator.boolean().default(true),
images: Validator.array() images: Validator.array().items(Validator.string()).optional(),
.items(Validator.string())
.optional(),
thumbnail: Validator.string().optional(), thumbnail: Validator.string().optional(),
handle: Validator.string().optional(), handle: Validator.string().optional(),
status: Validator.string() status: Validator.string()
@@ -203,9 +201,7 @@ export default async (req, res) => {
}) })
.allow(null) .allow(null)
.optional(), .optional(),
collection_id: Validator.string() collection_id: Validator.string().allow(null).optional(),
.allow(null)
.optional(),
tags: Validator.array() tags: Validator.array()
.items({ .items({
id: Validator.string().optional(), id: Validator.string().optional(),
@@ -225,30 +221,13 @@ export default async (req, res) => {
inventory_quantity: Validator.number().default(0), inventory_quantity: Validator.number().default(0),
allow_backorder: Validator.boolean().optional(), allow_backorder: Validator.boolean().optional(),
manage_inventory: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null) origin_country: Validator.string().optional().allow("").allow(null),
.optional(), mid_code: Validator.string().optional().allow("").allow(null),
height: Validator.number() material: Validator.string().optional().allow("").allow(null),
.allow(null)
.optional(),
width: Validator.number()
.allow(null)
.optional(),
origin_country: Validator.string()
.optional()
.allow("")
.allow(null),
mid_code: Validator.string()
.optional()
.allow("")
.allow(null),
material: Validator.string()
.optional()
.allow("")
.allow(null),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
prices: Validator.array() prices: Validator.array()
.items( .items(
@@ -256,9 +235,7 @@ export default async (req, res) => {
.keys({ .keys({
region_id: Validator.string(), region_id: Validator.string(),
currency_code: Validator.string(), currency_code: Validator.string(),
amount: Validator.number() amount: Validator.number().integer().required(),
.integer()
.required(),
sale_amount: Validator.number().optional(), sale_amount: Validator.number().optional(),
}) })
.xor("region_id", "currency_code") .xor("region_id", "currency_code")
@@ -270,30 +247,14 @@ export default async (req, res) => {
}) })
.default([]), .default([]),
}), }),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null) hs_code: Validator.string().optional().allow(""),
.optional(), origin_country: Validator.string().optional().allow(""),
height: Validator.number() mid_code: Validator.string().optional().allow(""),
.allow(null) material: Validator.string().optional().allow(""),
.optional(),
width: Validator.number()
.allow(null)
.optional(),
hs_code: Validator.string()
.optional()
.allow(""),
origin_country: Validator.string()
.optional()
.allow(""),
mid_code: Validator.string()
.optional()
.allow(""),
material: Validator.string()
.optional()
.allow(""),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -302,7 +263,6 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const productVariantService = req.scope.resolve("productVariantService") const productVariantService = req.scope.resolve("productVariantService")
const shippingProfileService = req.scope.resolve("shippingProfileService") const shippingProfileService = req.scope.resolve("shippingProfileService")
@@ -310,7 +270,7 @@ export default async (req, res) => {
const entityManager = req.scope.resolve("manager") const entityManager = req.scope.resolve("manager")
let newProduct let newProduct
await entityManager.transaction(async manager => { await entityManager.transaction(async (manager) => {
const { variants } = value const { variants } = value
delete value.variants delete value.variants
@@ -331,14 +291,16 @@ export default async (req, res) => {
.create({ ...value, profile_id: shippingProfile.id }) .create({ ...value, profile_id: shippingProfile.id })
if (variants) { if (variants) {
for (const [i, variant] of variants.entries()) variant.variant_rank = i for (const [i, variant] of variants.entries()) {
variant.variant_rank = i
}
const optionIds = value.options.map( const optionIds = value.options.map(
o => newProduct.options.find(newO => newO.title === o.title).id (o) => newProduct.options.find((newO) => newO.title === o.title).id
) )
await Promise.all( await Promise.all(
variants.map(async v => { variants.map(async (v) => {
const variant = { const variant = {
...v, ...v,
options: v.options.map((o, index) => ({ options: v.options.map((o, index) => ({
@@ -361,7 +323,4 @@ export default async (req, res) => {
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }
@@ -115,18 +115,10 @@ export default async (req, res) => {
inventory_quantity: Validator.number().default(0), inventory_quantity: Validator.number().default(0),
allow_backorder: Validator.boolean().optional(), allow_backorder: Validator.boolean().optional(),
manage_inventory: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null)
.optional(),
height: Validator.number()
.allow(null)
.optional(),
width: Validator.number()
.allow(null)
.optional(),
origin_country: Validator.string().allow(""), origin_country: Validator.string().allow(""),
mid_code: Validator.string().allow(""), mid_code: Validator.string().allow(""),
material: Validator.string().allow(""), material: Validator.string().allow(""),
@@ -137,9 +129,7 @@ export default async (req, res) => {
.keys({ .keys({
region_id: Validator.string().empty(null), region_id: Validator.string().empty(null),
currency_code: Validator.string().required(), currency_code: Validator.string().required(),
amount: Validator.number() amount: Validator.number().integer().required(),
.integer()
.required(),
sale_amount: Validator.number().optional(), sale_amount: Validator.number().optional(),
}) })
.xor("region_id", "currency_code") .xor("region_id", "currency_code")
@@ -158,7 +148,6 @@ 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 productVariantService = req.scope.resolve("productVariantService") const productVariantService = req.scope.resolve("productVariantService")
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
@@ -170,7 +159,4 @@ export default async (req, res) => {
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }
@@ -31,7 +31,6 @@ import { defaultRelations, defaultFields } from "."
export default async (req, res) => { export default async (req, res) => {
const { id, option_id } = req.params const { id, option_id } = req.params
try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
await productService.deleteOption(id, option_id) await productService.deleteOption(id, option_id)
const data = await productService.retrieve(id, { const data = await productService.retrieve(id, {
@@ -45,7 +44,4 @@ export default async (req, res) => {
deleted: true, deleted: true,
product: data, product: data,
}) })
} catch (err) {
throw err
}
} }
@@ -26,7 +26,6 @@
export default async (req, res) => { export default async (req, res) => {
const { id } = req.params const { id } = req.params
try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
await productService.delete(id) await productService.delete(id)
res.json({ res.json({
@@ -34,7 +33,4 @@ export default async (req, res) => {
object: "product", object: "product",
deleted: true, deleted: true,
}) })
} catch (err) {
throw err
}
} }
@@ -29,7 +29,6 @@ import { defaultRelations, defaultFields } from "."
export default async (req, res) => { export default async (req, res) => {
const { id, variant_id } = req.params const { id, variant_id } = req.params
try {
const productVariantService = req.scope.resolve("productVariantService") const productVariantService = req.scope.resolve("productVariantService")
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
@@ -46,7 +45,4 @@ export default async (req, res) => {
deleted: true, deleted: true,
product: data, product: data,
}) })
} 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("/products", route) app.use("/products", route)
route.post("/", middlewares.wrap(require("./create-product").default)) route.post("/", middlewares.wrap(require("./create-product").default))
@@ -1,4 +1,3 @@
import _ from "lodash"
import { MedusaError, Validator } from "medusa-core-utils" import { MedusaError, Validator } from "medusa-core-utils"
import { defaultFields, defaultRelations, filterableFields } from "./" import { defaultFields, defaultRelations, filterableFields } from "./"
@@ -42,7 +41,6 @@ export default async (req, res) => {
) )
} }
try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const limit = parseInt(req.query.limit) || 50 const limit = parseInt(req.query.limit) || 50
@@ -84,10 +82,7 @@ export default async (req, res) => {
take: limit, take: limit,
} }
let products = await productService.list(selector, listConfig) const products = await productService.list(selector, listConfig)
res.json({ products, count: products.length, offset, limit }) res.json({ products, count: products.length, offset, limit })
} catch (error) {
throw error
}
} }
@@ -1,11 +1,7 @@
export default async (req, res) => { export default async (req, res) => {
try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const tags = await productService.listTagsByUsage() const tags = await productService.listTagsByUsage()
res.json({ tags }) res.json({ tags })
} catch (error) {
throw error
}
} }
@@ -18,13 +18,9 @@
* $ref: "#/components/schemas/product_type" * $ref: "#/components/schemas/product_type"
*/ */
export default async (req, res) => { export default async (req, res) => {
try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const types = await productService.listTypes() const types = await productService.listTypes()
res.json({ types }) res.json({ types })
} catch (error) {
throw error
}
} }
@@ -14,7 +14,6 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
await productService.update(id, { await productService.update(id, {
metadata: { [value.key]: value.value }, metadata: { [value.key]: value.value },
@@ -26,7 +25,4 @@ export default async (req, res) => {
}) })
res.status(200).json({ product }) res.status(200).json({ product })
} catch (err) {
throw err
}
} }
@@ -41,7 +41,6 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
await productService.updateOption(id, option_id, value) await productService.updateOption(id, option_id, value)
@@ -52,7 +51,4 @@ export default async (req, res) => {
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }
@@ -188,9 +188,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
title: Validator.string().optional(), title: Validator.string().optional(),
subtitle: Validator.string() subtitle: Validator.string().optional().allow(null, ""),
.optional()
.allow(null, ""),
description: Validator.string().optional(), description: Validator.string().optional(),
discountable: Validator.boolean().optional(), discountable: Validator.boolean().optional(),
status: Validator.string().valid( status: Validator.string().valid(
@@ -206,9 +204,7 @@ export default async (req, res) => {
}) })
.allow(null) .allow(null)
.optional(), .optional(),
collection_id: Validator.string() collection_id: Validator.string().allow(null).optional(),
.allow(null)
.optional(),
tags: Validator.array() tags: Validator.array()
.items({ .items({
id: Validator.string().optional(), id: Validator.string().optional(),
@@ -216,26 +212,15 @@ export default async (req, res) => {
}) })
.optional(), .optional(),
handle: Validator.string().optional(), handle: Validator.string().optional(),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null)
.optional(),
height: Validator.number()
.allow(null)
.optional(),
width: Validator.number()
.allow(null)
.optional(),
origin_country: Validator.string().allow(null, ""), origin_country: Validator.string().allow(null, ""),
hs_code: Validator.string().allow(null, ""), hs_code: Validator.string().allow(null, ""),
mid_code: Validator.string().allow(null, ""), mid_code: Validator.string().allow(null, ""),
material: Validator.string().allow(null, ""), material: Validator.string().allow(null, ""),
images: Validator.array() images: Validator.array().items(Validator.string()).optional().optional(),
.items(Validator.string())
.optional()
.optional(),
thumbnail: Validator.string().optional(), thumbnail: Validator.string().optional(),
variants: Validator.array() variants: Validator.array()
.items({ .items({
@@ -249,9 +234,7 @@ export default async (req, res) => {
.keys({ .keys({
region_id: Validator.string(), region_id: Validator.string(),
currency_code: Validator.string(), currency_code: Validator.string(),
amount: Validator.number() amount: Validator.number().integer().required(),
.integer()
.required(),
sale_amount: Validator.number().optional(), sale_amount: Validator.number().optional(),
}) })
.xor("region_id", "currency_code") .xor("region_id", "currency_code")
@@ -266,21 +249,11 @@ export default async (req, res) => {
inventory_quantity: Validator.number().allow(null), inventory_quantity: Validator.number().allow(null),
allow_backorder: Validator.boolean().allow(null), allow_backorder: Validator.boolean().allow(null),
manage_inventory: Validator.boolean().allow(null), manage_inventory: Validator.boolean().allow(null),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null) hs_code: Validator.string().optional().allow(null, ""),
.optional(),
height: Validator.number()
.allow(null)
.optional(),
width: Validator.number()
.allow(null)
.optional(),
hs_code: Validator.string()
.optional()
.allow(null, ""),
origin_country: Validator.string().allow(null, ""), origin_country: Validator.string().allow(null, ""),
mid_code: Validator.string().allow(null, ""), mid_code: Validator.string().allow(null, ""),
material: Validator.string().allow(null, ""), material: Validator.string().allow(null, ""),
@@ -295,11 +268,10 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const entityManager = req.scope.resolve("manager") const entityManager = req.scope.resolve("manager")
await entityManager.transaction(async manager => { await entityManager.transaction(async (manager) => {
await productService.withTransaction(manager).update(id, value) await productService.withTransaction(manager).update(id, value)
}) })
@@ -309,7 +281,4 @@ export default async (req, res) => {
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }
@@ -1,4 +1,3 @@
import _ from "lodash"
import { MedusaError, Validator } from "medusa-core-utils" import { MedusaError, Validator } from "medusa-core-utils"
import { defaultFields, defaultRelations } from "./" import { defaultFields, defaultRelations } from "./"
@@ -116,9 +115,7 @@ export default async (req, res) => {
.keys({ .keys({
region_id: Validator.string().empty(null), region_id: Validator.string().empty(null),
currency_code: Validator.string(), currency_code: Validator.string(),
amount: Validator.number() amount: Validator.number().integer().required(),
.integer()
.required(),
sale_amount: Validator.number().optional(), sale_amount: Validator.number().optional(),
}) })
.xor("region_id", "currency_code") .xor("region_id", "currency_code")
@@ -133,30 +130,14 @@ export default async (req, res) => {
inventory_quantity: Validator.number().optional(), inventory_quantity: Validator.number().optional(),
allow_backorder: Validator.boolean().optional(), allow_backorder: Validator.boolean().optional(),
manage_inventory: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(),
weight: Validator.number() weight: Validator.number().allow(null).optional(),
.allow(null) length: Validator.number().allow(null).optional(),
.optional(), height: Validator.number().allow(null).optional(),
length: Validator.number() width: Validator.number().allow(null).optional(),
.allow(null) hs_code: Validator.string().optional().allow(null, ""),
.optional(), origin_country: Validator.string().optional().allow(null, ""),
height: Validator.number() mid_code: Validator.string().optional().allow(null, ""),
.allow(null) material: Validator.string().optional().allow(null, ""),
.optional(),
width: Validator.number()
.allow(null)
.optional(),
hs_code: Validator.string()
.optional()
.allow(null, ""),
origin_country: Validator.string()
.optional()
.allow(null, ""),
mid_code: Validator.string()
.optional()
.allow(null, ""),
material: Validator.string()
.optional()
.allow(null, ""),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -165,7 +146,6 @@ 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 productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const productVariantService = req.scope.resolve("productVariantService") const productVariantService = req.scope.resolve("productVariantService")
@@ -175,8 +155,6 @@ export default async (req, res) => {
select: defaultFields, select: defaultFields,
relations: defaultRelations, relations: defaultRelations,
}) })
res.json({ product }) res.json({ product })
} catch (err) {
throw err
}
} }