mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-29 04:34:33 -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 { HistoryEntryUpdateDto } from './history-entry-update.dto';
|
||||||
import { HistoryEntryDto } from './history-entry.dto';
|
import { HistoryEntryDto } from './history-entry.dto';
|
||||||
import { HistoryEntry } from './history-entry.entity';
|
import { HistoryEntry } from './history-entry.entity';
|
||||||
|
import { getIdentifier } from './utils';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HistoryService {
|
export class HistoryService {
|
||||||
|
@ -41,7 +42,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', 'user'],
|
relations: ['note', 'note.aliases', 'user'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +59,7 @@ export class HistoryService {
|
||||||
note: note,
|
note: note,
|
||||||
user: user,
|
user: user,
|
||||||
},
|
},
|
||||||
relations: ['note', 'user'],
|
relations: ['note', 'note.aliases', 'user'],
|
||||||
});
|
});
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
throw new NotInDBError(
|
throw new NotInDBError(
|
||||||
|
@ -150,23 +151,19 @@ export class HistoryService {
|
||||||
await this.connection.transaction(async (manager) => {
|
await this.connection.transaction(async (manager) => {
|
||||||
const currentHistory = await manager.find<HistoryEntry>(HistoryEntry, {
|
const currentHistory = await manager.find<HistoryEntry>(HistoryEntry, {
|
||||||
where: { user: user },
|
where: { user: user },
|
||||||
relations: ['note', 'user'],
|
relations: ['note', 'note.aliases', 'user'],
|
||||||
});
|
});
|
||||||
for (const entry of currentHistory) {
|
for (const entry of currentHistory) {
|
||||||
await manager.remove<HistoryEntry>(entry);
|
await manager.remove<HistoryEntry>(entry);
|
||||||
}
|
}
|
||||||
for (const historyEntry of history) {
|
for (const historyEntry of history) {
|
||||||
this.notesService.checkNoteIdOrAlias(historyEntry.note);
|
this.notesService.checkNoteIdOrAlias(historyEntry.note);
|
||||||
const note = await manager.findOne<Note>(Note, {
|
const note = await manager
|
||||||
where: [
|
.createQueryBuilder<Note>(Note, 'note')
|
||||||
{
|
.innerJoin('note.aliases', 'alias')
|
||||||
id: historyEntry.note,
|
.where('note.id = :id', { id: historyEntry.note })
|
||||||
},
|
.orWhere('alias.name = :id', { id: historyEntry.note })
|
||||||
{
|
.getOne();
|
||||||
alias: historyEntry.note,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
if (note === undefined) {
|
if (note === undefined) {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Could not find note '${historyEntry.note}'`,
|
`Could not find note '${historyEntry.note}'`,
|
||||||
|
@ -191,7 +188,7 @@ export class HistoryService {
|
||||||
*/
|
*/
|
||||||
toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto {
|
toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto {
|
||||||
return {
|
return {
|
||||||
identifier: entry.note.alias ? entry.note.alias : entry.note.id,
|
identifier: getIdentifier(entry),
|
||||||
lastVisited: entry.updatedAt,
|
lastVisited: entry.updatedAt,
|
||||||
tags: this.notesService.toTagList(entry.note),
|
tags: this.notesService.toTagList(entry.note),
|
||||||
title: entry.note.title ?? '',
|
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