From 0a8dd454ab674a4f93e01baa6622dd910e9b5b6d Mon Sep 17 00:00:00 2001 From: David Mehren Date: Thu, 29 Apr 2021 18:09:22 +0200 Subject: [PATCH] NoteEntity: Allow anonymous notes Notes created by anonymous users don't have an owner. This commit updates the entity accordingly. Signed-off-by: David Mehren --- src/notes/note.entity.ts | 7 ++++--- src/notes/notes.service.spec.ts | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index a3fda2b75..3f7da51c1 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -56,8 +56,9 @@ export class Note { viewCount: number; @ManyToOne((_) => User, (user) => user.ownedNotes, { onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted + nullable: true, }) - owner: User; + owner: User | null; @OneToMany((_) => Revision, (revision) => revision.note, { cascade: true }) revisions: Promise; @OneToMany((_) => AuthorColor, (authorColor) => authorColor.note) @@ -89,9 +90,9 @@ export class Note { } const newNote = new Note(); newNote.shortid = shortid; - newNote.alias = alias; + newNote.alias = alias ?? null; newNote.viewCount = 0; - newNote.owner = owner; + newNote.owner = owner ?? null; newNote.authorColors = []; newNote.userPermissions = []; newNote.groupPermissions = []; diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index bb2d3f20e..ab852a9e9 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -152,7 +152,7 @@ describe('NotesService', () => { expect(newNote.userPermissions).toHaveLength(0); expect(newNote.groupPermissions).toHaveLength(0); expect(newNote.tags).toHaveLength(0); - expect(newNote.owner).toBeUndefined(); + expect(newNote.owner).toBeNull(); expect(newNote.alias).toBeNull(); }); it('without alias, with owner', async () => { @@ -177,7 +177,7 @@ describe('NotesService', () => { expect(newNote.userPermissions).toHaveLength(0); expect(newNote.groupPermissions).toHaveLength(0); expect(newNote.tags).toHaveLength(0); - expect(newNote.owner).toBeUndefined(); + expect(newNote.owner).toBeNull(); expect(newNote.alias).toEqual(alias); }); it('with alias, with owner', async () => {