diff --git a/src/history/history.service.spec.ts b/src/history/history.service.spec.ts index 97d129c02..46a984800 100644 --- a/src/history/history.service.spec.ts +++ b/src/history/history.service.spec.ts @@ -241,6 +241,52 @@ describe('HistoryService', () => { }); describe('deleteHistoryEntry', () => { + describe('works', () => { + const user = {} as User; + const alias = 'alias'; + const note = Note.create(user, alias); + const historyEntry = HistoryEntry.create(user, note); + it('with an entry', async () => { + jest.spyOn(historyRepo, 'find').mockResolvedValueOnce([historyEntry]); + jest.spyOn(historyRepo, 'remove').mockImplementationOnce( + async (entry: HistoryEntry): Promise => { + expect(entry).toEqual(historyEntry); + return entry; + }, + ); + await service.deleteHistory(user); + }); + it('with multiple entries', async () => { + const alias2 = 'alias2'; + const note2 = Note.create(user, alias2); + const historyEntry2 = HistoryEntry.create(user, note2); + jest + .spyOn(historyRepo, 'find') + .mockResolvedValueOnce([historyEntry, historyEntry2]); + jest + .spyOn(historyRepo, 'remove') + .mockImplementationOnce( + async (entry: HistoryEntry): Promise => { + expect(entry).toEqual(historyEntry); + return entry; + }, + ) + .mockImplementationOnce( + async (entry: HistoryEntry): Promise => { + expect(entry).toEqual(historyEntry2); + return entry; + }, + ); + await service.deleteHistory(user); + }); + it('without an entry', async () => { + jest.spyOn(historyRepo, 'find').mockResolvedValueOnce([]); + await service.deleteHistory(user); + }); + }); + }); + + describe('deleteHistory', () => { describe('works', () => { it('with an entry', async () => { const user = {} as User; diff --git a/src/history/history.service.ts b/src/history/history.service.ts index 04f70eadc..d2793ac04 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -38,7 +38,7 @@ export class HistoryService { async getEntriesByUser(user: User): Promise { return await this.historyEntryRepository.find({ where: { user: user }, - relations: ['note'], + relations: ['note', 'user'], }); } @@ -136,6 +136,18 @@ export class HistoryService { return; } + /** + * @async + * Delete all history entries of a specific user + * @param {User} user - the user that the entry belongs to + */ + async deleteHistory(user: User): Promise { + const entries: HistoryEntry[] = await this.getEntriesByUser(user); + for (const entry of entries) { + await this.historyEntryRepository.remove(entry); + } + } + /** * Build HistoryEntryDto from a history entry. * @param {HistoryEntry} entry - the history entry to use