Add isPermanentlyDeleted field to Note model and update sync logic
Despliegue Automático / desplegar (push) Successful in 43s
Despliegue Automático / desplegar (push) Successful in 43s
This commit is contained in:
@@ -181,6 +181,7 @@ Body ejemplo:
|
|||||||
"serverVersion": 1,
|
"serverVersion": 1,
|
||||||
"position": 2000,
|
"position": 2000,
|
||||||
"isDeleted": true,
|
"isDeleted": true,
|
||||||
|
"isPermanentlyDeleted": false,
|
||||||
"updatedAt": "2026-05-18T10:10:00.000Z"
|
"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.
|
- `serverVersion`: `number` entero >= 0, obligatorio. Es la versión base local con la que se hizo el cambio.
|
||||||
- `position`: `number`, opcional.
|
- `position`: `number`, opcional.
|
||||||
- `isDeleted`: `boolean`, opcional, por defecto `false`.
|
- `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).
|
- `updatedAt`: `ISO date string`, opcional (solo informativo para UI).
|
||||||
|
|
||||||
#### Response
|
#### Response
|
||||||
@@ -260,6 +262,7 @@ Respuesta ejemplo (nuevo contrato):
|
|||||||
"serverVersion": 3,
|
"serverVersion": 3,
|
||||||
"position": 0,
|
"position": 0,
|
||||||
"isDeleted": false,
|
"isDeleted": false,
|
||||||
|
"isPermanentlyDeleted": false,
|
||||||
"updatedAt": "2026-05-18T10:15:00.000Z"
|
"updatedAt": "2026-05-18T10:15:00.000Z"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ const Note = sequelize.define('Note', {
|
|||||||
isDeleted: {
|
isDeleted: {
|
||||||
type: DataTypes.BOOLEAN,
|
type: DataTypes.BOOLEAN,
|
||||||
defaultValue: false
|
defaultValue: false
|
||||||
|
},
|
||||||
|
isPermanentlyDeleted: {
|
||||||
|
type: DataTypes.BOOLEAN,
|
||||||
|
defaultValue: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+29
-16
@@ -26,10 +26,27 @@ const normalizeNote = (note) => ({
|
|||||||
encrypted_body: note.encrypted_body,
|
encrypted_body: note.encrypted_body,
|
||||||
position: note.position,
|
position: note.position,
|
||||||
isDeleted: Boolean(note.isDeleted),
|
isDeleted: Boolean(note.isDeleted),
|
||||||
|
isPermanentlyDeleted: Boolean(note.isPermanentlyDeleted),
|
||||||
serverVersion: note.serverVersion || 1,
|
serverVersion: note.serverVersion || 1,
|
||||||
updatedAt: note.updatedAt instanceof Date ? note.updatedAt.toISOString() : toIso(note.updatedAt)
|
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) => {
|
const getIncomingVersion = (entityName, incoming) => {
|
||||||
if (!Number.isInteger(incoming.serverVersion) || incoming.serverVersion < 0) {
|
if (!Number.isInteger(incoming.serverVersion) || incoming.serverVersion < 0) {
|
||||||
throw new Error(`Cada ${entityName} debe incluir serverVersion como entero >= 0`);
|
throw new Error(`Cada ${entityName} debe incluir serverVersion como entero >= 0`);
|
||||||
@@ -90,7 +107,9 @@ class SyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const incomingNote of incomingNotes) {
|
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');
|
throw new Error('Cada nota debe incluir id, encrypted_title y encrypted_body');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,14 +121,12 @@ class SyncService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!existingNote) {
|
if (!existingNote) {
|
||||||
|
const storedNoteData = buildStoredNoteData(incomingNote);
|
||||||
|
|
||||||
await Note.create({
|
await Note.create({
|
||||||
id: incomingNote.id,
|
id: incomingNote.id,
|
||||||
userId,
|
userId,
|
||||||
categoryId: incomingNote.categoryId || null,
|
...storedNoteData,
|
||||||
encrypted_title: incomingNote.encrypted_title,
|
|
||||||
encrypted_body: incomingNote.encrypted_body,
|
|
||||||
position: incomingNote.position ?? 0,
|
|
||||||
isDeleted: Boolean(incomingNote.isDeleted),
|
|
||||||
serverVersion: 1
|
serverVersion: 1
|
||||||
}, { transaction });
|
}, { transaction });
|
||||||
continue;
|
continue;
|
||||||
@@ -118,24 +135,20 @@ class SyncService {
|
|||||||
const serverVersion = existingNote.serverVersion || 1;
|
const serverVersion = existingNote.serverVersion || 1;
|
||||||
|
|
||||||
if (incomingBaseVersion === serverVersion) {
|
if (incomingBaseVersion === serverVersion) {
|
||||||
|
const storedNoteData = buildStoredNoteData(incomingNote, existingNote);
|
||||||
|
|
||||||
await existingNote.update({
|
await existingNote.update({
|
||||||
categoryId: incomingNote.categoryId || null,
|
...storedNoteData,
|
||||||
encrypted_title: incomingNote.encrypted_title,
|
|
||||||
encrypted_body: incomingNote.encrypted_body,
|
|
||||||
position: incomingNote.position ?? existingNote.position,
|
|
||||||
isDeleted: Boolean(incomingNote.isDeleted),
|
|
||||||
serverVersion: serverVersion + 1
|
serverVersion: serverVersion + 1
|
||||||
}, { transaction });
|
}, { transaction });
|
||||||
} else if (serverVersion > incomingBaseVersion) {
|
} else if (serverVersion > incomingBaseVersion) {
|
||||||
// Server version is newer -> create a new note with incoming content so user sees a duplicate
|
// Server version is newer -> create a new note with incoming content so user sees a duplicate
|
||||||
|
const storedNoteData = buildStoredNoteData(incomingNote);
|
||||||
|
|
||||||
await Note.create({
|
await Note.create({
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
userId,
|
userId,
|
||||||
categoryId: incomingNote.categoryId || null,
|
...storedNoteData,
|
||||||
encrypted_title: incomingNote.encrypted_title,
|
|
||||||
encrypted_body: incomingNote.encrypted_body,
|
|
||||||
position: incomingNote.position ?? 0,
|
|
||||||
isDeleted: Boolean(incomingNote.isDeleted),
|
|
||||||
serverVersion: 1
|
serverVersion: 1
|
||||||
}, { transaction });
|
}, { transaction });
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user