From dbf467fea56ecb62a6c8d49e7b7bf8ad7ce7541a Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 21 Nov 2021 17:12:35 +0100 Subject: [PATCH] chore: extract getNote code from GetNotePipe.transform This was done so the same code could be used in the PermissionsGuard Signed-off-by: Philip Molares --- src/api/utils/get-note.pipe.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/api/utils/get-note.pipe.ts b/src/api/utils/get-note.pipe.ts index f20c770ca..8790e326a 100644 --- a/src/api/utils/get-note.pipe.ts +++ b/src/api/utils/get-note.pipe.ts @@ -26,18 +26,25 @@ export class GetNotePipe implements PipeTransform> { } async transform(noteIdOrAlias: string, _: ArgumentMetadata): Promise { - let note: Note; - try { - note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias); - } catch (e) { - if (e instanceof NotInDBError) { - throw new NotFoundException(e.message); - } - if (e instanceof ForbiddenIdError) { - throw new BadRequestException(e.message); - } - throw e; - } - return note; + return await getNote(this.noteService, noteIdOrAlias); } } + +export async function getNote( + noteService: NotesService, + noteIdOrAlias: string, +): Promise { + let note: Note; + try { + note = await noteService.getNoteByIdOrAlias(noteIdOrAlias); + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + if (e instanceof ForbiddenIdError) { + throw new BadRequestException(e.message); + } + throw e; + } + return note; +}