HistoryService: Add deleteHistory method

This method deletes all history entries of a user.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-03-03 15:23:45 +01:00 committed by David Mehren
parent 8273d8a5b9
commit b872694158
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 59 additions and 1 deletions

View file

@ -241,6 +241,52 @@ describe('HistoryService', () => {
}); });
describe('deleteHistoryEntry', () => { 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<HistoryEntry> => {
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<HistoryEntry> => {
expect(entry).toEqual(historyEntry);
return entry;
},
)
.mockImplementationOnce(
async (entry: HistoryEntry): Promise<HistoryEntry> => {
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', () => { describe('works', () => {
it('with an entry', async () => { it('with an entry', async () => {
const user = {} as User; const user = {} as User;

View file

@ -38,7 +38,7 @@ export class HistoryService {
async getEntriesByUser(user: User): Promise<HistoryEntry[]> { async getEntriesByUser(user: User): Promise<HistoryEntry[]> {
return await this.historyEntryRepository.find({ return await this.historyEntryRepository.find({
where: { user: user }, where: { user: user },
relations: ['note'], relations: ['note', 'user'],
}); });
} }
@ -136,6 +136,18 @@ export class HistoryService {
return; 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<void> {
const entries: HistoryEntry[] = await this.getEntriesByUser(user);
for (const entry of entries) {
await this.historyEntryRepository.remove(entry);
}
}
/** /**
* Build HistoryEntryDto from a history entry. * Build HistoryEntryDto from a history entry.
* @param {HistoryEntry} entry - the history entry to use * @param {HistoryEntry} entry - the history entry to use