From 73a8932c64cd761ec6de1426b18aa81d098caf92 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 19 May 2026 11:08:30 +0200 Subject: [PATCH] Add isPermanentlyDeleted field to Note model and update sync logic --- docs/API.md | 3 +++ src/models/Note.js | 4 ++++ src/services/syncService.js | 45 ++++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/docs/API.md b/docs/API.md index b80d8a1..f8aef51 100644 --- a/docs/API.md +++ b/docs/API.md @@ -181,6 +181,7 @@ Body ejemplo: "serverVersion": 1, "position": 2000, "isDeleted": true, + "isPermanentlyDeleted": false, "updatedAt": "2026-05-18T10:10:00.000Z" } ] @@ -239,6 +240,7 @@ Campos: - `serverVersion`: `number` entero >= 0, obligatorio. Es la versión base local con la que se hizo el cambio. - `position`: `number`, opcional. - `isDeleted`: `boolean`, opcional, por defecto `false`. +- `isPermanentlyDeleted`: `boolean`, opcional, por defecto `false`. Si llega en `true`, el servidor guarda la nota con `encrypted_title` y `encrypted_body` vacíos, `position` en `0` y `isDeleted` en `true`. - `updatedAt`: `ISO date string`, opcional (solo informativo para UI). #### Response @@ -260,6 +262,7 @@ Respuesta ejemplo (nuevo contrato): "serverVersion": 3, "position": 0, "isDeleted": false, + "isPermanentlyDeleted": false, "updatedAt": "2026-05-18T10:15:00.000Z" } ] diff --git a/src/models/Note.js b/src/models/Note.js index d40201d..5bbb058 100644 --- a/src/models/Note.js +++ b/src/models/Note.js @@ -27,6 +27,10 @@ const Note = sequelize.define('Note', { isDeleted: { type: DataTypes.BOOLEAN, defaultValue: false + }, + isPermanentlyDeleted: { + type: DataTypes.BOOLEAN, + defaultValue: false } }); diff --git a/src/services/syncService.js b/src/services/syncService.js index 368bef0..0c9872e 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -26,10 +26,27 @@ const normalizeNote = (note) => ({ encrypted_body: note.encrypted_body, position: note.position, isDeleted: Boolean(note.isDeleted), + isPermanentlyDeleted: Boolean(note.isPermanentlyDeleted), serverVersion: note.serverVersion || 1, updatedAt: note.updatedAt instanceof Date ? note.updatedAt.toISOString() : toIso(note.updatedAt) }); +const buildStoredNoteData = (incomingNote, existingNote = null) => { + const isPermanentlyDeleted = Boolean(incomingNote.isPermanentlyDeleted); + const position = isPermanentlyDeleted + ? 0 + : (incomingNote.position ?? existingNote?.position ?? 0); + + return { + categoryId: incomingNote.categoryId || null, + encrypted_title: isPermanentlyDeleted ? '' : incomingNote.encrypted_title, + encrypted_body: isPermanentlyDeleted ? '' : incomingNote.encrypted_body, + position, + isDeleted: Boolean(incomingNote.isDeleted) || isPermanentlyDeleted, + isPermanentlyDeleted + }; +}; + const getIncomingVersion = (entityName, incoming) => { if (!Number.isInteger(incoming.serverVersion) || incoming.serverVersion < 0) { throw new Error(`Cada ${entityName} debe incluir serverVersion como entero >= 0`); @@ -90,7 +107,9 @@ class SyncService { } for (const incomingNote of incomingNotes) { - if (!incomingNote.id || !incomingNote.encrypted_title || !incomingNote.encrypted_body) { + const isPermanentlyDeleted = Boolean(incomingNote.isPermanentlyDeleted); + + if (!incomingNote.id || (!isPermanentlyDeleted && (!incomingNote.encrypted_title || !incomingNote.encrypted_body))) { throw new Error('Cada nota debe incluir id, encrypted_title y encrypted_body'); } @@ -102,14 +121,12 @@ class SyncService { }); if (!existingNote) { + const storedNoteData = buildStoredNoteData(incomingNote); + await Note.create({ id: incomingNote.id, userId, - categoryId: incomingNote.categoryId || null, - encrypted_title: incomingNote.encrypted_title, - encrypted_body: incomingNote.encrypted_body, - position: incomingNote.position ?? 0, - isDeleted: Boolean(incomingNote.isDeleted), + ...storedNoteData, serverVersion: 1 }, { transaction }); continue; @@ -118,24 +135,20 @@ class SyncService { const serverVersion = existingNote.serverVersion || 1; if (incomingBaseVersion === serverVersion) { + const storedNoteData = buildStoredNoteData(incomingNote, existingNote); + await existingNote.update({ - categoryId: incomingNote.categoryId || null, - encrypted_title: incomingNote.encrypted_title, - encrypted_body: incomingNote.encrypted_body, - position: incomingNote.position ?? existingNote.position, - isDeleted: Boolean(incomingNote.isDeleted), + ...storedNoteData, serverVersion: serverVersion + 1 }, { transaction }); } else if (serverVersion > incomingBaseVersion) { // Server version is newer -> create a new note with incoming content so user sees a duplicate + const storedNoteData = buildStoredNoteData(incomingNote); + await Note.create({ id: randomUUID(), userId, - categoryId: incomingNote.categoryId || null, - encrypted_title: incomingNote.encrypted_title, - encrypted_body: incomingNote.encrypted_body, - position: incomingNote.position ?? 0, - isDeleted: Boolean(incomingNote.isDeleted), + ...storedNoteData, serverVersion: 1 }, { transaction }); } else {