Add createdAt and updatedAt handling for categories and notes in sync service
Despliegue Automático / desplegar (push) Successful in 1m1s

This commit is contained in:
2026-08-18 18:10:50 +02:00
parent f7864c6f50
commit 0de97d3a00
+27 -8
View File
@@ -19,6 +19,8 @@ const normalizeCategory = (category) => ({
delete: Boolean(category.delete), delete: Boolean(category.delete),
serverVersion: category.serverVersion || 1, serverVersion: category.serverVersion || 1,
isDirty: false, 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) updatedAt: category.updatedAt instanceof Date ? category.updatedAt.toISOString() : toIso(category.updatedAt)
}); });
@@ -31,6 +33,8 @@ const normalizeNote = (note) => ({
delete: Boolean(note.delete), delete: Boolean(note.delete),
permanentDelete: Boolean(note.permanentDelete), permanentDelete: Boolean(note.permanentDelete),
serverVersion: note.serverVersion || 1, 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) updatedAt: note.updatedAt instanceof Date ? note.updatedAt.toISOString() : toIso(note.updatedAt)
}); });
@@ -40,13 +44,19 @@ const buildStoredNoteData = (incomingNote, existingNote = null) => {
? 0 ? 0
: (incomingNote.position ?? existingNote?.position ?? 0); : (incomingNote.position ?? existingNote?.position ?? 0);
return { const data = {
title: permanentDelete ? '' : incomingNote.title, title: permanentDelete ? '' : incomingNote.title,
body: permanentDelete ? '' : incomingNote.body, body: permanentDelete ? '' : incomingNote.body,
position, position,
delete: Boolean(incomingNote.delete) || permanentDelete, delete: Boolean(incomingNote.delete) || permanentDelete,
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) => { const getIncomingVersion = (entityName, incoming) => {
@@ -72,7 +82,6 @@ class SyncService {
const incomingCategories = Array.isArray(changes.categories) ? changes.categories : []; const incomingCategories = Array.isArray(changes.categories) ? changes.categories : [];
const incomingNotes = Array.isArray(changes.notes) ? changes.notes : []; const incomingNotes = Array.isArray(changes.notes) ? changes.notes : [];
// ARRAYS PARA IDs DESFASADOS
const conflictedCategoryIds = []; const conflictedCategoryIds = [];
const conflictedNoteIds = []; const conflictedNoteIds = [];
@@ -91,7 +100,8 @@ class SyncService {
}); });
if (!existingCategory) { if (!existingCategory) {
await Category.create({ // 👇 Preparamos los datos base e insertamos fechas si vienen
const newCategoryData = {
id: incomingCategory.id, id: incomingCategory.id,
userId, userId,
encrypted_name: incomingCategory.encrypted_name, encrypted_name: incomingCategory.encrypted_name,
@@ -99,19 +109,28 @@ class SyncService {
iconCodePoint: incomingCategory.iconCodePoint ?? null, iconCodePoint: incomingCategory.iconCodePoint ?? null,
delete: Boolean(incomingCategory.delete), delete: Boolean(incomingCategory.delete),
serverVersion: 1 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; continue;
} }
const serverVersion = existingCategory.serverVersion || 1; const serverVersion = existingCategory.serverVersion || 1;
if (incomingBaseVersion === serverVersion) { if (incomingBaseVersion === serverVersion) {
await existingCategory.update({ const updateCategoryData = {
encrypted_name: incomingCategory.encrypted_name, encrypted_name: incomingCategory.encrypted_name,
colorValue: incomingCategory.colorValue ?? existingCategory.colorValue ?? null, colorValue: incomingCategory.colorValue ?? existingCategory.colorValue ?? null,
iconCodePoint: incomingCategory.iconCodePoint ?? existingCategory.iconCodePoint ?? null, iconCodePoint: incomingCategory.iconCodePoint ?? existingCategory.iconCodePoint ?? null,
delete: Boolean(incomingCategory.delete), delete: Boolean(incomingCategory.delete),
serverVersion: serverVersion + 1 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) { } else if (serverVersion > incomingBaseVersion) {
conflictedCategoryIds.push(existingCategory.id); conflictedCategoryIds.push(existingCategory.id);
} }
@@ -138,7 +157,7 @@ class SyncService {
const newNote = await Note.create({ const newNote = await Note.create({
id: incomingNote.id, id: incomingNote.id,
userId, userId,
...storedNoteData, ...storedNoteData, // 👈 Aquí ya van las fechas procesadas en la función helper
serverVersion: 1 serverVersion: 1
}, { transaction }); }, { transaction });
@@ -154,7 +173,7 @@ class SyncService {
const storedNoteData = buildStoredNoteData(incomingNote, existingNote); const storedNoteData = buildStoredNoteData(incomingNote, existingNote);
await existingNote.update({ await existingNote.update({
...storedNoteData, ...storedNoteData, // 👈 Se sobrescriben con las del móvil si fueron enviadas
serverVersion: serverVersion + 1 serverVersion: serverVersion + 1
}, { transaction }); }, { transaction });