-
Notifications
You must be signed in to change notification settings - Fork 2
/
receiver.js
217 lines (198 loc) · 5.89 KB
/
receiver.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var receiver = function(expedition) {
this.expedition = expedition;
};
receiver.prototype.validate = function() {
var invalid_fields = [];
if (!this.typeId && this.type !== 'INDIVIDUAL' && !this.name) {
invalid_fields.push('name');
}
if (!this.typeId && !this.address1) {
invalid_fields.push('address1');
}
if (!this.typeId && !this.zipCode) {
invalid_fields.push('zipCode');
}
if (this.typeId !== 'DEPOT' && !this.city) {
invalid_fields.push('city');
}
if (this.type === 'DEPOT' && !this.contactLastName) {
invalid_fields.push("contactLastName with type 'DEPOT'");
}
if (this.type === 'DEPOT' && !this.contactFirstName) {
invalid_fields.push("contactFirstName with type 'DEPOT'");
}
if (this.expedition.hasPriorityOrGuarantee()) {
if (!this.contactLastName) {
invalid_fields.push('contactLastName with Priority or Guarantee set on a parcel');
}
if (!this.contactFirstName) {
invalid_fields.push('contactFirstName with Priority or Guarantee set on a parcel');
}
}
if (invalid_fields.length > 0) {
return new Error('fields ' + invalid_fields.join(', ') + ' are required for this Receiver');
}
return true;
};
receiver.prototype.get = function() {
var validate = this.validate();
if (validate !== true) {
throw validate;
}
var mreceiver = {};
if (this.name) {
mreceiver.name = this.name;
}
if (this.address1) {
mreceiver.address1 = this.address1;
}
if (this.zipCode) {
mreceiver.zipCode = this.zipCode;
}
if (this.city) {
mreceiver.city = this.city;
}
if (this.address2) {
mreceiver.address2 = this.address2;
}
if (this.type) {
mreceiver.type = this.type;
}
if (this.typeId) {
mreceiver.typeId = this.typeId;
}
if (this.instructions) {
mreceiver.instructions = this.instructions
}
if (this.contactLastName) {
mreceiver.contactLastName = this.contactLastName;
}
if (this.contactFirstName) {
mreceiver.contactFirstName = this.contactFirstName;
}
if (this.emailAddress) {
mreceiver.emailAddress = this.emailAddress;
}
if (this.phoneNumber) {
mreceiver.phoneNumber = this.phoneNumber;
}
if (this.accessCode) {
mreceiver.accessCode = this.accessCode;
}
if (this.floorNumber) {
mreceiver.floorNumber = this.floorNumber;
}
if (this.buldingId) {
mreceiver.buldingId = this.buldingId;
}
if (this.sendNotification) {
mreceiver.sendNotification = this.sendNotification;
}
return mreceiver;
};
receiver.prototype.setType = function(type) {
if (['ENTERPRISE', 'DEPOT', 'DROPOFFPOINT', 'INDIVIDUAL'].indexOf(type) === -1) {
throw new Error("Invalid Reveiver type: type must be in ['ENTERPRISE', 'DEPOT', 'DROPOFFPOINT', 'INDIVIDUAL']");
}
this.type = type;
};
receiver.prototype.setTypeId = function(code) {
if (['DEPOT', 'DROPOFFPOINT'].indexOf(this.type) === -1) {
throw new Error("TypeId must be specified only when type in ['DEPOT', 'DROPOFFPOINT']");
}
this.typeId = code;
};
receiver.prototype.setName = function(name) {
if (name.length > 32) {
throw new Error('Receiver name max length is 32');
}
this.name = name;
};
receiver.prototype.setAddress1 = function(address1) {
if (address1.length > 32) {
throw new Error('Receiver address1 max length is 32');
}
this.address1 = address1;
};
receiver.prototype.setAddress2 = function(address2) {
if (address2.length > 32) {
throw new Error('Receiver address2 max length is 32');
}
this.address2 = address2;
};
receiver.prototype.setZipCode = function(zipCode) {
if (zipCode.length !== 5) {
throw new Error('Receiver zipCode must be 5 digits');
}
this.zipCode = zipCode;
};
receiver.prototype.setCity = function(city) {
if (city.length > 27) {
throw new Error('Receiver city max length is 27');
}
this.city = city;
};
receiver.prototype.setInstructions = function(instructions) {
if (instructions.length > 60) {
throw new Error('Receiver instructions max length is 60');
}
this.instructions = instructions;
};
receiver.prototype.setContactLastName = function(contactLastName) {
if (contactLastName.length > 19) {
throw new Error('Receiver contactLastName max length is 19');
}
this.contactLastName = contactLastName;
};
receiver.prototype.setContactFirstName = function(contactFirstName) {
if (contactFirstName.length > 12) {
throw new Error('Receiver contactFirstName max length is 12');
}
this.contactFirstName = contactFirstName;
};
receiver.prototype.setEmailAddress = function(emailAddress) {
if (emailAddress.length > 80) {
throw new Error('Receiver emailAddress max length is 80');
}
var regexEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (!emailAddress.match(regexEmail)) {
return new Error('Receiver emailAddress must comply with RFC 2822 norm');
}
this.emailAddress = emailAddress;
};
receiver.prototype.setPhoneNumber = function(phoneNumber) {
if (phoneNumber.length > 15) {
throw new Error('Receiver phoneNumber max length is 15');
}
var regexNumber = /^\d+$/;
if (!phoneNumber.match(regexNumber)) {
throw new Error('Receiver phoneNumber musn\'t have any separator');
}
this.phoneNumber = phoneNumber;
};
receiver.prototype.setAccessCode = function(accessCode) {
if (accessCode.length > 7) {
throw new Error('Receiver accessCode max length is 7');
}
this.accessCode = accessCode;
};
receiver.prototype.setFloorNumber = function(floorNumber) {
if (floorNumber.length > 2) {
throw new Error('Receiver floorNumber max length is 2');
}
this.floorNumber = floorNumber;
};
receiver.prototype.setBuldingId = function(buldingId) {
if (buldingId.length > 3) {
throw new Error('Receiver buldingId max length is 3');
}
this.buldingId = buldingId;
};
receiver.prototype.setSendNotification = function(sendNotification) {
if (sendNotification) {
this.sendNotification = '1';
} else {
this.sendNotification = '0';
}
};
module.exports = receiver;