feat: add aliases to service files

This commit makes it possible to identifier notes via any alias in the note and history service.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-06-06 17:47:38 +02:00 committed by David Mehren
parent babd5ce393
commit 2b76d33a23
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 45 additions and 27 deletions

View file

@ -11,6 +11,7 @@ import { NotInDBError } from '../errors/errors';
import { ConsoleLoggerService } from '../logger/console-logger.service'; import { ConsoleLoggerService } from '../logger/console-logger.service';
import { Note } from '../notes/note.entity'; import { Note } from '../notes/note.entity';
import { NotesService } from '../notes/notes.service'; import { NotesService } from '../notes/notes.service';
import { getPrimaryAlias } from '../notes/utils';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { HistoryEntryImportDto } from './history-entry-import.dto'; import { HistoryEntryImportDto } from './history-entry-import.dto';

View file

@ -24,6 +24,7 @@ import { RevisionsService } from '../revisions/revisions.service';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck'; import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck';
import { Alias } from './alias.entity';
import { NoteMetadataDto } from './note-metadata.dto'; import { NoteMetadataDto } from './note-metadata.dto';
import { import {
NotePermissionsDto, NotePermissionsDto,
@ -32,6 +33,7 @@ import {
import { NoteDto } from './note.dto'; import { NoteDto } from './note.dto';
import { Note } from './note.entity'; import { Note } from './note.entity';
import { Tag } from './tag.entity'; import { Tag } from './tag.entity';
import { getPrimaryAlias } from './utils';
@Injectable() @Injectable()
export class NotesService { export class NotesService {
@ -39,6 +41,7 @@ export class NotesService {
private readonly logger: ConsoleLoggerService, private readonly logger: ConsoleLoggerService,
@InjectRepository(Note) private noteRepository: Repository<Note>, @InjectRepository(Note) private noteRepository: Repository<Note>,
@InjectRepository(Tag) private tagRepository: Repository<Tag>, @InjectRepository(Tag) private tagRepository: Repository<Tag>,
@InjectRepository(Alias) private aliasRepository: Repository<Alias>,
@InjectRepository(User) private userRepository: Repository<User>, @InjectRepository(User) private userRepository: Repository<User>,
@Inject(UsersService) private usersService: UsersService, @Inject(UsersService) private usersService: UsersService,
@Inject(GroupsService) private groupsService: GroupsService, @Inject(GroupsService) private groupsService: GroupsService,
@ -59,7 +62,13 @@ export class NotesService {
async getUserNotes(user: User): Promise<Note[]> { async getUserNotes(user: User): Promise<Note[]> {
const notes = await this.noteRepository.find({ const notes = await this.noteRepository.find({
where: { owner: user }, where: { owner: user },
relations: ['owner', 'userPermissions', 'groupPermissions', 'tags'], relations: [
'owner',
'userPermissions',
'groupPermissions',
'tags',
'aliases',
],
}); });
if (notes === undefined) { if (notes === undefined) {
return []; return [];
@ -79,21 +88,19 @@ export class NotesService {
*/ */
async createNote( async createNote(
noteContent: string, noteContent: string,
alias?: NoteMetadataDto['alias'], alias?: string,
owner?: User, owner?: User,
): Promise<Note> { ): Promise<Note> {
const newNote = Note.create(); if (alias) {
this.checkNoteIdOrAlias(alias);
}
const newNote = Note.create(owner, alias);
//TODO: Calculate patch //TODO: Calculate patch
newNote.revisions = Promise.resolve([ newNote.revisions = Promise.resolve([
Revision.create(noteContent, noteContent), Revision.create(noteContent, noteContent),
]); ]);
if (alias) {
newNote.alias = alias;
this.checkNoteIdOrAlias(alias);
}
if (owner) { if (owner) {
newNote.historyEntries = [HistoryEntry.create(owner)]; newNote.historyEntries = [HistoryEntry.create(owner)];
newNote.owner = owner;
} }
try { try {
return await this.noteRepository.save(newNote); return await this.noteRepository.save(newNote);
@ -156,24 +163,33 @@ export class NotesService {
'getNoteByIdOrAlias', 'getNoteByIdOrAlias',
); );
this.checkNoteIdOrAlias(noteIdOrAlias); this.checkNoteIdOrAlias(noteIdOrAlias);
const note = await this.noteRepository.findOne({
where: [ /**
{ * This query gets the note's aliases, owner, groupPermissions (and the groups), userPermissions (and the users) and tags and
publicId: noteIdOrAlias, * then only gets the note, that either has a publicId :noteIdOrAlias or has any alias with this name.
}, **/
{ const note = await this.noteRepository
alias: noteIdOrAlias, .createQueryBuilder('note')
}, .leftJoinAndSelect('note.aliases', 'alias')
], .leftJoinAndSelect('note.owner', 'owner')
relations: [ .leftJoinAndSelect('note.groupPermissions', 'group_permission')
'owner', .leftJoinAndSelect('group_permission.group', 'group')
'groupPermissions', .leftJoinAndSelect('note.userPermissions', 'user_permission')
'groupPermissions.group', .leftJoinAndSelect('user_permission.user', 'user')
'userPermissions', .leftJoinAndSelect('note.tags', 'tag')
'userPermissions.user', .where('note.publicId = :noteIdOrAlias')
'tags', .orWhere((queryBuilder) => {
], const subQuery = queryBuilder
}); .subQuery()
.select('alias.noteId')
.from(Alias, 'alias')
.where('alias.name = :noteIdOrAlias')
.getQuery();
return 'note.id IN ' + subQuery;
})
.setParameter('noteIdOrAlias', noteIdOrAlias)
.getOne();
if (note === undefined) { if (note === undefined) {
this.logger.debug( this.logger.debug(
`Could not find note '${noteIdOrAlias}'`, `Could not find note '${noteIdOrAlias}'`,
@ -369,7 +385,8 @@ export class NotesService {
const updateUser = await this.calculateUpdateUser(note); const updateUser = await this.calculateUpdateUser(note);
return { return {
id: note.publicId, id: note.publicId,
alias: note.alias ?? null, aliases: note.aliases.map((alias) => alias.name),
primaryAlias: getPrimaryAlias(note) ?? null,
title: note.title ?? '', title: note.title ?? '',
createTime: (await this.getFirstRevision(note)).createdAt, createTime: (await this.getFirstRevision(note)).createdAt,
description: note.description ?? '', description: note.description ?? '',