diff --git a/services/targo-hub/lib/roster.js b/services/targo-hub/lib/roster.js index 0c4d384..edf7c8f 100644 --- a/services/targo-hub/lib/roster.js +++ b/services/targo-hub/lib/roster.js @@ -677,10 +677,18 @@ async function handle (req, res, method, path, url) { if (path === '/roster/publish-week' && method === 'POST') { const b = await parseBody(req) const existing = await fetchAssignments(b.start, b.days || 7) - let deleted = 0 - for (const a of existing) { const r = await retryWrite(() => erp.remove('Shift Assignment', a.name)); if (r.ok) deleted++ } - let created = 0; let errors = 0 - for (const a of (b.assignments || [])) { + const desired = b.assignments || [] + // DIFF par clé tech|date|shift : on ne touche QUE ce qui a changé (≫ rapide vs wipe+recreate complet). + const keyOf = (a) => a.tech + '|' + a.date + '|' + a.shift + const existByKey = {}; for (const a of existing) existByKey[keyOf(a)] = a + const desiredKeys = new Set(desired.map(keyOf)) + let deleted = 0; let created = 0; let errors = 0; let unchanged = 0 + for (const a of existing) { // supprimer ceux qui ne sont plus voulus + if (desiredKeys.has(keyOf(a))) continue + const r = await retryWrite(() => erp.remove('Shift Assignment', a.name)); if (r.ok) deleted++ + } + for (const a of desired) { // créer seulement les nouveaux ; ignorer les inchangés + if (existByKey[keyOf(a)]) { unchanged++; continue } const r = await retryWrite(() => erp.create('Shift Assignment', { technician: a.tech, technician_name: a.tech_name || '', assignment_date: a.date, shift_template: a.shift, zone: a.zone || '', hours: Number(a.hours) || 0, @@ -704,7 +712,7 @@ async function handle (req, res, method, path, url) { } } catch (e) { /* notif non bloquante */ } } - return json(res, 200, { ok: errors === 0, created, deleted, errors, notified }) + return json(res, 200, { ok: errors === 0, created, deleted, errors, notified, unchanged }) } // Modifier / supprimer un type de shift (Shift Template) const mTpl = path.match(/^\/roster\/template\/(.+)$/)