feat(data): dedupe history by (date, employeeId)
This commit is contained in:
1 parent
3d314e84fd
commit
23683102bc
3 files changed
+51
-4
No files matched your search
+3
-3
@@ -61,11 +61,11 @@ export function toggleClosure(state, isoDate) {
|
||||
if (idx >= 0) arr.splice(idx, 1); else arr.push(isoDate);
|
||||
}
|
||||
|
||||
// Adds entries to history, replacing any existing entries for the same dates
|
||||
// Adds entries to history, replacing any existing entries with the same (date, employeeId)
|
||||
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));
|
||||
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));
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,6 +5,6 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "npx serve . -p 3001",
|
||||
"test": "node tests/algorithm.test.mjs"
|
||||
"test": "node tests/algorithm.test.mjs && node tests/data.test.mjs"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { addHistoryEntries, removeHistoryEntry } from '../js/data.js';
|
||||
|
||||
function makeState(history = []) {
|
||||
return {
|
||||
employees: [],
|
||||
calendar: { holidays: [], companyClosures: [] },
|
||||
history: [...history]
|
||||
};
|
||||
}
|
||||
|
||||
// addHistoryEntries: two entries with same date but different employees both kept
|
||||
{
|
||||
const state = makeState();
|
||||
addHistoryEntries(state, [
|
||||
{ date: '2026-04-01', employeeId: 'alice', manual: false },
|
||||
{ date: '2026-04-01', employeeId: 'bob', manual: false }
|
||||
]);
|
||||
assert.equal(state.history.length, 2, 'two distinct entries kept on same date');
|
||||
const ids = state.history.map(h => h.employeeId).sort();
|
||||
assert.deepEqual(ids, ['alice', 'bob'], 'both employees present');
|
||||
}
|
||||
|
||||
// addHistoryEntries: same (date, employeeId) replaced, others on same date untouched
|
||||
{
|
||||
const state = makeState([
|
||||
{ date: '2026-04-01', employeeId: 'alice', manual: false },
|
||||
{ date: '2026-04-01', employeeId: 'bob', manual: false }
|
||||
]);
|
||||
addHistoryEntries(state, [
|
||||
{ date: '2026-04-01', employeeId: 'alice', manual: true }
|
||||
]);
|
||||
assert.equal(state.history.length, 2, 'still two entries');
|
||||
const alice = state.history.find(h => h.employeeId === 'alice');
|
||||
assert.equal(alice.manual, true, 'alice replaced with manual=true');
|
||||
const bob = state.history.find(h => h.employeeId === 'bob');
|
||||
assert.equal(bob.manual, false, 'bob untouched');
|
||||
}
|
||||
|
||||
// addHistoryEntries: empty input → no change
|
||||
{
|
||||
const state = makeState([{ date: '2026-04-01', employeeId: 'alice', manual: false }]);
|
||||
addHistoryEntries(state, []);
|
||||
assert.equal(state.history.length, 1, 'empty entries → no change');
|
||||
}
|
||||
|
||||
console.log('All data tests passed.');
|
||||
Reference in new issue
Block a user