mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 17:56:30 -05:00
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:
parent
8273d8a5b9
commit
b872694158
2 changed files with 59 additions and 1 deletions
|
@ -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;
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue