-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
233 lines (161 loc) · 7.47 KB
/
calculator.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(function(){
"use strict";
const WORK_TIME_START = 9;
const WORK_TIME_END = 17;
const WORK_TIME_PER_DAY = WORK_TIME_END - WORK_TIME_START;
const MILLISECONDS_TO_MINUTES = 1000 * 60;
const HOURS_TO_MINUTES = 60;
const WORK_TIME_IN_MINUTES = WORK_TIME_PER_DAY * 60;
const FIRST_WORKDAY = 1;
const LAST_WORKDAY = 5;
const HTML_ELEMENT = document.getElementById('bug-reports');
HTML_ELEMENT.innerHTML = '';
const IsWorkingDay = (reportDate) => {
return reportDate.getDay() > 0 && reportDate.getDay() < 6
};
const IsInWorkingTime = (reportDate) => {
return reportDate.getHours() >= WORK_TIME_START && reportDate.getHours() < WORK_TIME_END;
};
const GetNextWorkDay = (reportDate) => {
if ( reportDate.getDay()+1 >= FIRST_WORKDAY && reportDate.getDay()+1 <= LAST_WORKDAY) {
return (new Date(reportDate)).setDate(reportDate.getDate() + 1);
}
if ( reportDate.getDay() === 5 ){
return (new Date(reportDate)).setDate(reportDate.getDate() + 3);
}
showError('Wrong date!' + reportDate.getDay() );
};
const CalculateTimeLeft = (startDate, timeInMinutes) => {
let endDate = new Date(startDate);
endDate.setHours(WORK_TIME_END);
endDate.setMinutes(0);
let availableTime = new Date(endDate - startDate).getTime();
let availableMinutes = availableTime / MILLISECONDS_TO_MINUTES;
return ( timeInMinutes > WORK_TIME_IN_MINUTES ) ? timeInMinutes - availableMinutes : timeInMinutes ;
};
const showError = (message) => {
console.error(message);
};
const validateData = (submitDate, turnAroundTime) => {
try {
submitDate = new Date(submitDate);
}catch (e){
throw 'Invalid date!';
}
if( submitDate instanceof Date === false ){
return 'Invalid date!';
}
if( typeof turnAroundTime !== "number" ){
return 'The second parameter must be integer!';
}
if ( turnAroundTime < 0 ){
return 'Turn around time must be bigger than zero!';
}
if ( ! IsInWorkingTime(submitDate) || ! IsWorkingDay(submitDate) ){
return 'Bug must be reported between 9AM - 5PM!';
}
return true;
};
const CalculateDueDate = (submitDate, turnAroundTime) => {
let validateResult = validateData(submitDate, turnAroundTime);
if ( validateResult !== true ){
return validateResult;
}
let dueDate = new Date(submitDate);
let turnAroundTimeInMinutes = turnAroundTime * HOURS_TO_MINUTES;
let submitDateHours = submitDate.getHours();
let submitDateMinutes = submitDate.getMinutes();
let timeLeftOnSubmitDay = WORK_TIME_IN_MINUTES - (
(submitDateHours * HOURS_TO_MINUTES + submitDateMinutes) - WORK_TIME_START * HOURS_TO_MINUTES);
if ( timeLeftOnSubmitDay > turnAroundTimeInMinutes){
dueDate.setMinutes( dueDate.getMinutes() + turnAroundTimeInMinutes );
}
else {
turnAroundTimeInMinutes = turnAroundTimeInMinutes - timeLeftOnSubmitDay;
while ( turnAroundTimeInMinutes > 0 ) {
let nextWorkingDay = new Date(GetNextWorkDay(dueDate));
nextWorkingDay.setHours(WORK_TIME_START);
nextWorkingDay.setMinutes(0);
dueDate = nextWorkingDay;
if ( turnAroundTimeInMinutes < WORK_TIME_IN_MINUTES ) {
dueDate.setHours( WORK_TIME_START + turnAroundTimeInMinutes / 60);
dueDate.setMinutes( turnAroundTimeInMinutes % 60 );
turnAroundTimeInMinutes = 0;
}
if ( turnAroundTimeInMinutes !== 0 ){
turnAroundTimeInMinutes = CalculateTimeLeft(nextWorkingDay, turnAroundTimeInMinutes);
}
}
}
return dueDate;
};
const showTemplate = (data, error) => {
let errorClass = error.length > 0 ? 'error' : '';
let hours = (data.turnAroundTime > 1 ) ? 'hours' : 'hour';
HTML_ELEMENT.innerHTML += `
<div class="report ${errorClass}">
<div class="submit-date"><span class="title">Bug report date: </span>${data.submitDateString} ${data.submitTimeString}</div>
<div class="due-date"><span class="title">Due date: </span>${data.dueDateString} ${data.dueTimeString}</div>
<div class="turn-around-time"><span class="title">Turn around time: </span>${data.turnAroundTime + ' ' + hours} </div>
<div class="errors">${error.join(' ')}</div>
</div>
`;
};
const formatDateString = (date) => {
date = new Date(date);
let month = (date.getMonth() + 1);
let day = date.getDate();
if ( month < 10 ) month = '0' + month;
if ( day < 10 ) day = '0' + day;
return date.getFullYear() + "-" + month + "-" + day;
};
const formatTimeString = (date) => {
date = new Date(date);
return date.getHours() + ":" + date.getMinutes();
};
/** TEST CASES **/
let testCases = [
{ submitDate: new Date("2018-06-27 15:22"), turnAroundTime: 16 },
{ submitDate: new Date("2018-06-21 11:28"), turnAroundTime: 3 },
{ submitDate: new Date("2018-06-21 16:28"), turnAroundTime: 1 },
{ submitDate: new Date("2018-06-21 9:20"), turnAroundTime: 7 },
{ submitDate: new Date("2018-06-29 16:42"), turnAroundTime: 14 },
{ submitDate: new Date("2018-06-27 19:22"), turnAroundTime: 2 },
{ submitDate: new Date("2018-06-27 8:22"), turnAroundTime: 28 },
{ submitDate: new Date("2018-06-27 15:22"), turnAroundTime: -4 },
{ submitDate: new Date("2018-06-27 16:42"), turnAroundTime: 0.5 },
{ submitDate: new Date("2018-06-27 16:22"), turnAroundTime: 1.5 },
{ submitDate: new Date("2018-06-27 16:22"), turnAroundTime: 49.5 },
{ submitDate: "2018-06-25 14:11", turnAroundTime: 10 },
{ submitDate: "2:12PM", turnAroundTime: 5 },
];
testCases.forEach((test) => {
let submitDateString = '<span class="error-badge"> INVALID</span>';
let submitTimeString = '<span class="error-badge"> INVALID</span>';
let dueDateString = '<span class="error-badge"> INVALID</span>';
let dueTimeString = '<span class="error-badge"> INVALID</span>';
let error = [];
let submitDate = new Date(test.submitDate);
if ( submitDate instanceof Date === false || submitDate.toString() === 'Invalid Date') {
error.push('Bug report date is not valid date!');
submitDateString = '( ' + test.submitDate + ' ) ' + ' <span class="error-badge"> INVALID</span>' ;
}else{
submitDateString = formatDateString(submitDate);
submitTimeString = formatTimeString(submitDate);
let dueDate = CalculateDueDate(submitDate, test.turnAroundTime);
if ( dueDate instanceof Date) {
dueDateString = formatDateString(dueDate);
dueTimeString = formatTimeString(dueDate);
}else{
error.push( dueDate );
}
}
showTemplate({
submitDateString,
submitTimeString,
dueDateString,
dueTimeString,
turnAroundTime: test.turnAroundTime
}, error)
});
})();