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
|
import { jsonapi } from "./jsonapi";
type RestrictedDate = {
year: Number,
month: Number,
day: Number,
reason: string | null,
lock: boolean
}
class RestrictedDatesHelper
{
static #loadedYears : Number[] = [];
static #restrictedDates: RestrictedDate[] = [];
static isDateRestricted(date: Date, returnBoolean: Boolean = false): RestrictedDate | Boolean {
const restrictedDate : RestrictedDate | undefined = this.#restrictedDates.find(item => {
return item.year === date.getFullYear()
&& item.month === date.getMonth() + 1
&& item.day === date.getDate();
});
if (returnBoolean) {
return !!restrictedDate;
}
return restrictedDate ?? this.#convertDate(date, null, false);
}
static async loadRestrictedDatesByYear(year: Number): Promise<void> {
if (this.#loadedYears.includes(year)) {
return Promise.reject();
}
this.#loadedYears.push(year);
jsonapi.withPromises().request('holidays', {data: {
'filter[year]': year
}}).then((response: [] | Object) => {
// Since PHP will return an empty object as an array,
// we need to check
if (Array.isArray(response)) {
return;
}
for (const [date, data] of Object.entries(response)) {
this.#addRestrictedDate(
new Date(date),
data.holiday,
data.mandatory
);
}
});
}
static #addRestrictedDate(date: Date, reason: string, lock: boolean = true): void {
const restricted = this.#convertDate(date, reason, lock);
this.#restrictedDates = this.#restrictedDates.filter(item => {
return item.year !== restricted.year
|| item.month !== restricted.month
|| item.day !== restricted.day;
});
this.#restrictedDates.push(restricted);
}
static removeRestrictedDate(date: Date): void {
this.#restrictedDates = this.#restrictedDates.filter(item => {
return item.year !== date.getFullYear()
|| item.month !== date.getMonth() + 1
|| item.day !== date.getDate();
});
}
static #convertDate(date: Date, reason: string | null, lock: boolean): RestrictedDate {
return {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
reason,
lock
};
}
}
export default RestrictedDatesHelper;
|