Relevo
Cómo se identifica a quién avisar tras cambiar un turno
Se comparan el cuadrante comunicado y el nuevo por identificador de turno. El resultado recoge los cambios y las personas afectadas.
function compareEntries(before,assignments) {
const current=new Map(assignments.map(row=>[row.id,publishedRow(row)]));
const currentNames=new Map(assignments.filter(r=>r.personId!==null).map(r=>[r.personId,r.personName]));
const changed=[],affected=new Map();
let kept=0;
for (const id of new Set([...before.keys(),...current.keys()])) {
const old=before.get(id)??null, next=current.get(id)??null;
let status;
if (!old) status='added';
else if (!next) status='removed';
else if (!sameSlot(old,next)) status='rescheduled';
else if (old.personId!==next.personId) status=old.personId===null?'filled':next.personId===null?'vacated':'reassigned';
else {
if (old.personId!==null) kept++;
if (old.priority===next.priority && old.fixed===next.fixed && old.personName===next.personName) continue;
status='updated';
}
changed.push({id,status,before:old,after:next});
for (const entry of [old,next]) if (entry?.personId!=null) {
const person=affected.get(entry.personId)??{id:entry.personId,name:currentNames.get(entry.personId)??entry.personName,shifts:[]};
if (!person.shifts.includes(id)) person.shifts.push(id);
affected.set(entry.personId,person);
}
}
return {previouslyAssigned:[...before.values()].filter(row=>row.personId!==null).length,kept,changed,people:[...affected.values()]};
}