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