fix: make discounts pass eslint

This commit is contained in:
Ronaldo Caetano committed 2021-10-25 22:06:46 -03:00
1 parent a1e446c121
commit 3df2b4279f
14 files changed
+17 -101

No files matched your search

-1
View File
@@ -30,7 +30,6 @@
/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/discounts
/packages/medusa/src/api/routes/admin/draft-orders
/packages/medusa/src/api/routes/admin/gift-cards
/packages/medusa/src/api/routes/admin/notes
@@ -22,9 +22,7 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, region_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.addRegion(discount_id, region_id)
const discount = await discountService.retrieve(discount_id, {
@@ -33,7 +31,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -22,10 +22,7 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, variant_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.addValidProduct(discount_id, variant_id)
const discount = await discountService.retrieve(discount_id, {
@@ -34,7 +31,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -63,47 +63,29 @@ export default async (req, res) => {
.keys({
description: Validator.string().optional(),
type: Validator.string().required(),
value: Validator.number()
.positive()
.required(),
value: Validator.number().positive().required(),
allocation: Validator.string().required(),
valid_for: Validator.array().items(Validator.string()),
})
.required(),
is_disabled: Validator.boolean().default(false),
starts_at: Validator.date().optional(),
ends_at: Validator.date()
.greater(Validator.ref("starts_at"))
.optional(),
valid_duration: Validator.string()
.isoDuration()
.allow(null)
.optional(),
usage_limit: Validator.number()
.positive()
.optional(),
regions: Validator.array()
.items(Validator.string())
.optional(),
ends_at: Validator.date().greater(Validator.ref("starts_at")).optional(),
valid_duration: Validator.string().isoDuration().allow(null).optional(),
usage_limit: Validator.number().positive().optional(),
regions: Validator.array().items(Validator.string()).optional(),
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const discountService = req.scope.resolve("discountService")
const created = await discountService.create(value)
const discount = await discountService.retrieve(
created.id,
defaultRelations
)
const discount = await discountService.retrieve(created.id, defaultRelations)
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -31,11 +31,11 @@ export default async (req, res) => {
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const discountService = req.scope.resolve("discountService")
const created = await discountService.createDynamicCode(discount_id, value)
@@ -44,7 +44,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -25,8 +25,6 @@
*/
export default async (req, res) => {
const { discount_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.delete(discount_id)
@@ -35,7 +33,4 @@ export default async (req, res) => {
object: "discount",
deleted: true,
})
} catch (err) {
throw err
}
}
@@ -20,8 +20,6 @@
*/
export default async (req, res) => {
const { discount_id, code } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.deleteDynamicCode(discount_id, code)
@@ -30,7 +28,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -1,4 +1,4 @@
import { defaultFields, defaultRelations } from "./"
import { defaultRelations } from "./"
/**
* @oas [get] /discounts/code/{code}
@@ -21,15 +21,8 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { code } = req.params
try {
const discountService = req.scope.resolve("discountService")
const discount = await discountService.retrieveByCode(
code,
defaultRelations
)
const discount = await discountService.retrieveByCode(code, defaultRelations)
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -21,7 +21,6 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
const data = await discountService.retrieve(discount_id, {
select: defaultFields,
@@ -29,7 +28,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount: data })
} catch (err) {
throw err
}
}
@@ -3,7 +3,7 @@ import middlewares from "../../../middlewares"
const route = Router()
export default app => {
export default (app) => {
app.use("/discounts", route)
route.get("/", middlewares.wrap(require("./list-discounts").default))
@@ -18,13 +18,10 @@ import { defaultFields, defaultRelations } from "./"
* $ref: "#/components/schemas/discount"
*/
export default async (req, res) => {
try {
const discountService = req.scope.resolve("discountService")
const limit = parseInt(req.query.limit) || 20
const offset = parseInt(req.query.offset) || 0
let selector = {}
const selector = {}
if ("q" in req.query) {
selector.q = req.query.q
@@ -52,7 +49,4 @@ export default async (req, res) => {
)
res.status(200).json({ discounts, count, offset, limit })
} catch (err) {
throw err
}
}
@@ -22,18 +22,13 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, region_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.removeRegion(discount_id, region_id)
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -22,10 +22,7 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, variant_id } = req.params
try {
const discountService = req.scope.resolve("discountService")
await discountService.removeValidProduct(discount_id, variant_id)
const discount = await discountService.retrieve(discount_id, {
@@ -34,7 +31,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}
@@ -70,20 +70,12 @@ export default async (req, res) => {
starts_at: Validator.date().optional(),
ends_at: Validator.when("starts_at", {
not: undefined,
then: Validator.date()
.greater(Validator.ref("starts_at"))
.optional(),
then: Validator.date().greater(Validator.ref("starts_at")).optional(),
otherwise: Validator.date().optional(),
}),
valid_duration: Validator.string()
.isoDuration().allow(null)
.optional(),
usage_limit: Validator.number()
.positive()
.optional(),
regions: Validator.array()
.items(Validator.string())
.optional(),
valid_duration: Validator.string().isoDuration().allow(null).optional(),
usage_limit: Validator.number().positive().optional(),
regions: Validator.array().items(Validator.string()).optional(),
})
const { value, error } = schema.validate(req.body)
@@ -92,9 +84,7 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const discountService = req.scope.resolve("discountService")
await discountService.update(discount_id, value)
const discount = await discountService.retrieve(discount_id, {
@@ -103,7 +93,4 @@ export default async (req, res) => {
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
}