refactor(history-service): use NoteService to get note

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-04 17:55:38 +01:00
parent 5b7026758a
commit 72c354d5f6
2 changed files with 14 additions and 17 deletions

View file

@ -59,6 +59,10 @@ describe('HistoryService', () => {
provide: getRepositoryToken(HistoryEntry), provide: getRepositoryToken(HistoryEntry),
useClass: Repository, useClass: Repository,
}, },
{
provide: getRepositoryToken(Note),
useClass: Repository,
},
], ],
imports: [ imports: [
LoggerModule, LoggerModule,
@ -362,11 +366,17 @@ describe('HistoryService', () => {
updatedAt: historyEntryImport.lastVisited, updatedAt: historyEntryImport.lastVisited,
}; };
const createQueryBuilder = { const createQueryBuilder = {
innerJoin: () => createQueryBuilder, leftJoinAndSelect: () => createQueryBuilder,
where: () => createQueryBuilder, where: () => createQueryBuilder,
orWhere: () => createQueryBuilder, orWhere: () => createQueryBuilder,
setParameter: () => createQueryBuilder,
getOne: () => note, getOne: () => note,
}; };
jest
.spyOn(noteRepo, 'createQueryBuilder')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementation(() => createQueryBuilder);
const mockedManager = { const mockedManager = {
find: jest.fn().mockResolvedValueOnce([historyEntry]), find: jest.fn().mockResolvedValueOnce([historyEntry]),
createQueryBuilder: () => createQueryBuilder, createQueryBuilder: () => createQueryBuilder,

View file

@ -157,22 +157,9 @@ export class HistoryService {
await manager.remove<HistoryEntry>(entry); await manager.remove<HistoryEntry>(entry);
} }
for (const historyEntry of history) { for (const historyEntry of history) {
this.notesService.checkNoteIdOrAlias(historyEntry.note); const note = await this.notesService.getNoteByIdOrAlias(
const note = await manager historyEntry.note,
.createQueryBuilder<Note>(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 entry = HistoryEntry.create(user, note) as HistoryEntry; const entry = HistoryEntry.create(user, note) as HistoryEntry;
entry.pinStatus = historyEntry.pinStatus; entry.pinStatus = historyEntry.pinStatus;
entry.updatedAt = historyEntry.lastVisited; entry.updatedAt = historyEntry.lastVisited;