Add isPermanentlyDeleted field to Note model and update sync logic
Despliegue Automático / desplegar (push) Successful in 43s

This commit is contained in:
2026-05-19 11:08:30 +02:00
parent fa78a7e9d1
commit 73a8932c64
3 changed files with 36 additions and 16 deletions
+4
View File
@@ -27,6 +27,10 @@ const Note = sequelize.define('Note', {
isDeleted: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
isPermanentlyDeleted: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
});
+29 -16
View File
@@ -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 {