Adds dynamic coupon codes and segment plugin

This commit is contained in:
Sebastian Rindom
2020-07-17 14:06:55 +02:00
parent ec6a538877
commit 3710f7f80a
41 changed files with 11784 additions and 109 deletions
@@ -37,6 +37,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
is_dynamic: false
})
})
})
@@ -43,6 +43,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
is_dynamic: false,
}
)
})
@@ -3,6 +3,7 @@ import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
code: Validator.string().required(),
is_dynamic: Validator.boolean().default(false),
discount_rule: Validator.object()
.keys({
description: Validator.string().optional(),
@@ -0,0 +1,25 @@
import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
const { discount_id } = req.params
const schema = Validator.object().keys({
code: Validator.string().required(),
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")
await discountService.createDynamicCode(discount_id, value)
const data = await discountService.retrieve(dicsount_id)
res.status(200).json({ discount: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,21 @@
import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
const { discount_id, code } = req.params
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")
await discountService.deleteDynamicCode(discount_id, code)
const data = await discountService.retrieve(dicsount_id)
res.status(200).json({ discount: data })
} catch (err) {
throw err
}
}
@@ -22,6 +22,16 @@ export default app => {
middlewares.wrap(require("./delete-discount").default)
)
// Dynamic codes
route.post(
"/:discount_id/dynamic-codes",
middlewares.wrap(require("./create-dynamic-code").default)
)
route.delete(
"/:discount_id/dynamic-codes/:code",
middlewares.wrap(require("./delete-dynamic-code").default)
)
// Discount valid variants management
route.post(
"/:discount_id/variants/:variant_id",
@@ -4,6 +4,7 @@ export default async (req, res) => {
const { discount_id } = req.params
const schema = Validator.object().keys({
code: Validator.string().required(),
is_dynamic: Validator.boolean().default(false),
discount_rule: Validator.object()
.keys({
description: Validator.string().optional(),
@@ -13,6 +13,7 @@ export default async (req, res) => {
res.status(200).json({ cart })
} catch (err) {
console.log(err.response.data)
throw err
}
}
@@ -34,7 +34,6 @@ export default async (req, res) => {
res.status(200).json({ order })
} catch (err) {
console.log(err)
// If something fails it might be because the order has already been created
// if it has we find it from the cart id
const orderService = req.scope.resolve("orderService")