From d2b60a316f15c2480d665b141a46e8609e04c8de Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 29 Aug 2021 21:42:46 +0200 Subject: [PATCH] HistoryService: Refactor `deleteHistoryEntry` The function now expects a `Note` object instead of a noteId to make it more consistent with other functions. Signed-off-by: David Mehren --- src/api/private/me/history/history.controller.ts | 6 ++++-- src/api/public/me/me.controller.ts | 2 +- src/history/history.service.spec.ts | 4 ++-- src/history/history.service.ts | 6 +++--- test/private-api/history.e2e-spec.ts | 2 +- test/public-api/me.e2e-spec.ts | 6 ++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/api/private/me/history/history.controller.ts b/src/api/private/me/history/history.controller.ts index dd37ad784..fdb098b4c 100644 --- a/src/api/private/me/history/history.controller.ts +++ b/src/api/private/me/history/history.controller.ts @@ -107,11 +107,13 @@ export class HistoryController { } @Delete(':note') - async deleteHistoryEntry(@Param('note') noteId: string): Promise { + async deleteHistoryEntry( + @Param('note', GetNotePipe) note: Note, + ): Promise { try { // ToDo: use actual user here const user = await this.userService.getUserByUsername('hardcoded'); - await this.historyService.deleteHistoryEntry(noteId, user); + await this.historyService.deleteHistoryEntry(note, user); } catch (e) { if (e instanceof NotInDBError) { throw new NotFoundException(e.message); diff --git a/src/api/public/me/me.controller.ts b/src/api/public/me/me.controller.ts index 718d26df7..26ae88fea 100644 --- a/src/api/public/me/me.controller.ts +++ b/src/api/public/me/me.controller.ts @@ -149,7 +149,7 @@ export class MeController { @ApiNotFoundResponse({ description: notFoundDescription }) async deleteHistoryEntry( @RequestUser() user: User, - @Param('note') note: string, + @Param('note', GetNotePipe) note: Note, ): Promise { // ToDo: Check if user is allowed to delete note try { diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index d66456946..1aecaaec5 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -311,7 +311,7 @@ describe('HistoryService', () => { return entry; }, ); - await service.deleteHistoryEntry(alias, user); + await service.deleteHistoryEntry(note, user); }); }); describe('fails', () => { @@ -321,7 +321,7 @@ describe('HistoryService', () => { const note = Note.create(user, alias); jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined); jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note); - await expect(service.deleteHistoryEntry(alias, user)).rejects.toThrow( + await expect(service.deleteHistoryEntry(note, user)).rejects.toThrow( NotInDBError, ); }); diff --git a/src/history/history.service.ts b/src/history/history.service.ts index 5c1794c14..1ae9debd6 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -130,12 +130,12 @@ export class HistoryService { /** * @async * Delete the history entry identified by the user and a note id or alias - * @param {string} noteIdOrAlias - the note that the history entry belongs to + * @param {Note} note - the note that the history entry belongs to * @param {User} user - the user that the history entry belongs to * @throws {NotInDBError} the specified history entry does not exist */ - async deleteHistoryEntry(noteIdOrAlias: string, user: User): Promise { - const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user); + async deleteHistoryEntry(note: Note, user: User): Promise { + const entry = await this.getEntryByNote(note, user); await this.historyEntryRepository.remove(entry); return; } diff --git a/test/private-api/history.e2e-spec.ts b/test/private-api/history.e2e-spec.ts index d098c7bd1..1aae79ffe 100644 --- a/test/private-api/history.e2e-spec.ts +++ b/test/private-api/history.e2e-spec.ts @@ -191,7 +191,7 @@ describe('History', () => { const userEntries = await historyService.getEntriesByUser(user); expect(userEntries.length).toEqual(1); expect(userEntries[0].pinStatus).toBeTruthy(); - await historyService.deleteHistoryEntry(note2.alias, user); + await historyService.deleteHistoryEntry(note2, user); }); it('DELETE /me/history/:note', async () => { diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index 17edd7d6e..341ae6b16 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -96,10 +96,8 @@ describe('Me', () => { it(`GET /me/history`, async () => { const noteName = 'testGetNoteHistory1'; const note = await notesService.createNote('', noteName); - const createdHistoryEntry = await historyService.updateHistoryEntryTimestamp( - note, - user, - ); + const createdHistoryEntry = + await historyService.updateHistoryEntryTimestamp(note, user); const response = await request(app.getHttpServer()) .get('/me/history') .expect('Content-Type', /json/)