mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
chore: create getIdentifier utility function
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
8c214820e1
commit
babd5ce393
3 changed files with 65 additions and 14 deletions
|
@ -17,6 +17,7 @@ import { HistoryEntryImportDto } from './history-entry-import.dto';
|
|||
import { HistoryEntryUpdateDto } from './history-entry-update.dto';
|
||||
import { HistoryEntryDto } from './history-entry.dto';
|
||||
import { HistoryEntry } from './history-entry.entity';
|
||||
import { getIdentifier } from './utils';
|
||||
|
||||
@Injectable()
|
||||
export class HistoryService {
|
||||
|
@ -41,7 +42,7 @@ export class HistoryService {
|
|||
async getEntriesByUser(user: User): Promise<HistoryEntry[]> {
|
||||
return await this.historyEntryRepository.find({
|
||||
where: { user: user },
|
||||
relations: ['note', 'user'],
|
||||
relations: ['note', 'note.aliases', 'user'],
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -58,7 +59,7 @@ export class HistoryService {
|
|||
note: note,
|
||||
user: user,
|
||||
},
|
||||
relations: ['note', 'user'],
|
||||
relations: ['note', 'note.aliases', 'user'],
|
||||
});
|
||||
if (!entry) {
|
||||
throw new NotInDBError(
|
||||
|
@ -150,23 +151,19 @@ export class HistoryService {
|
|||
await this.connection.transaction(async (manager) => {
|
||||
const currentHistory = await manager.find<HistoryEntry>(HistoryEntry, {
|
||||
where: { user: user },
|
||||
relations: ['note', 'user'],
|
||||
relations: ['note', 'note.aliases', 'user'],
|
||||
});
|
||||
for (const entry of currentHistory) {
|
||||
await manager.remove<HistoryEntry>(entry);
|
||||
}
|
||||
for (const historyEntry of history) {
|
||||
this.notesService.checkNoteIdOrAlias(historyEntry.note);
|
||||
const note = await manager.findOne<Note>(Note, {
|
||||
where: [
|
||||
{
|
||||
id: historyEntry.note,
|
||||
},
|
||||
{
|
||||
alias: 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}'`,
|
||||
|
@ -191,7 +188,7 @@ export class HistoryService {
|
|||
*/
|
||||
toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto {
|
||||
return {
|
||||
identifier: entry.note.alias ? entry.note.alias : entry.note.id,
|
||||
identifier: getIdentifier(entry),
|
||||
lastVisited: entry.updatedAt,
|
||||
tags: this.notesService.toTagList(entry.note),
|
||||
title: entry.note.title ?? '',
|
||||
|
|
36
src/history/utils.spec.ts
Normal file
36
src/history/utils.spec.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { Alias } from '../notes/alias.entity';
|
||||
import { Note } from '../notes/note.entity';
|
||||
import { User } from '../users/user.entity';
|
||||
import { HistoryEntry } from './history-entry.entity';
|
||||
import { getIdentifier } from './utils';
|
||||
|
||||
describe('getIdentifier', () => {
|
||||
const alias = 'alias';
|
||||
let note: Note;
|
||||
let entry: HistoryEntry;
|
||||
beforeEach(() => {
|
||||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
note = Note.create(user, alias);
|
||||
entry = HistoryEntry.create(user, note);
|
||||
});
|
||||
it('returns the publicId if there are no aliases', () => {
|
||||
note.aliases = undefined as unknown as Alias[];
|
||||
expect(getIdentifier(entry)).toEqual(note.publicId);
|
||||
});
|
||||
it('returns the publicId, if the alias array is empty', () => {
|
||||
note.aliases = [];
|
||||
expect(getIdentifier(entry)).toEqual(note.publicId);
|
||||
});
|
||||
it('returns the publicId, if the only alias is not primary', () => {
|
||||
note.aliases[0].primary = false;
|
||||
expect(getIdentifier(entry)).toEqual(note.publicId);
|
||||
});
|
||||
it('returns the primary alias, if one exists', () => {
|
||||
expect(getIdentifier(entry)).toEqual(note.aliases[0].name);
|
||||
});
|
||||
});
|
18
src/history/utils.ts
Normal file
18
src/history/utils.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { getPrimaryAlias } from '../notes/utils';
|
||||
import { HistoryEntry } from './history-entry.entity';
|
||||
|
||||
export function getIdentifier(entry: HistoryEntry): string {
|
||||
if (!entry.note.aliases || entry.note.aliases.length === 0) {
|
||||
return entry.note.publicId;
|
||||
}
|
||||
const primaryAlias = getPrimaryAlias(entry.note);
|
||||
if (primaryAlias === undefined) {
|
||||
return entry.note.publicId;
|
||||
}
|
||||
return primaryAlias;
|
||||
}
|
Loading…
Reference in a new issue