76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
export function loadFromJSON(json) {
|
|
let parsed;
|
|
try {
|
|
parsed = JSON.parse(json);
|
|
} catch (e) {
|
|
throw new Error('Ungültige JSON-Datei: ' + e.message);
|
|
}
|
|
if (typeof parsed !== 'object' || parsed === null) {
|
|
throw new Error('Ungültiges Dateiformat: Objekt erwartet');
|
|
}
|
|
return {
|
|
employees: Array.isArray(parsed.employees) ? parsed.employees : [],
|
|
calendar: (parsed.calendar && typeof parsed.calendar === 'object')
|
|
? {
|
|
holidays: Array.isArray(parsed.calendar.holidays) ? parsed.calendar.holidays : [],
|
|
companyClosures: Array.isArray(parsed.calendar.companyClosures) ? parsed.calendar.companyClosures : []
|
|
}
|
|
: { holidays: [], companyClosures: [] },
|
|
history: Array.isArray(parsed.history) ? parsed.history : []
|
|
};
|
|
}
|
|
|
|
export function saveToJSON(state) {
|
|
return JSON.stringify({
|
|
employees: state.employees,
|
|
calendar: state.calendar,
|
|
history: state.history
|
|
}, null, 2);
|
|
}
|
|
|
|
export function createEmployee(name) {
|
|
const trimmed = (name ?? '').trim();
|
|
if (!trimmed) throw new Error('Mitarbeitername darf nicht leer sein');
|
|
return {
|
|
id: crypto.randomUUID(),
|
|
name: trimmed,
|
|
constraints: {
|
|
neverDays: [],
|
|
lowPriorityDays: [],
|
|
onlyDays: [],
|
|
maxPerMonth: null,
|
|
minGapDays: 0,
|
|
vacations: []
|
|
}
|
|
};
|
|
}
|
|
|
|
export function removeEmployee(state, id) {
|
|
state.employees = state.employees.filter(e => e.id !== id);
|
|
}
|
|
|
|
export function toggleHoliday(state, isoDate) {
|
|
const arr = state.calendar.holidays;
|
|
const idx = arr.indexOf(isoDate);
|
|
if (idx >= 0) arr.splice(idx, 1); else arr.push(isoDate);
|
|
}
|
|
|
|
export function toggleClosure(state, isoDate) {
|
|
const arr = state.calendar.companyClosures;
|
|
const idx = arr.indexOf(isoDate);
|
|
if (idx >= 0) arr.splice(idx, 1); else arr.push(isoDate);
|
|
}
|
|
|
|
// Adds entries to history, replacing any existing entries for the same dates
|
|
export function addHistoryEntries(state, entries) {
|
|
if (!entries?.length) return;
|
|
const dates = new Set(entries.map(e => e.date));
|
|
state.history = state.history.filter(h => !dates.has(h.date));
|
|
state.history.push(...entries);
|
|
state.history.sort((a, b) => a.date.localeCompare(b.date));
|
|
}
|
|
|
|
export function removeHistoryEntry(state, isoDate) {
|
|
state.history = state.history.filter(h => h.date !== isoDate);
|
|
}
|