-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
449 lines (394 loc) · 15.2 KB
/
main.ts
File metadata and controls
449 lines (394 loc) · 15.2 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// import inquirer from 'inquirer';
import { Employee } from './employees.js';
import { HolidayRequests, statusPending, statusApproved, statusRejected } from './holidayRequests.js';
import { HolidayRules } from './holidayRules.js';
import { format,areIntervalsOverlapping , formatDistance, formatRelative, isValid, isWeekend, eachDayOfInterval, differenceInDays, subDays } from 'date-fns';
import express, {Request, response, Response} from 'express';
import path from 'path';
import ejs from 'ejs';
import axios, { AxiosResponse } from 'axios';
import bodyParser from 'body-parser';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded());
app.listen(port, () => {
console.log(`Server started at ${port} port`);
});
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
const employees: Employee[] = [];
employees.push({
id: 1,
name: "Yura",
remainingHolidays: 14,
});
employees.push({
id: 2,
name: "Sveta",
remainingHolidays: 14,
});
employees.push({
id: 3,
name: "Yaroslav",
remainingHolidays: 15,
});
employees.push({
id: 4,
name: "Dima",
remainingHolidays: 13,
});
const requests: HolidayRequests[] = [];
requests.push({
employeeId: 1,
startDate: "2024-04-01",
endDate: "2024-04-15",
status: statusPending,
});
const approvedOrRejectedRequests: HolidayRequests[] = [];
const rules: HolidayRules[] = [];
const rule = new HolidayRules("2024-03-16", "2024-03-18");
rules.push(rule);
let successMessage:string;
let failMessage:string;
// function arrayToObject(arr:[]) {
// return arr.reduce((acc, currentValue, index) => {
// acc[index] = currentValue;
// return acc;
// }, {});
// }
function main(){
interface Holiday {
date: string;
localName: string;
name: string;
countryCode: string;
}
async function fetchHolidays(year: number, countryCode: string): Promise<Holiday[]> {
try {
const response: AxiosResponse<Holiday[]> = await axios.get<Holiday[]>(`https://date.nager.at/api/v3/publicholidays/${year}/${countryCode}`);
return response.data;
} catch (error) {
console.error('An error occurred while executing the request:', error);
return [];
}
}
//const holidaysPromise: Promise<Holiday[]> = fetchHolidays(2024, 'UA');
const holidays: Holiday[] = [];
let relevantHolidays: Holiday[] = [];
fetchHolidays(2024, 'UA')
.then((holidaysData: Holiday[]) => {
holidays.push(...holidaysData);
console.log('Public Holidays List:', holidays);
})
.catch((error) => {
console.error('An error occurred while receiving holidays:', error);
});
function checkDates(employeeId:number,startDate:string,endDate:string,){
try {
const periodOfVacation = differenceInDays(endDate,startDate);
const isHolidayOvarlappingWithBlackoutPeriod = !areIntervalsOverlapping({start:rules[0].blackoutStartDate,end:rules[0].blackoutEndDate},{start:startDate,end:endDate});
const employee = employees.find((emp) => emp.id === employeeId);
if(periodOfVacation>0 && differenceInDays(startDate,Date())>0){
if(employee) {
// @ts-ignore
if(employee.remainingHolidays>=periodOfVacation){
if(isHolidayOvarlappingWithBlackoutPeriod) {
if(periodOfVacation<=rules[0].maxConsecutiveDays){
return true;
} else{
console.log("You chose too much days for your holiday!!!");
return false;
}
}else{
console.log("There is a Blackout Period in the dates you chose!!!");
return false;
}
}else{
console.log("You chose too much days for your holiday!!!");
return false;
}
}else{
console.log("There is no employee with such id, please enter the correct eployee id!!!");
return false;
}
}else{
console.log("You chose the wrong period of holiday!!!");
return false;
}
} catch (error) {
console.log("The date was entered incorrectly");
return false;
}
}
function updateRequest(id:number,startDate:string,endDate:string){
console.log(startDate + " " + endDate);
console.log(id)
console.log(typeof id)
console.log(requests[id]);
const startDateObj = new Date(startDate);
const endDateObj = new Date(endDate);
console.log(startDateObj);
console.log(endDateObj);
if (isValid(startDateObj) && isValid(endDateObj) && requests[id] !== undefined) {
const formattedsStartDate = format(startDateObj, 'yyyy-MM-dd');
const formattedsEndDate = format(endDateObj, 'yyyy-MM-dd');
if(checkDates(requests[id].employeeId, formattedsStartDate, formattedsEndDate)){
requests[id].startDate = formattedsStartDate;
requests[id].endDate = formattedsEndDate;
requests[id].status = statusPending;
console.log(startDateObj + " " + endDateObj);
console.log("Перемога");
console.log(requests[id]);
return requests[id];
}
}
}
app.post('/update-request', (req, res) => {
const startDate:string = req.body.startDate;
const endDate:string = req.body.endDate;
const id = Number(req.body.idOfRequest);
updateRequest(id,startDate,endDate);
});
app.post('/delete-request', (req, res) => {
try {
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.delete('/delete-request', (req, res) => {
try {
const requestId:number = Number(req.query.requestId);
const result = req.query.result;
if(result){
requests.splice(requestId, 1);
console.log(requests);
}
successMessage = "Holiday request deleted successfully!";
res.redirect('/holidays');
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.get('/update-request', (req, res) => {
try {
const idOfRequest: number = Number(req.query.requestId);
res.render('update-request', { idOfRequest: idOfRequest});
} catch (error) {
res.status(500).send(error);
}
});
app.get('/employees', (req, res) => {
try {
const employeesJson = JSON.stringify(employees);
console.log(req);
res.render('employees', { employees: JSON.parse(employeesJson) });
} catch (e) {
res.status(500).send('Internal Server Error');
}
});
app.get('/holidays', (req, res) => {
try {
relevantHolidays = [];
const dates = requests.map(request => {
return {
startDate: request.startDate,
endDate: request.endDate
};
});
holidays.forEach(holiday => {
dates.forEach(date => {
if (areIntervalsOverlapping(
{start: new Date(holiday.date), end: new Date(holiday.date)},
{start: new Date(date.startDate), end: new Date(date.endDate)}
)) {
relevantHolidays.push(holiday);
}
});
});
console.log("Relevant Holidays:", relevantHolidays);
res.render('holidays', {requests, approvedOrRejectedRequests, successMessage, relevantHolidays});
} catch (e) {
res.status(500).send('Internal Server Error');
}
});
app.post('/approve-reject-holiday', (req, res) => {
try {
const idOfEmployee = parseInt(req.body.idOfEmployee);
const action = req.body.action;
const requestId = parseInt(req.body.requestId);
const request = requests.find((r) => r.employeeId === idOfEmployee);
if (request) {
if (action === 'approve') {
request.status = statusApproved;
const holidayLength = differenceInDays(request.endDate, request.startDate);
employees[request.employeeId-1].remainingHolidays = employees[request.employeeId-1].remainingHolidays - holidayLength;
approvedOrRejectedRequests.push(request);
requests.splice(requestId, 1);
successMessage = 'Holiday request approved successfully!'
} else if (action === 'reject') {
approvedOrRejectedRequests.push(request);
requests.splice(requestId, 1);
request.status = statusRejected;
successMessage = 'Holiday request rejected successfully!'
}else if (action === 'update') {
res.redirect(`/update-request?requestId=${requestId}`);
}
res.redirect('/holidays');
} else {
res.status(404).send('Request not found');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.post("/add-holiday", (req, res) => {
const employeeId = parseInt(req.body.employeeId as string);
const startDate = req.body.startDate as string;
const endDate = req.body.endDate as string;
if(checkDates(employeeId,startDate,endDate)==true){
requests.push(new HolidayRequests(employeeId, startDate, endDate));
successMessage = "Holiday request created successfully!";
res.redirect('/holidays');
}else {
res.redirect('/add-holiday');
}
});
app.get('/add-holiday', (req, res) => {
try {
res.render('add-holiday', {failMessage, holidays});
} catch (error) {
res.status(500).send(error);
}
});
}
/*async function addEmployee() {
const { id, name, remainingHolidays } = await inquirer.prompt([
{
type: 'input',
name: 'id',
message: 'Enter the id of the new empoyee',
},
{
type: 'input',
name: 'name',
message: 'Enter the name of the new employee:',
},
{
type: 'number',
name: 'remainingHolidays',
message: 'Enter the remaining holidays for the new employee:',
},
]);
employees.push(new Employee(id, name, remainingHolidays));
console.log('New employee added successfully!');
}
// View of the list of added Employees
function viewEmployees() {
console.log('List of employees:');
employees.forEach( (emp) => {
console.log(`${emp.id} ${emp.name}: ${emp.remainingHolidays} days remaining holidays`);
});
}
//Submit Holiday Request
async function submitHolidayRequest() {
const { employeeId, startDate, endDate, status } = await inquirer.prompt([
{
type: 'list',
name: 'employeeId',
message: 'Choose the employee:',
choices: employees.map((employee) => employee.id),
},
{
type: 'input',
name: 'startDate',
message: 'Enter the start date of the holiday (YYYY-MM-DD):',
},
{
type: 'input',
name: 'endDate',
message: 'Enter the end date of the holiday (YYYY-MM-DD):',
},
]);
function parseDate(input: string): Date {
const parts = input.split('-');
return new Date(+parts[0], +parts[1], +parts[2]);
}
// Check Blackout period function
if(areIntervalsOverlapping({start:rules[0].blackoutStartDate,end:rules[0].blackoutEndDate},{start:startDate,end:endDate})){
console.log("The requested holiday period falls within the blackout period.");
return;
}else{
console.log("The requested holiday period is outside the blackout period.");
}
const daysRequested = differenceInDays(
parseDate(endDate),
parseDate(startDate)
)
// Check Max Consecutive days function
if (daysRequested > rules[0].maxConsecutiveDays //|| daysRequested > employees[employeeId].remainingHolidays) {
console.log(`Request exceeds the maximum consecutive holiday limit of ${rules[0].maxConsecutiveDays} days.`);
return;
}
const employee = employees.find((emp) => emp.id === employeeId);
if (employee) {
if(daysRequested > employee.remainingHolidays){
console.log('This employee does not have this much holidays!');
}else{
requests.push( new HolidayRequests (employeeId, startDate, endDate, status));
console.log('Holiday request submitted successfully!');
}
} else {
console.log('Employee not found!');
}
}
// View Pending Holiday Requests
function viewPendingHolidayRequests() {
console.log('List of pending holiday requests:');
requests.filter((request) => request.status === 'Pending').forEach((request) => {
console.log(`${request.employeeId}: Start date ${request.startDate} to End date ${request.endDate} - ${request.status}`);
});
}
//Approving or Reject Request
async function approveRejectHolidayRequest() {
const pendingRequests = requests.filter((request) => request.status === 'Pending');
if (pendingRequests.length === 0) {
console.log('No pending holiday requests.');
return;
}
const { requestToProcess } = await inquirer.prompt([
{
type: 'list',
name: 'requestToProcess',
message: 'Choose a pending holiday request to approve or reject:',
choices: pendingRequests.map((request) => `${request.employeeId}: Start date ${request.startDate} - End date ${request.endDate}`),
},
]);
const selectedRequest = pendingRequests.find(
(request) =>
`${request.employeeId}: Start date ${request.startDate} - End date ${request.endDate}` === requestToProcess
);
if (selectedRequest) {
const { approve } = await inquirer.prompt([
{
type: 'confirm',
name: 'approve',
message: 'Do you want to approve this holiday request?',
default: true,
},
]);
if (approve) {
selectedRequest.status = 'Approved';
console.log('Holiday request approved!');
} else {
selectedRequest.status = 'Rejected';
console.log('Holiday request rejected!');
}
}
}
*/
main();