Commit 78f44669 by Clinton Blackburn

Coupon model cleanup

- The URL for the model will always end with a trailing slash
- Minor formatting fixes

ECOM-7096
parent 2a30f64d
...@@ -18,8 +18,7 @@ define([ ...@@ -18,8 +18,7 @@ define([
_, _,
_s, _s,
Utils, Utils,
moment moment) {
) {
'use strict'; 'use strict';
_.extend(Backbone.Validation.messages, { _.extend(Backbone.Validation.messages, {
...@@ -104,9 +103,7 @@ define([ ...@@ -104,9 +103,7 @@ define([
} }
} }
}, },
enterprise_customer: { enterprise_customer: {required: false},
required: false,
},
end_date: function (val) { end_date: function (val) {
var startDate, var startDate,
endDate; endDate;
...@@ -129,19 +126,19 @@ define([ ...@@ -129,19 +126,19 @@ define([
} }
}, },
invoice_number: { invoice_number: {
required: function() { required: function () {
return this.isPrepaidInvoiceType(); return this.isPrepaidInvoiceType();
} }
}, },
invoice_payment_date: { invoice_payment_date: {
required: function() { required: function () {
return this.isPrepaidInvoiceType(); return this.isPrepaidInvoiceType();
} }
}, },
invoice_type: {required: true}, invoice_type: {required: true},
max_uses: function(val) { max_uses: function (val) {
var numberPattern = new RegExp('[0-9]+'); var numberPattern = new RegExp('[0-9]+');
if (val===''){ if (val === '') {
this.unset('max_uses'); this.unset('max_uses');
} }
if (val && !numberPattern.test(val)) { if (val && !numberPattern.test(val)) {
...@@ -152,7 +149,7 @@ define([ ...@@ -152,7 +149,7 @@ define([
}, },
price: { price: {
pattern: 'number', pattern: 'number',
required: function() { required: function () {
return this.isPrepaidInvoiceType(); return this.isPrepaidInvoiceType();
} }
}, },
...@@ -181,13 +178,22 @@ define([ ...@@ -181,13 +178,22 @@ define([
title: {required: true} title: {required: true}
}, },
url: function () {
var url = this._super();
// Ensure the URL always ends with a trailing slash
url += _s.endsWith(url, '/') ? '' : '/';
return url;
},
initialize: function () { initialize: function () {
this.on('change:seats', this.updateSeatData); this.on('change:seats', this.updateSeatData);
this.on('change:quantity', this.updateTotalValue(this.getSeatPrice)); this.on('change:quantity', this.updateTotalValue(this.getSeatPrice));
this.on('change:payment_information', this.updatePaymentInformation); this.on('change:payment_information', this.updatePaymentInformation);
}, },
isPrepaidInvoiceType: function() { isPrepaidInvoiceType: function () {
return this.get('invoice_type') === 'Prepaid'; return this.get('invoice_type') === 'Prepaid';
}, },
...@@ -200,12 +206,12 @@ define([ ...@@ -200,12 +206,12 @@ define([
this.set('total_value', this.get('quantity') * seat_price); this.set('total_value', this.get('quantity') * seat_price);
}, },
getCertificateType: function(seat_data) { getCertificateType: function (seat_data) {
var seat_type = _.findWhere(seat_data, {'name': 'certificate_type'}); var seat_type = _.findWhere(seat_data, {'name': 'certificate_type'});
return seat_type ? seat_type.value : ''; return seat_type ? seat_type.value : '';
}, },
getCourseID: function(seat_data) { getCourseID: function (seat_data) {
var course_id = _.findWhere(seat_data, {'name': 'course_key'}); var course_id = _.findWhere(seat_data, {'name': 'course_key'});
return course_id ? course_id.value : ''; return course_id ? course_id.value : '';
}, },
...@@ -243,7 +249,7 @@ define([ ...@@ -243,7 +249,7 @@ define([
} }
}, },
updatePaymentInformation: function() { updatePaymentInformation: function () {
var payment_information = this.get('payment_information'), var payment_information = this.get('payment_information'),
invoice = payment_information.Invoice, invoice = payment_information.Invoice,
tax_deducted = invoice.tax_deducted_source ? 'Yes' : 'No'; tax_deducted = invoice.tax_deducted_source ? 'Yes' : 'No';
...@@ -268,7 +274,7 @@ define([ ...@@ -268,7 +274,7 @@ define([
contentType: 'application/json' contentType: 'application/json'
}); });
if (!options.patch){ if (!options.patch) {
this.set('start_datetime', moment.utc(this.get('start_date'))); this.set('start_datetime', moment.utc(this.get('start_date')));
this.set('end_datetime', moment.utc(this.get('end_date'))); this.set('end_datetime', moment.utc(this.get('end_date')));
......
...@@ -18,6 +18,19 @@ define([ ...@@ -18,6 +18,19 @@ define([
enrollmentCodeData = Mock_Coupons.enrollmentCodeCouponModelData; enrollmentCodeData = Mock_Coupons.enrollmentCodeCouponModelData;
describe('Coupon model', function () { describe('Coupon model', function () {
describe('url', function () {
it('should be /api/v2/coupons/ for new instances', function () {
var instance = new Coupon();
expect(instance.url()).toEqual('/api/v2/coupons/');
});
it('should have a trailing slash for existing instances', function () {
var id = 1,
instance = new Coupon({id: id});
expect(instance.url()).toEqual('/api/v2/coupons/' + id + '/');
});
});
describe('validation', function () { describe('validation', function () {
var model; var model;
...@@ -113,7 +126,7 @@ define([ ...@@ -113,7 +126,7 @@ define([
expect(model.isValid()).toBe(true); expect(model.isValid()).toBe(true);
}); });
it('should validate invoice data.', function() { it('should validate invoice data.', function () {
model.set('price', 'text'); model.set('price', 'text');
model.validate(); model.validate();
expect(model.isValid()).toBeFalsy(); expect(model.isValid()).toBeFalsy();
...@@ -129,7 +142,7 @@ define([ ...@@ -129,7 +142,7 @@ define([
expect(model.isValid()).toBeTruthy(); expect(model.isValid()).toBeTruthy();
}); });
it('should validate coupon code.', function() { it('should validate coupon code.', function () {
model.set('code', '!#$%&/()='); model.set('code', '!#$%&/()=');
model.validate(); model.validate();
expect(model.isValid()).toBeFalsy(); expect(model.isValid()).toBeFalsy();
...@@ -139,7 +152,7 @@ define([ ...@@ -139,7 +152,7 @@ define([
expect(model.isValid()).toBeTruthy(); expect(model.isValid()).toBeTruthy();
}); });
it('should validate email domain.', function() { it('should validate email domain.', function () {
var invalid_domains = [ var invalid_domains = [
'-invalid.com', 'invalid', 'invalid-.com', 'invalid.c', 'valid.com,', '-invalid.com', 'invalid', 'invalid-.com', 'invalid.c', 'valid.com,',
'invalid.photography1', 'valid.com,invalid', 'valid.com,invalid-.com', 'invalid.photography1', 'valid.com,invalid', 'valid.com,invalid-.com',
...@@ -152,19 +165,19 @@ define([ ...@@ -152,19 +165,19 @@ define([
'valid.com,valid.co', 'çççç.рф', 'çç-ççç32.中国', 'ççç.ççç.இலங்கை' 'valid.com,valid.co', 'çççç.рф', 'çç-ççç32.中国', 'ççç.ççç.இலங்கை'
]; ];
_.each(invalid_domains, function(domain) { _.each(invalid_domains, function (domain) {
model.set('email_domains', domain); model.set('email_domains', domain);
model.validate(); model.validate();
expect(model.isValid()).toBeFalsy(); expect(model.isValid()).toBeFalsy();
}); });
_.each(valid_domains, function(domain) { _.each(valid_domains, function (domain) {
model.set('email_domains', domain); model.set('email_domains', domain);
model.validate(); model.validate();
expect(model.isValid()).toBeTruthy(); expect(model.isValid()).toBeTruthy();
}); });
}); });
it('should validate max_uses value', function() { it('should validate max_uses value', function () {
model.set('max_uses', 'abc'); model.set('max_uses', 'abc');
model.validate(); model.validate();
expect(model.isValid()).toBeFalsy(); expect(model.isValid()).toBeFalsy();
...@@ -286,4 +299,4 @@ define([ ...@@ -286,4 +299,4 @@ define([
}); });
}); });
}); });
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