Files
Meeting-Mixer/js/data.js
T

88 lines
2.7 KiB
JavaScript

function parseSettings(raw) {
const n = raw?.moderatorsPerDay;
const valid = Number.isInteger(n) && n >= 1 && n <= 5;
return { moderatorsPerDay: valid ? n : 1 };
}
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 : [],
settings: parseSettings(parsed.settings)
};
}
export function saveToJSON(state) {
return JSON.stringify({
employees: state.employees,
calendar: state.calendar,
history: state.history,
settings: state.settings
}, 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 with the same (date, employeeId)
export function addHistoryEntries(state, entries) {
if (!entries?.length) return;
const keys = new Set(entries.map(e => e.date + '|' + e.employeeId));
state.history = state.history.filter(h => !keys.has(h.date + '|' + h.employeeId));
state.history.push(...entries);
state.history.sort((a, b) => a.date.localeCompare(b.date));
}
export function removeHistoryEntry(state, isoDate, employeeId) {
if (employeeId === undefined) {
state.history = state.history.filter(h => h.date !== isoDate);
} else {
state.history = state.history.filter(h => !(h.date === isoDate && h.employeeId === employeeId));
}
}