-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
313 lines (280 loc) · 9.51 KB
/
storage.js
File metadata and controls
313 lines (280 loc) · 9.51 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
/**
* Data Storage Manager
* Manages employee and duty data using localStorage
*/
class DataStorage {
constructor() {
this.STORAGE_KEY_EMPLOYEES = 'dienstplan_employees';
this.STORAGE_KEY_DUTIES = 'dienstplan_duties';
}
/**
* Get all employees
* @returns {Array} Array of employee names
*/
getEmployees() {
try {
const data = localStorage.getItem(this.STORAGE_KEY_EMPLOYEES);
if (!data) {
return [];
}
const parsed = JSON.parse(data);
if (!Array.isArray(parsed)) {
console.error('Fehler: Mitarbeiter-Daten sind kein Array. Zurücksetzen auf leeres Array');
return [];
}
return parsed;
} catch (e) {
console.error('Fehler beim Laden der Mitarbeiter-Daten:', e);
return [];
}
}
/**
* Save employees list
* @param {Array} employees - Array of employee names
*/
saveEmployees(employees) {
try {
if (!Array.isArray(employees)) {
console.error('Fehler: employees muss ein Array sein');
throw new TypeError('employees muss ein Array sein');
}
localStorage.setItem(this.STORAGE_KEY_EMPLOYEES, JSON.stringify(employees));
} catch (e) {
console.error('Fehler beim Speichern der Mitarbeiter-Daten:', e);
throw e;
}
}
/**
* Add a new employee
* @param {string} employeeName
* @returns {boolean} Success status
*/
addEmployee(employeeName) {
const employees = this.getEmployees();
if (employees.includes(employeeName)) {
return false; // Already exists
}
employees.push(employeeName);
this.saveEmployees(employees.sort());
return true;
}
/**
* Remove an employee and all their duties
* @param {string} employeeName
*/
removeEmployee(employeeName) {
// Remove from employees list
const employees = this.getEmployees();
const filtered = employees.filter(e => e !== employeeName);
this.saveEmployees(filtered);
// Remove all duties for this employee
const allDuties = this.getAllDuties();
delete allDuties[employeeName];
this.saveAllDuties(allDuties);
}
/**
* Get all duties data (all employees, all months)
* @returns {Object} Object with structure: {employeeName: {year-month: [duties]}}
*/
getAllDuties() {
try {
const data = localStorage.getItem(this.STORAGE_KEY_DUTIES);
if (!data) {
return {};
}
const parsed = JSON.parse(data);
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
console.error('Fehler: Dienst-Daten sind kein gültiges Objekt. Zurücksetzen auf leeres Objekt');
return {};
}
return parsed;
} catch (e) {
console.error('Fehler beim Laden der Dienst-Daten:', e);
return {};
}
}
/**
* Save all duties data
* @param {Object} duties
*/
saveAllDuties(duties) {
try {
if (typeof duties !== 'object' || duties === null || Array.isArray(duties)) {
console.error('Fehler: duties muss ein gültiges Objekt sein');
throw new TypeError('duties muss ein gültiges Objekt sein');
}
localStorage.setItem(this.STORAGE_KEY_DUTIES, JSON.stringify(duties));
} catch (e) {
console.error('Fehler beim Speichern der Dienst-Daten:', e);
throw e;
}
}
/**
* Get duties for a specific employee and month
* @param {string} employeeName
* @param {number} year
* @param {number} month (1-12)
* @returns {Array} Array of duty objects
*/
getDutiesForMonth(employeeName, year, month) {
try {
const allDuties = this.getAllDuties();
const monthKey = `${year}-${String(month).padStart(2, '0')}`;
if (!allDuties[employeeName] || !allDuties[employeeName][monthKey]) {
return [];
}
// Convert date strings back to Date objects
return allDuties[employeeName][monthKey].map(duty => {
try {
const dateObj = new Date(duty.date);
if (isNaN(dateObj.getTime())) {
console.error(`Fehler: Ungültiges Datum für Dienst: ${duty.date}`);
return null;
}
return {
...duty,
date: dateObj
};
} catch (e) {
console.error('Fehler beim Konvertieren des Datums:', e);
return null;
}
}).filter(duty => duty !== null); // Filter out invalid entries
} catch (e) {
console.error('Fehler beim Laden der Dienste für Monat:', e);
return [];
}
}
/**
* Save duties for a specific employee and month
* @param {string} employeeName
* @param {number} year
* @param {number} month (1-12)
* @param {Array} duties - Array of duty objects
*/
saveDutiesForMonth(employeeName, year, month, duties) {
try {
if (!Array.isArray(duties)) {
console.error('Fehler: duties muss ein Array sein');
throw new TypeError('duties muss ein Array sein');
}
const allDuties = this.getAllDuties();
const monthKey = `${year}-${String(month).padStart(2, '0')}`;
if (!allDuties[employeeName]) {
allDuties[employeeName] = {};
}
// Convert Date objects to strings for storage
allDuties[employeeName][monthKey] = duties.map(duty => {
if (!duty.date || !(duty.date instanceof Date)) {
console.error('Fehler: Dienst hat kein gültiges Datum:', duty);
throw new TypeError('Dienst muss ein gültiges Date-Objekt haben');
}
return {
...duty,
date: duty.date.toISOString()
};
});
this.saveAllDuties(allDuties);
} catch (e) {
console.error('Fehler beim Speichern der Dienste für Monat:', e);
throw e;
}
}
/**
* Add a duty for an employee
* @param {string} employeeName
* @param {number} year
* @param {number} month (1-12)
* @param {Date} date
* @param {number} share (1.0 or 0.5)
*/
addDuty(employeeName, year, month, date, share) {
const duties = this.getDutiesForMonth(employeeName, year, month);
// Check if duty already exists for this date
const existingIndex = duties.findIndex(d =>
d.date.toDateString() === date.toDateString()
);
if (existingIndex >= 0) {
// Update existing duty
duties[existingIndex].share = share;
} else {
// Add new duty
duties.push({ date, share });
}
// Sort by date
duties.sort((a, b) => a.date - b.date);
this.saveDutiesForMonth(employeeName, year, month, duties);
}
/**
* Remove a duty
* @param {string} employeeName
* @param {number} year
* @param {number} month (1-12)
* @param {Date} date
*/
removeDuty(employeeName, year, month, date) {
const duties = this.getDutiesForMonth(employeeName, year, month);
const filtered = duties.filter(d =>
d.date.toDateString() !== date.toDateString()
);
this.saveDutiesForMonth(employeeName, year, month, filtered);
}
/**
* Get all duties for all employees in a specific month
* @param {number} year
* @param {number} month (1-12)
* @returns {Object} Object with employee names as keys
*/
getAllEmployeeDutiesForMonth(year, month) {
const employees = this.getEmployees();
const result = {};
employees.forEach(employee => {
result[employee] = this.getDutiesForMonth(employee, year, month);
});
return result;
}
/**
* Clear all data
*/
clearAll() {
localStorage.removeItem(this.STORAGE_KEY_EMPLOYEES);
localStorage.removeItem(this.STORAGE_KEY_DUTIES);
}
/**
* Export data as JSON
* @returns {string} JSON string
*/
exportData() {
try {
return JSON.stringify({
employees: this.getEmployees(),
duties: this.getAllDuties()
}, null, 2);
} catch (e) {
console.error('Fehler beim Exportieren der Daten:', e);
throw new Error('Fehler beim Exportieren der Daten: ' + e.message);
}
}
/**
* Import data from JSON
* @param {string} jsonString
* @returns {boolean} Success status
*/
importData(jsonString) {
try {
const data = JSON.parse(jsonString);
if (data.employees) {
this.saveEmployees(data.employees);
}
if (data.duties) {
this.saveAllDuties(data.duties);
}
return true;
} catch (e) {
console.error('Import failed:', e);
return false;
}
}
}
// Make it available globally
window.DataStorage = DataStorage;