Repository navigation
Expand file tree
/
Copy pathparcelRequest.js
More file actions
84 lines (75 loc) · 2.39 KB
/
Copy pathparcelRequest.js
File metadata and controls
84 lines (75 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var parcelRequest = function(expedition) {
this.expedition = expedition;
};
parcelRequest.prototype.validate = function() {
if (!this.sequenceNumber) {
return new Error('parcelRequest require a sequenceNumber');
}
if (!this.weight) {
return new Error('parcelRequest require a weight');
}
return true;
};
parcelRequest.prototype.get = function() {
var validate = this.validate();
if (validate !== true) {
throw validate;
}
var parcel = {
sequenceNumber: this.sequenceNumber,
weight: this.weight
};
if (this.customerReference) {
parcel.customerReference = this.customerReference;
}
if (this.insuranceAmount) {
parcel.insuranceAmount = this.insuranceAmount;
}
if (this.priorityGuarantee) {
parcel.priorityGuarantee = this.priorityGuarantee;
}
if (this.comment) {
parcel.comment = this.comment;
}
return parcel;
};
parcelRequest.prototype._setSequenceNumber = function(sequenceNumber) {
this.sequenceNumber = sequenceNumber;
};
parcelRequest.prototype.setCustomerReference = function(customerReference) {
if (customerReference.length > 10) {
throw new Error('parcelRequest customerReference max length is 10');
}
this.customerReference = customerReference;
};
parcelRequest.prototype.setWeight = function(weight) {
var regexDecimal = /^(\d+\.?\d*|\.\d+)$/;
if (!weight.match(regexDecimal)) {
throw new Error('parcelRequest weight must be a decimal with dot notation');
}
this.weight = weight;
};
parcelRequest.prototype.setInsuranceAmount = function(insuranceAmount) {
var regexDecimal = /^(\d+\.?\d*|\.\d+)$/;
if (!weight.match(regexDecimal)) {
throw new Error('parcelRequest insuranceAmount must be a decimal with dot notation');
}
var val = parseFloat(insuranceAmount);
if (val < 1 || val > 25000) {
throw new Error('parcelRequest insuranceAmount must be a between 1 and 25000');
}
this.insuranceAmount = insuranceAmount;
};
parcelRequest.prototype.setPriorityGuarantee = function(priorityGuarantee) {
if (['', 'PTY', 'GUE'].indexOf(priorityGuarantee) === -1) {
throw new Error("parcelRequest priorityGuarantee must be in ['', 'PTY', 'GUE']");
}
this.priorityGuarantee = priorityGuarantee;
};
parcelRequest.prototype.setComment = function(comment) {
if (comment.length > 30) {
throw new Error('parcelRequest comment max length is 30');
}
this.comment = comment;
};
module.exports = parcelRequest;