From 4d827afccba05a5c42bdc33719348bf45847c955 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:33:31 -0800 Subject: [PATCH] Desktop: Fixes #11226: Fix reordering notes in custom sort order when some notes are deleted (#11592) --- packages/lib/models/Note.ts | 1 + .../lib/models/Note_CustomSortOrder.test.js | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/lib/models/Note.ts b/packages/lib/models/Note.ts index a09ae23f1..9160c52fd 100644 --- a/packages/lib/models/Note.ts +++ b/packages/lib/models/Note.ts @@ -1030,6 +1030,7 @@ export default class Note extends BaseItem { FROM notes WHERE is_conflict = 0 + AND deleted_time = 0 ${showCompletedTodos ? '' : 'AND todo_completed = 0'} AND parent_id = ? `; diff --git a/packages/lib/models/Note_CustomSortOrder.test.js b/packages/lib/models/Note_CustomSortOrder.test.js index 26b18da4c..4e2c3b64c 100644 --- a/packages/lib/models/Note_CustomSortOrder.test.js +++ b/packages/lib/models/Note_CustomSortOrder.test.js @@ -282,4 +282,34 @@ describe('models/Note_CustomSortOrder', () => { expect(resortedNotes3[5].id).toBe(resortedNotes2[5].id); })); + it('should account for items in the trash', async () => { + const folder1 = await Folder.save({}); + + const notes = []; + notes.push(await Note.save({ order: 1003, parent_id: folder1.id, deleted_time: 1 })); await time.msleep(2); + notes.push(await Note.save({ order: 1002, parent_id: folder1.id })); await time.msleep(2); + notes.push(await Note.save({ order: 1001, parent_id: folder1.id })); await time.msleep(2); + notes.push(await Note.save({ order: 1000, parent_id: folder1.id })); await time.msleep(2); + + const sortedNoteIds = async () => { + const notes = await Note.previews(folder1.id, { + fields: ['id', 'order', 'user_created_time', 'is_todo', 'todo_completed'], + order: Note.customOrderByColumns(), + }); + return notes.map(note => note.id); + }; + + // Should sort items correctly initially, with deleted items omitted + expect(await sortedNoteIds()).toEqual([ + notes[1].id, notes[2].id, notes[3].id, + ]); + + // Move a note to the end + await Note.insertNotesAt(folder1.id, [notes[1].id], 3, true, true); + + // Should correctly reorder notes + expect(await sortedNoteIds()).toEqual([ + notes[2].id, notes[3].id, notes[1].id, + ]); + }); });