Merge pull request #677 from RonaldoCaetano/fix-discounts

Fix discounts
This commit is contained in:
Sebastian Rindom
2021-10-26 12:00:06 +02:00
committed by GitHub
14 changed files with 109 additions and 193 deletions

View File

@@ -27,7 +27,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/notes
/packages/medusa/src/api/routes/admin/orders

View File

@@ -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")
const discountService = req.scope.resolve("discountService")
await discountService.addRegion(discount_id, region_id)
await discountService.addRegion(discount_id, region_id)
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -22,19 +22,13 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, variant_id } = req.params
const discountService = req.scope.resolve("discountService")
await discountService.addValidProduct(discount_id, variant_id)
try {
const discountService = req.scope.resolve("discountService")
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
await discountService.addValidProduct(discount_id, variant_id)
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -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 discountService = req.scope.resolve("discountService")
const created = await discountService.create(value)
const discount = await discountService.retrieve(created.id, defaultRelations)
const created = await discountService.create(value)
const discount = await discountService.retrieve(
created.id,
defaultRelations
)
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -31,20 +31,17 @@ 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)
const discountService = req.scope.resolve("discountService")
const created = await discountService.createDynamicCode(discount_id, value)
const discount = await discountService.retrieve(created.id, {
relations: ["rule", "rule.valid_for", "regions"],
})
const discount = await discountService.retrieve(created.id, {
relations: ["rule", "rule.valid_for", "regions"],
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -25,17 +25,12 @@
*/
export default async (req, res) => {
const { discount_id } = req.params
const discountService = req.scope.resolve("discountService")
await discountService.delete(discount_id)
try {
const discountService = req.scope.resolve("discountService")
await discountService.delete(discount_id)
res.json({
id: discount_id,
object: "discount",
deleted: true,
})
} catch (err) {
throw err
}
res.json({
id: discount_id,
object: "discount",
deleted: true,
})
}

View File

@@ -20,17 +20,12 @@
*/
export default async (req, res) => {
const { discount_id, code } = req.params
const discountService = req.scope.resolve("discountService")
await discountService.deleteDynamicCode(discount_id, code)
try {
const discountService = req.scope.resolve("discountService")
await discountService.deleteDynamicCode(discount_id, code)
const discount = await discountService.retrieve(discount_id, {
relations: ["rule", "rule.valid_for", "regions"],
})
const discount = await discountService.retrieve(discount_id, {
relations: ["rule", "rule.valid_for", "regions"],
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -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 discountService = req.scope.resolve("discountService")
const discount = await discountService.retrieveByCode(code, defaultRelations)
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -21,15 +21,11 @@ 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,
relations: defaultRelations,
})
const discountService = req.scope.resolve("discountService")
const data = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount: data })
} catch (err) {
throw err
}
res.status(200).json({ discount: data })
}

View File

@@ -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))

View File

@@ -18,41 +18,35 @@ import { defaultFields, defaultRelations } from "./"
* $ref: "#/components/schemas/discount"
*/
export default async (req, res) => {
try {
const discountService = req.scope.resolve("discountService")
const discountService = req.scope.resolve("discountService")
const limit = parseInt(req.query.limit) || 20
const offset = parseInt(req.query.offset) || 0
const selector = {}
const limit = parseInt(req.query.limit) || 20
const offset = parseInt(req.query.offset) || 0
let selector = {}
if ("q" in req.query) {
selector.q = req.query.q
}
if ("is_dynamic" in req.query) {
selector.is_dynamic = req.query.is_dynamic === "true"
}
if ("is_disabled" in req.query) {
selector.is_disabled = req.query.is_disabled === "true"
}
const listConfig = {
select: defaultFields,
relations: defaultRelations,
skip: offset,
take: limit,
order: { created_at: "DESC" },
}
const [discounts, count] = await discountService.listAndCount(
selector,
listConfig
)
res.status(200).json({ discounts, count, offset, limit })
} catch (err) {
throw err
if ("q" in req.query) {
selector.q = req.query.q
}
if ("is_dynamic" in req.query) {
selector.is_dynamic = req.query.is_dynamic === "true"
}
if ("is_disabled" in req.query) {
selector.is_disabled = req.query.is_disabled === "true"
}
const listConfig = {
select: defaultFields,
relations: defaultRelations,
skip: offset,
take: limit,
order: { created_at: "DESC" },
}
const [discounts, count] = await discountService.listAndCount(
selector,
listConfig
)
res.status(200).json({ discounts, count, offset, limit })
}

View File

@@ -22,18 +22,13 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, region_id } = req.params
const discountService = req.scope.resolve("discountService")
await discountService.removeRegion(discount_id, region_id)
try {
const discountService = req.scope.resolve("discountService")
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
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
}
res.status(200).json({ discount })
}

View File

@@ -22,19 +22,13 @@ import { defaultFields, defaultRelations } from "./"
*/
export default async (req, res) => {
const { discount_id, variant_id } = req.params
const discountService = req.scope.resolve("discountService")
await discountService.removeValidProduct(discount_id, variant_id)
try {
const discountService = req.scope.resolve("discountService")
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
await discountService.removeValidProduct(discount_id, variant_id)
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}

View File

@@ -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,18 +84,13 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const discountService = req.scope.resolve("discountService")
const discountService = req.scope.resolve("discountService")
await discountService.update(discount_id, value)
await discountService.update(discount_id, value)
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
const discount = await discountService.retrieve(discount_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ discount })
} catch (err) {
throw err
}
res.status(200).json({ discount })
}