Commit 420afd3c by Vedran Karačić Committed by GitHub

Merge pull request #976 from edx/vkaracic/SOL-2109

[SOL-2109] Change the default values of max_uses field in coupon form.
parents 9b1a74ab 9979f117
......@@ -46,7 +46,25 @@ define([
},
validation: {
benefit_value: {
pattern: 'number',
required: function () {
return this.get('coupon_type') === 'Discount code';
}
},
catalog_query: {
required: function () {
return this.get('catalog_type') === 'Multiple courses';
}
},
category: {required: true},
client: {required: true},
code: {
pattern: /^[a-zA-Z0-9]+$/,
required: false,
rangeLength: [1, 16],
msg: gettext('This field must be empty or contain 1-16 alphanumeric characters.')
},
course_id: {
pattern: 'courseId',
msg: gettext('A valid course ID is required'),
......@@ -54,65 +72,69 @@ define([
return this.get('catalog_type') === 'Single course';
}
},
title: {required: true},
client: {required: true},
// seat_type is for validation only, stock_record_ids holds the values
seat_type: {
required: function () {
return this.get('catalog_type') === 'Single course';
course_seat_types: function (val) {
if (this.get('catalog_type') === 'Multiple courses' && val.length === 0) {
return Backbone.Validation.messages.seat_types;
}
},
quantity: {pattern: 'number'},
benefit_value: {
email_domains: {
pattern:
/^((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}(,((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,})*$/,
required: false
},
end_date: function (val) {
var startDate,
endDate;
if (_.isEmpty(val)) {
return Backbone.Validation.messages.required;
}
endDate = moment(new Date(val));
if (!endDate.isValid()) {
return Backbone.Validation.messages.date;
}
startDate = moment(new Date(this.get('start_date')));
if (startDate && endDate.isBefore(startDate)) {
return gettext('Must occur after start date');
}
},
invoice_discount_value: {
pattern: 'number',
required: function () {
return this.get('coupon_type') === 'Discount code';
return this.get('invoice_type') === 'Postpaid';
}
},
invoice_type: {required: true},
invoice_number: {
required: function() {
return this.isPrepaidInvoiceType();
}
},
price: {
pattern: 'number',
invoice_payment_date: {
required: function() {
return this.isPrepaidInvoiceType();
}
},
invoice_payment_date: {
required: function() {
return this.isPrepaidInvoiceType();
invoice_type: {required: true},
max_uses: function(val) {
var numberPattern = new RegExp('[0-9]+');
if (val && !numberPattern.test(val)) {
return Backbone.Validation.messages.number;
} else if (val && val < 2 && this.get('voucher_type') === 'Multi-use') {
return gettext('Max uses for multi-use coupons must be higher than 2.');
}
},
invoice_discount_value: {
price: {
pattern: 'number',
required: function () {
return this.get('invoice_type') === 'Postpaid';
required: function() {
return this.isPrepaidInvoiceType();
}
},
code: {
pattern: /^[a-zA-Z0-9]+$/,
required: false,
rangeLength: [1, 16],
msg: gettext('This field must be empty or contain 1-16 alphanumeric characters.')
},
catalog_query: {
quantity: {pattern: 'number'},
// seat_type is for validation only, stock_record_ids holds the values
seat_type: {
required: function () {
return this.get('catalog_type') === 'Multiple courses';
}
},
course_seat_types: function (val) {
if (this.get('catalog_type') === 'Multiple courses' && val.length === 0) {
return Backbone.Validation.messages.seat_types;
return this.get('catalog_type') === 'Single course';
}
},
email_domains: {
pattern:
/^((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}(,((xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,})*$/,
required: false
},
start_date: function (val) {
var startDate,
endDate;
......@@ -128,21 +150,7 @@ define([
return gettext('Must occur before end date');
}
},
end_date: function (val) {
var startDate,
endDate;
if (_.isEmpty(val)) {
return Backbone.Validation.messages.required;
}
endDate = moment(new Date(val));
if (!endDate.isValid()) {
return Backbone.Validation.messages.date;
}
startDate = moment(new Date(this.get('start_date')));
if (startDate && endDate.isBefore(startDate)) {
return gettext('Must occur after start date');
}
}
title: {required: true},
},
initialize: function () {
......
......@@ -136,6 +136,30 @@ define([
expect(model.isValid()).toBeTruthy();
});
});
it('should validate max_uses value', function() {
model.set('max_uses', 'abc');
model.validate();
expect(model.isValid()).toBeFalsy();
model.set('max_uses', 1);
model.set('voucher_type', 'Multi-use');
model.validate();
expect(model.isValid()).toBeFalsy();
model.set('max_uses', 2);
model.validate();
expect(model.isValid()).toBeTruthy();
model.unset('max_uses');
model.validate();
expect(model.isValid()).toBeTruthy();
model.set('max_uses', 1);
model.set('voucher_type', 'Once per customer');
model.validate();
expect(model.isValid()).toBeTruthy();
});
});
describe('test model methods', function () {
......
......@@ -421,7 +421,8 @@ define([
},
toggleVoucherTypeField: function () {
var voucherType = this.model.get('voucher_type');
var maxUsesFieldSelector = '[name=max_uses]',
voucherType = this.model.get('voucher_type');
if (!this.editing) {
this.emptyCodeField();
}
......@@ -429,13 +430,23 @@ define([
* Show the code field only for discount coupons and when the quantity is 1 to avoid
* integrity issues.
*/
if (voucherType !== 'Single use') {
if (voucherType === 'Single use') {
this.hideField(maxUsesFieldSelector, 1);
} else {
if (this.model.get('coupon_type') === 'Discount code' && this.$('[name=quantity]').val() === 1) {
this.formGroup('[name=code]').removeClass(this.hiddenClass);
}
this.formGroup('[name=max_uses]').removeClass(this.hiddenClass);
} else {
this.hideField('[name=max_uses]', 1);
this.formGroup(maxUsesFieldSelector).removeClass(this.hiddenClass);
/* For coupons that can be used multiple times by multiple users, the max_uses
* field needs to be empty by default and the minimum can not be less than 2.
*/
if (voucherType === 'Multi-use') {
this.model.set('max_uses', null);
this.$(maxUsesFieldSelector).attr('min', 2);
} else {
this.model.set('max_uses', 1);
this.$(maxUsesFieldSelector).attr('min', 1);
}
}
},
......
......@@ -55,7 +55,7 @@
<div class="form-group">
<label for="max-uses"><%= gettext('Maximum Number of Uses') %></label>
<input id="max-uses" type="number" step="1" class="form-control non-editable" name="max_uses" value="1" min="1">
<input id="max-uses" type="number" step="1" class="form-control non-editable" name="max_uses" value="1" min="1" placeholder="Defaults to 10000">
<p class="help-block"></p>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment