Adds dynamic coupon codes and segment plugin
This commit is contained in:
1 parent
ec6a538877
commit
3710f7f80a
41 files changed
+11784
-109
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { Router } from "express"
|
||||
import bodyParser from "body-parser"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default (container) => {
|
||||
const route = Router()
|
||||
|
||||
route.post("/discount-code", bodyParser.json(), async (req, res) => {
|
||||
const schema = Validator.object().keys({
|
||||
discount_code: Validator.string().required(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
const discountGenerator = req.scope.resolve("discountGeneratorService")
|
||||
const code = await discountGenerator.generateDiscount(value.discount_code)
|
||||
|
||||
res.json({
|
||||
code
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
return route
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import randomize from "randomatic"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
class DiscountGeneratorService extends BaseService {
|
||||
constructor({ discountService }) {
|
||||
super()
|
||||
|
||||
this.discountService_ = discountService
|
||||
}
|
||||
|
||||
async generateDiscount(dynamicDiscountCode) {
|
||||
const discount = await this.discountService_.retrieveByCode(dynamicDiscountCode)
|
||||
const data = {
|
||||
code: randomize("A0", 10)
|
||||
}
|
||||
return this.discountService_.createDynamicCode(discount._id, data)
|
||||
}
|
||||
}
|
||||
|
||||
export default DiscountGeneratorService
|
||||
Reference in new issue
Block a user