diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 896938695..f139a86a3 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -59,6 +59,10 @@ describe('HistoryService', () => { provide: getRepositoryToken(HistoryEntry), useClass: Repository, }, + { + provide: getRepositoryToken(Note), + useClass: Repository, + }, ], imports: [ LoggerModule, @@ -362,11 +366,17 @@ describe('HistoryService', () => { updatedAt: historyEntryImport.lastVisited, }; const createQueryBuilder = { - innerJoin: () => createQueryBuilder, + leftJoinAndSelect: () => createQueryBuilder, where: () => createQueryBuilder, orWhere: () => createQueryBuilder, + setParameter: () => createQueryBuilder, getOne: () => note, }; + jest + .spyOn(noteRepo, 'createQueryBuilder') + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + .mockImplementation(() => createQueryBuilder); const mockedManager = { find: jest.fn().mockResolvedValueOnce([historyEntry]), createQueryBuilder: () => createQueryBuilder, diff --git a/src/history/history.service.ts b/src/history/history.service.ts index 3cdea5269..3578a88e1 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -157,22 +157,9 @@ export class HistoryService { await manager.remove(entry); } for (const historyEntry of history) { - this.notesService.checkNoteIdOrAlias(historyEntry.note); - const note = await manager - .createQueryBuilder(Note, 'note') - .innerJoin('note.aliases', 'alias') - .where('note.id = :id', { id: historyEntry.note }) - .orWhere('alias.name = :id', { id: historyEntry.note }) - .getOne(); - if (note === undefined) { - this.logger.debug( - `Could not find note '${historyEntry.note}'`, - 'setHistory', - ); - throw new NotInDBError( - `Note with id/alias '${historyEntry.note}' not found.`, - ); - } + const note = await this.notesService.getNoteByIdOrAlias( + historyEntry.note, + ); const entry = HistoryEntry.create(user, note) as HistoryEntry; entry.pinStatus = historyEntry.pinStatus; entry.updatedAt = historyEntry.lastVisited;