Fixes creating discount with rule

This commit is contained in:
olivermrbl
2020-07-04 18:11:18 +02:00
parent d6fc477636
commit 24dbfc3dc7
2 changed files with 16 additions and 5 deletions

View File

@@ -7,10 +7,13 @@ export default async (req, res) => {
.keys({
description: Validator.string().optional(),
type: Validator.string().required(),
value: Validator.number().required(),
value: Validator.number()
.positive()
.required(),
allocation: Validator.string().required(),
valid_for: Validator.array().items(Validator.string()),
usage_limit: Validator.number().optional(),
total_limit: Validator.number().optional(),
})
.required(),
usage_count: Validator.number().optional(),

View File

@@ -47,9 +47,9 @@ class DiscountService extends BaseService {
}
/**
* Validates discount rules
* @param {DiscountRule} discountRule - the discount rule to validate
* @return {DiscountRule} the validated discount rule
* Creates a discount rule with provided data given that the data is validated.
* @param {DiscountRule} discountRule - the discount rule to create
* @return {Promise} the result of the create operation
*/
validateDiscountRule_(discountRule) {
const schema = Validator.object().keys({
@@ -91,6 +91,14 @@ class DiscountService extends BaseService {
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.
* Normalizes discount code to uppercase.
@@ -98,7 +106,7 @@ class DiscountService extends BaseService {
* @return {Promise} the result of the create operation
*/
async create(discount) {
await this.validateDiscountRule_(discount.discount_rule)
discount.discount_rule = this.validateDiscountRule_(discount.discount_rule)
discount.code = this.normalizeDiscountCode_(discount.code)