From 0de97d3a004ffe63cb360ef04e7d291c7f57a538 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 18 Aug 2026 18:10:50 +0200 Subject: [PATCH] Add createdAt and updatedAt handling for categories and notes in sync service --- src/services/syncService.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/services/syncService.js b/src/services/syncService.js index 814a84a..a7e838d 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -19,6 +19,8 @@ const normalizeCategory = (category) => ({ delete: Boolean(category.delete), serverVersion: category.serverVersion || 1, isDirty: false, + // 👇 Añadido createdAt para devolverlo siempre al cliente + createdAt: category.createdAt instanceof Date ? category.createdAt.toISOString() : toIso(category.createdAt), updatedAt: category.updatedAt instanceof Date ? category.updatedAt.toISOString() : toIso(category.updatedAt) }); @@ -31,6 +33,8 @@ const normalizeNote = (note) => ({ delete: Boolean(note.delete), permanentDelete: Boolean(note.permanentDelete), serverVersion: note.serverVersion || 1, + // 👇 Añadido createdAt + createdAt: note.createdAt instanceof Date ? note.createdAt.toISOString() : toIso(note.createdAt), updatedAt: note.updatedAt instanceof Date ? note.updatedAt.toISOString() : toIso(note.updatedAt) }); @@ -40,13 +44,19 @@ const buildStoredNoteData = (incomingNote, existingNote = null) => { ? 0 : (incomingNote.position ?? existingNote?.position ?? 0); - return { + const data = { title: permanentDelete ? '' : incomingNote.title, body: permanentDelete ? '' : incomingNote.body, position, delete: Boolean(incomingNote.delete) || permanentDelete, permanentDelete }; + + // 👇 Extraemos las fechas si el móvil las envía para guardarlas en disco + if (incomingNote.createdAt) data.createdAt = new Date(incomingNote.createdAt); + if (incomingNote.updatedAt) data.updatedAt = new Date(incomingNote.updatedAt); + + return data; }; const getIncomingVersion = (entityName, incoming) => { @@ -72,7 +82,6 @@ class SyncService { const incomingCategories = Array.isArray(changes.categories) ? changes.categories : []; const incomingNotes = Array.isArray(changes.notes) ? changes.notes : []; - // ARRAYS PARA IDs DESFASADOS const conflictedCategoryIds = []; const conflictedNoteIds = []; @@ -91,7 +100,8 @@ class SyncService { }); if (!existingCategory) { - await Category.create({ + // 👇 Preparamos los datos base e insertamos fechas si vienen + const newCategoryData = { id: incomingCategory.id, userId, encrypted_name: incomingCategory.encrypted_name, @@ -99,19 +109,28 @@ class SyncService { iconCodePoint: incomingCategory.iconCodePoint ?? null, delete: Boolean(incomingCategory.delete), serverVersion: 1 - }, { transaction }); + }; + if (incomingCategory.createdAt) newCategoryData.createdAt = new Date(incomingCategory.createdAt); + if (incomingCategory.updatedAt) newCategoryData.updatedAt = new Date(incomingCategory.updatedAt); + + await Category.create(newCategoryData, { transaction }); continue; } const serverVersion = existingCategory.serverVersion || 1; if (incomingBaseVersion === serverVersion) { - await existingCategory.update({ + const updateCategoryData = { encrypted_name: incomingCategory.encrypted_name, colorValue: incomingCategory.colorValue ?? existingCategory.colorValue ?? null, iconCodePoint: incomingCategory.iconCodePoint ?? existingCategory.iconCodePoint ?? null, delete: Boolean(incomingCategory.delete), serverVersion: serverVersion + 1 - }, { transaction }); + }; + // 👇 Actualizamos fechas si se envían + if (incomingCategory.createdAt) updateCategoryData.createdAt = new Date(incomingCategory.createdAt); + if (incomingCategory.updatedAt) updateCategoryData.updatedAt = new Date(incomingCategory.updatedAt); + + await existingCategory.update(updateCategoryData, { transaction }); } else if (serverVersion > incomingBaseVersion) { conflictedCategoryIds.push(existingCategory.id); } @@ -138,7 +157,7 @@ class SyncService { const newNote = await Note.create({ id: incomingNote.id, userId, - ...storedNoteData, + ...storedNoteData, // 👈 Aquí ya van las fechas procesadas en la función helper serverVersion: 1 }, { transaction }); @@ -154,7 +173,7 @@ class SyncService { const storedNoteData = buildStoredNoteData(incomingNote, existingNote); await existingNote.update({ - ...storedNoteData, + ...storedNoteData, // 👈 Se sobrescriben con las del móvil si fueron enviadas serverVersion: serverVersion + 1 }, { transaction });