From 24dbfc3dc73804750c91b4da8a8fbf0143a5367e Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Sat, 4 Jul 2020 18:11:18 +0200 Subject: [PATCH] Fixes creating discount with rule --- .../routes/admin/discounts/create-discount.js | 5 ++++- packages/medusa/src/services/discount.js | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/medusa/src/api/routes/admin/discounts/create-discount.js b/packages/medusa/src/api/routes/admin/discounts/create-discount.js index 39f9fa2f06..7823b9d4bc 100644 --- a/packages/medusa/src/api/routes/admin/discounts/create-discount.js +++ b/packages/medusa/src/api/routes/admin/discounts/create-discount.js @@ -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(), diff --git a/packages/medusa/src/services/discount.js b/packages/medusa/src/services/discount.js index 3b00c31498..513c50461d 100644 --- a/packages/medusa/src/services/discount.js +++ b/packages/medusa/src/services/discount.js @@ -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)