diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index de6aa31de..f55337f1e 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -14,6 +14,7 @@ import { NotInDBError, PermissionsUpdateInconsistentError, } from '../errors/errors'; +import { Group } from '../groups/group.entity'; import { GroupsService } from '../groups/groups.service'; import { HistoryEntry } from '../history/history-entry.entity'; import { ConsoleLoggerService } from '../logger/console-logger.service'; @@ -335,6 +336,117 @@ export class NotesService { return await this.noteRepository.save(note); } + /** + * @async + * Set permission for a specific user on a note. + * @param {Note} note - the note + * @param {User} permissionUser - the user for which the permission should be set + * @param {boolean} canEdit - specifies if the user can edit the note + * @return {Note} the note with the new permission + */ + async setUserPermission( + note: Note, + permissionUser: User, + canEdit: boolean, + ): Promise { + const permissions = await note.userPermissions; + const permission = permissions.find( + (value: NoteUserPermission, index: number) => { + if (value.user.id == permissionUser.id) { + if (value.canEdit != canEdit) { + value.canEdit = canEdit; + permissions[index] = value; + } + return true; + } + }, + ); + if (permission == undefined) { + const noteUserPermission = NoteUserPermission.create( + permissionUser, + note, + canEdit, + ); + (await note.userPermissions).push(noteUserPermission); + } + return await this.noteRepository.save(note); + } + + /** + * @async + * Remove permission for a specific user on a note. + * @param {Note} note - the note + * @param {User} permissionUser - the user for which the permission should be set + * @return {Note} the note with the new permission + */ + async removeUserPermission(note: Note, permissionUser: User): Promise { + const permissions = await note.userPermissions; + const permissionsFiltered = permissions.filter( + (value: NoteUserPermission) => { + return value.user.id != permissionUser.id; + }, + ); + note.userPermissions = Promise.resolve(permissionsFiltered); + return await this.noteRepository.save(note); + } + + /** + * @async + * Set permission for a specific group on a note. + * @param {Note} note - the note + * @param {Group} permissionGroup - the group for which the permission should be set + * @param {boolean} canEdit - specifies if the group can edit the note + * @return {Note} the note with the new permission + */ + async setGroupPermission( + note: Note, + permissionGroup: Group, + canEdit: boolean, + ): Promise { + const permissions = await note.groupPermissions; + const permission = permissions.find( + (value: NoteGroupPermission, index: number) => { + if (value.group.id == permissionGroup.id) { + if (value.canEdit != canEdit) { + value.canEdit = canEdit; + permissions[index] = value; + } + return true; + } + }, + ); + if (permission == undefined) { + const noteGroupPermission = NoteGroupPermission.create( + permissionGroup, + note, + canEdit, + ); + (await note.groupPermissions).push(noteGroupPermission); + } + return await this.noteRepository.save(note); + } + + /** + * @async + * Remove permission for a specific group on a note. + * @param {Note} note - the note + * @param {Group} permissionGroup - the group for which the permission should be set + * @return {Note} the note with the new permission + */ + async removeGroupPermission( + note: Note, + permissionGroup: Group, + ): Promise { + const permissions = await note.groupPermissions; + const permissionsFiltered = permissions.filter( + (value: NoteGroupPermission) => { + return value.group.id != permissionGroup.id; + }, + ); + note.groupPermissions = Promise.resolve(permissionsFiltered); + return await this.noteRepository.save(note); + } + /** * @async * Calculate the updateUser (for the NoteDto) for a Note. @@ -417,6 +529,18 @@ export class NotesService { }; } + /** + * @async + * Updates the owner of a note. + * @param {Note} note - the note to use + * @param {User} owner - the new owner + * @return {Note} the updated note + */ + async changeOwner(note: Note, owner: User): Promise { + note.owner = Promise.resolve(owner); + return await this.noteRepository.save(note); + } + /** * @async * Build NoteDto from a note.