fix(core): exclude trashed docs from journal lookups (#15461)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Trashed journals are now excluded from journal date lists and date-based searches. * Journal dates automatically reappear when a journal is restored from the trash. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
import { Framework, LiveData } from '@toeverything/infra';
|
||||||
|
import { describe, expect, test } from 'vitest';
|
||||||
|
|
||||||
|
import type { DocsService } from '../../doc';
|
||||||
|
import { JournalStore } from '../store/journal';
|
||||||
|
|
||||||
|
function createDoc(id: string, journal: string, trash = false) {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
properties$: new LiveData({ journal }),
|
||||||
|
trash$: new LiveData(trash),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createStore(docs: ReturnType<typeof createDoc>[]) {
|
||||||
|
const docsService = {
|
||||||
|
list: {
|
||||||
|
docs$: new LiveData(docs),
|
||||||
|
},
|
||||||
|
} as unknown as DocsService;
|
||||||
|
|
||||||
|
const framework = new Framework();
|
||||||
|
framework.store(JournalStore, () => new JournalStore(docsService));
|
||||||
|
return framework.provider().get(JournalStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('JournalStore', () => {
|
||||||
|
const date = '2026-08-10';
|
||||||
|
|
||||||
|
test('docsByJournalDate$ skips trashed journals', () => {
|
||||||
|
const store = createStore([
|
||||||
|
createDoc('trashed', date, true),
|
||||||
|
createDoc('kept', date),
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(store.docsByJournalDate$(date).value.map(doc => doc.id)).toEqual([
|
||||||
|
'kept',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('docsByJournalDate$ is empty when the only journal is trashed', () => {
|
||||||
|
const store = createStore([createDoc('trashed', date, true)]);
|
||||||
|
|
||||||
|
expect(store.docsByJournalDate$(date).value).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getDocsByJournalDate skips trashed journals', () => {
|
||||||
|
const store = createStore([
|
||||||
|
createDoc('trashed', date, true),
|
||||||
|
createDoc('kept', date),
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(store.getDocsByJournalDate(date).map(doc => doc.id)).toEqual([
|
||||||
|
'kept',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allJournalDates$ skips trashed journals', () => {
|
||||||
|
const store = createStore([
|
||||||
|
createDoc('trashed', date, true),
|
||||||
|
createDoc('kept', '2026-08-09'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(store.allJournalDates$.value).toEqual(new Set(['2026-08-09']));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('restoring a journal claims its date again', () => {
|
||||||
|
const doc = createDoc('journal', date, true);
|
||||||
|
const store = createStore([doc]);
|
||||||
|
|
||||||
|
expect(store.docsByJournalDate$(date).value).toEqual([]);
|
||||||
|
|
||||||
|
doc.trash$.next(false);
|
||||||
|
|
||||||
|
expect(store.docsByJournalDate$(date).value.map(d => d.id)).toEqual([
|
||||||
|
'journal',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -16,6 +16,9 @@ export class JournalStore extends Store {
|
|||||||
return new Set(
|
return new Set(
|
||||||
get(this.docsService.list.docs$)
|
get(this.docsService.list.docs$)
|
||||||
.filter(doc => {
|
.filter(doc => {
|
||||||
|
if (get(doc.trash$)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const journal = get(doc.properties$.selector(p => p.journal));
|
const journal = get(doc.properties$.selector(p => p.journal));
|
||||||
return !!journal && isJournalString(journal);
|
return !!journal && isJournalString(journal);
|
||||||
})
|
})
|
||||||
@@ -53,12 +56,15 @@ export class JournalStore extends Store {
|
|||||||
|
|
||||||
getDocsByJournalDate(date: string) {
|
getDocsByJournalDate(date: string) {
|
||||||
return this.docsService.list.docs$.value.filter(
|
return this.docsService.list.docs$.value.filter(
|
||||||
doc => doc.properties$.value.journal === date
|
doc => !doc.trash$.value && doc.properties$.value.journal === date
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
docsByJournalDate$(date: string) {
|
docsByJournalDate$(date: string) {
|
||||||
return LiveData.computed(get => {
|
return LiveData.computed(get => {
|
||||||
return get(this.docsService.list.docs$).filter(doc => {
|
return get(this.docsService.list.docs$).filter(doc => {
|
||||||
|
if (get(doc.trash$)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const journal = get(doc.properties$.selector(p => p.journal));
|
const journal = get(doc.properties$.selector(p => p.journal));
|
||||||
return journal === date;
|
return journal === date;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user