-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet.ts
253 lines (220 loc) · 6.64 KB
/
spreadsheet.ts
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
import SlackMessage from "./sendMessage";
import {Names} from "./slugs";
import * as moment from "moment";
export default class Spreadsheet {
private sheet;
private name: string;
private thisYear: number;
private thisMonth: number;
private thisDate: number;
private doc;
constructor(sheet, name, doc) {
this.sheet = sheet;
this.name = name;
this.thisYear = moment().year();
this.thisMonth = moment().month();
this.doc = doc;
}
// Getting row 5, col DK and > for all the DATES in the sheet
public getRowColOfDates() {
return new Promise((resolve, reject) => {
this.sheet.getCells({
"max-row": 5,
"min-col": 6,
"min-row": 5,
"return-empty": false,
}, (err, arr) => {
if (err) {
return reject(err);
}
return resolve(arr);
});
});
}
public getWeekdays(arr, second) {
const monthDatesWithoutWeekend: any[] = [];
for (const item of arr) {
const dates = item.value;
const day: number = moment(dates, "MM-DD-YYYY").day();
const year: number = moment(dates, "MM-DD-YYYY").year();
const month: number = moment(dates, "MM-DD-YYYY").month();
// Check for prev months
if (second) {
if (this.thisMonth === 0) {
if ((this.thisYear - 1) === year
&& 11 === month
&& day !== 6 && day !== 0) {
monthDatesWithoutWeekend.push(item);
}
}else {
if (this.thisYear === year
&& (this.thisMonth - 1) === month
&& day !== 6 && day !== 0) {
monthDatesWithoutWeekend.push(item);
}
}
}else {
if (this.thisYear === year
&& (this.thisMonth) === month
&& day !== 6 && day !== 0) {
monthDatesWithoutWeekend.push(item);
}
}
}
return monthDatesWithoutWeekend;
}
public getSpecificMonthSpan(arr, second) {
const monthCols: number[] = [];
// Getting right columns of month and year span
for (const item of arr) {
const dates = item.value;
const year: number = moment(dates, "MM-DD-YYYY").year();
const month: number = moment(dates, "MM-DD-YYYY").month();
// If it's the 2, check for previous month
if (second) {
// If it's January, check for december last year.
if (this.thisMonth === 0) {
if (11 === month && (this.thisYear - 1) === year) {
monthCols.push(item.col);
}
}else {
if ((this.thisMonth - 1) === month && this.thisYear === year) {
monthCols.push(item.col);
}
}
}else {
if ((this.thisMonth) === month && this.thisYear === year) {
monthCols.push(item.col);
}
}
}
return monthCols;
}
// Getting holidays from HOLIDAY-sheet
public checkHolidays() {
return new Promise((resolve, reject) => {
this.doc.getInfo((error, info) => {
for (const worksheet of info.worksheets) {
if (worksheet.title === "Holidays") {
worksheet.getCells({
"max-col": 1,
"min-col": 1,
"return-empty": false,
}, (err, arr) => {
if (err) {
return reject(err);
}
return resolve(arr);
});
}
}
});
});
}
public workingDates(datesWithoutWeekend, holidaysArr, second) {
const arrDatesOfVacation: number[] = [];
for (const item of holidaysArr) {
const dates = item.value;
const months: number = moment(dates).month();
const years: number = moment(dates).year();
if (second) {
if (this.thisMonth === 0) {
if ((this.thisYear - 1) === years && 11 === months) {
arrDatesOfVacation.push(dates);
}
}else {
if (this.thisYear === years && (this.thisMonth - 1) === months) {
arrDatesOfVacation.push(dates);
}
}
}else {
if (this.thisYear === years && this.thisMonth === months) {
arrDatesOfVacation.push(dates);
}
}
}
for (const item of datesWithoutWeekend) {
const date = moment(item.value, "MM-DD-YYYY").format("YYYY-MM-DD");
for (const item2 of arrDatesOfVacation) {
const vacationDate = item2.toString();
if (vacationDate === date) {
const index = datesWithoutWeekend.indexOf(item);
if (index > -1) {
datesWithoutWeekend.splice(index, 1);
}
}
}
}
return datesWithoutWeekend;
}
// Finding row 20 with reported time by the staff
public findRowOfReportedTime() {
return new Promise((resolve, reject) => {
this.sheet.getCells({
"max-col": 2,
"min-col": 2,
"min-row": 20,
"return-empty": false,
}, (err, allCells) => {
if (err) {
return reject(err);
}
return resolve(allCells);
});
});
}
public getRowNumberOfReportedTime(reportedTimeObj) {
let rowNumber: number;
for (const item of reportedTimeObj){
if (item.value === "Total rapporterad tid") {
rowNumber = item.row;
}
}
return rowNumber;
}
public getTimeReported(row, monthSpan) {
// New staff doesnt have the current month.
if ( monthSpan.length !== 0 ) {
const maxCol = monthSpan[monthSpan.length - 1];
return new Promise((resolve, reject) => {
this.sheet.getCells({
"max-col": maxCol,
"max-row": row,
"min-col": monthSpan[0],
"min-row": row,
"return-empty": false,
}, (err, arrHourCells) => {
if (err) {
return reject(err);
}
return resolve(arrHourCells);
});
});
}else {
return undefined;
// return "du behöver inte fylla i tidsrapporten än!";
}
}
public checkTimeFilled(workingDates, arrHourCells) {
if ( typeof arrHourCells !== "undefined" ) {
let isFilled: boolean = true;
const message = "glöm ej att fylla i tidsrapporten! :scream:";
// const message2 = "du har fyllt i tidsrapporten! YIHOOO! :sunglasses:";
for (const item2 of workingDates) {
for (const item of arrHourCells){
if (item2.col === item.col) {
const timeInCol: number = parseInt(item.value, 0);
if (timeInCol === 0) {
isFilled = false;
}
}
}
}
const name = Names[this.name] ? `<@${Names[this.name]}>` : this.name;
if (!isFilled) {
const unfilledMessage = new SlackMessage( name, message);
unfilledMessage.sendMessage();
}
}
}
}