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),
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,

View file

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