Fixes creating discount with rule
This commit is contained in:
@@ -7,10 +7,13 @@ 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().required(),
|
value: Validator.number()
|
||||||
|
.positive()
|
||||||
|
.required(),
|
||||||
allocation: Validator.string().required(),
|
allocation: Validator.string().required(),
|
||||||
valid_for: Validator.array().items(Validator.string()),
|
valid_for: Validator.array().items(Validator.string()),
|
||||||
usage_limit: Validator.number().optional(),
|
usage_limit: Validator.number().optional(),
|
||||||
|
total_limit: Validator.number().optional(),
|
||||||
})
|
})
|
||||||
.required(),
|
.required(),
|
||||||
usage_count: Validator.number().optional(),
|
usage_count: Validator.number().optional(),
|
||||||
|
|||||||
@@ -47,9 +47,9 @@ class DiscountService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates discount rules
|
* Creates a discount rule with provided data given that the data is validated.
|
||||||
* @param {DiscountRule} discountRule - the discount rule to validate
|
* @param {DiscountRule} discountRule - the discount rule to create
|
||||||
* @return {DiscountRule} the validated discount rule
|
* @return {Promise} the result of the create operation
|
||||||
*/
|
*/
|
||||||
validateDiscountRule_(discountRule) {
|
validateDiscountRule_(discountRule) {
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
@@ -91,6 +91,14 @@ class DiscountService extends BaseService {
|
|||||||
return discountCode.toUpperCase()
|
return discountCode.toUpperCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Object} selector - the query object for find
|
||||||
|
* @return {Promise} the result of the find operation
|
||||||
|
*/
|
||||||
|
list(selector) {
|
||||||
|
return this.discountModel_.find(selector)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a discount with provided data given that the data is validated.
|
* Creates a discount with provided data given that the data is validated.
|
||||||
* Normalizes discount code to uppercase.
|
* Normalizes discount code to uppercase.
|
||||||
@@ -98,7 +106,7 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the create operation
|
* @return {Promise} the result of the create operation
|
||||||
*/
|
*/
|
||||||
async create(discount) {
|
async create(discount) {
|
||||||
await this.validateDiscountRule_(discount.discount_rule)
|
discount.discount_rule = this.validateDiscountRule_(discount.discount_rule)
|
||||||
|
|
||||||
discount.code = this.normalizeDiscountCode_(discount.code)
|
discount.code = this.normalizeDiscountCode_(discount.code)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user