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([
_,
_s,
Utils,
moment
) {
moment) {
'use strict';
_.extend(Backbone.Validation.messages, {
......@@ -104,9 +103,7 @@ define([
}
}
},
enterprise_customer: {
required: false,
},
enterprise_customer: {required: false},
end_date: function (val) {
var startDate,
endDate;
......@@ -129,19 +126,19 @@ define([
}
},
invoice_number: {
required: function() {
required: function () {
return this.isPrepaidInvoiceType();
}
},
invoice_payment_date: {
required: function() {
required: function () {
return this.isPrepaidInvoiceType();
}
},
invoice_type: {required: true},
max_uses: function(val) {
max_uses: function (val) {
var numberPattern = new RegExp('[0-9]+');
if (val===''){
if (val === '') {
this.unset('max_uses');
}
if (val && !numberPattern.test(val)) {
......@@ -152,7 +149,7 @@ define([
},
price: {
pattern: 'number',
required: function() {
required: function () {
return this.isPrepaidInvoiceType();
}
},
......@@ -181,13 +178,22 @@ define([
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 () {
this.on('change:seats', this.updateSeatData);
this.on('change:quantity', this.updateTotalValue(this.getSeatPrice));
this.on('change:payment_information', this.updatePaymentInformation);
},
isPrepaidInvoiceType: function() {
isPrepaidInvoiceType: function () {
return this.get('invoice_type') === 'Prepaid';
},
......@@ -200,12 +206,12 @@ define([
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'});
return seat_type ? seat_type.value : '';
},
getCourseID: function(seat_data) {
getCourseID: function (seat_data) {
var course_id = _.findWhere(seat_data, {'name': 'course_key'});
return course_id ? course_id.value : '';
},
......@@ -243,7 +249,7 @@ define([
}
},
updatePaymentInformation: function() {
updatePaymentInformation: function () {
var payment_information = this.get('payment_information'),
invoice = payment_information.Invoice,
tax_deducted = invoice.tax_deducted_source ? 'Yes' : 'No';
......@@ -268,7 +274,7 @@ define([
contentType: 'application/json'
});
if (!options.patch){
if (!options.patch) {
this.set('start_datetime', moment.utc(this.get('start_date')));
this.set('end_datetime', moment.utc(this.get('end_date')));
......
......@@ -18,6 +18,19 @@ define([
enrollmentCodeData = Mock_Coupons.enrollmentCodeCouponModelData;
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 () {
var model;
......@@ -113,7 +126,7 @@ define([
expect(model.isValid()).toBe(true);
});
it('should validate invoice data.', function() {
it('should validate invoice data.', function () {
model.set('price', 'text');
model.validate();
expect(model.isValid()).toBeFalsy();
......@@ -129,7 +142,7 @@ define([
expect(model.isValid()).toBeTruthy();
});
it('should validate coupon code.', function() {
it('should validate coupon code.', function () {
model.set('code', '!#$%&/()=');
model.validate();
expect(model.isValid()).toBeFalsy();
......@@ -139,7 +152,7 @@ define([
expect(model.isValid()).toBeTruthy();
});
it('should validate email domain.', function() {
it('should validate email domain.', function () {
var invalid_domains = [
'-invalid.com', 'invalid', 'invalid-.com', 'invalid.c', 'valid.com,',
'invalid.photography1', 'valid.com,invalid', 'valid.com,invalid-.com',
......@@ -152,19 +165,19 @@ define([
'valid.com,valid.co', 'çççç.рф', 'çç-ççç32.中国', 'ççç.ççç.இலங்கை'
];
_.each(invalid_domains, function(domain) {
_.each(invalid_domains, function (domain) {
model.set('email_domains', domain);
model.validate();
expect(model.isValid()).toBeFalsy();
});
_.each(valid_domains, function(domain) {
_.each(valid_domains, function (domain) {
model.set('email_domains', domain);
model.validate();
expect(model.isValid()).toBeTruthy();
});
});
it('should validate max_uses value', function() {
it('should validate max_uses value', function () {
model.set('max_uses', 'abc');
model.validate();
expect(model.isValid()).toBeFalsy();
......@@ -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