mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 18:25:21 -04:00
refactor(note): lazy-load relations
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
9e608c75d3
commit
3c0c11e3d4
23 changed files with 343 additions and 284 deletions
|
@ -158,8 +158,11 @@ describe('AliasService', () => {
|
|||
const alias2 = 'testAlias2';
|
||||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
describe('removes one alias correctly', () => {
|
||||
const note = Note.create(user, alias) as Note;
|
||||
note.aliases.push(Alias.create(alias2, note, false) as Alias);
|
||||
let note: Note;
|
||||
beforeAll(async () => {
|
||||
note = Note.create(user, alias) as Note;
|
||||
(await note.aliases).push(Alias.create(alias2, note, false) as Alias);
|
||||
});
|
||||
it('with two aliases', async () => {
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
|
@ -170,9 +173,10 @@ describe('AliasService', () => {
|
|||
async (alias: Alias): Promise<Alias> => alias,
|
||||
);
|
||||
const savedNote = await service.removeAlias(note, alias2);
|
||||
expect(savedNote.aliases).toHaveLength(1);
|
||||
expect(savedNote.aliases[0].name).toEqual(alias);
|
||||
expect(savedNote.aliases[0].primary).toBeTruthy();
|
||||
const aliases = await savedNote.aliases;
|
||||
expect(aliases).toHaveLength(1);
|
||||
expect(aliases[0].name).toEqual(alias);
|
||||
expect(aliases[0].primary).toBeTruthy();
|
||||
});
|
||||
it('with one alias, that is primary', async () => {
|
||||
jest
|
||||
|
@ -184,12 +188,15 @@ describe('AliasService', () => {
|
|||
async (alias: Alias): Promise<Alias> => alias,
|
||||
);
|
||||
const savedNote = await service.removeAlias(note, alias);
|
||||
expect(savedNote.aliases).toHaveLength(0);
|
||||
expect(await savedNote.aliases).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
describe('does not remove one alias', () => {
|
||||
const note = Note.create(user, alias) as Note;
|
||||
note.aliases.push(Alias.create(alias2, note, false) as Alias);
|
||||
let note: Note;
|
||||
beforeEach(async () => {
|
||||
note = Note.create(user, alias) as Note;
|
||||
(await note.aliases).push(Alias.create(alias2, note, false) as Alias);
|
||||
});
|
||||
it('if the alias is unknown', async () => {
|
||||
await expect(service.removeAlias(note, 'non existent')).rejects.toThrow(
|
||||
NotInDBError,
|
||||
|
@ -206,10 +213,18 @@ describe('AliasService', () => {
|
|||
describe('makeAliasPrimary', () => {
|
||||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
const aliasName = 'testAlias';
|
||||
const note = Note.create(user, aliasName) as Note;
|
||||
const alias = Alias.create(aliasName, note, true) as Alias;
|
||||
const alias2 = Alias.create('testAlias2', note, false) as Alias;
|
||||
note.aliases.push(alias2);
|
||||
let note: Note;
|
||||
let alias: Alias;
|
||||
let alias2: Alias;
|
||||
beforeEach(async () => {
|
||||
note = Note.create(user, aliasName) as Note;
|
||||
alias = Alias.create(aliasName, note, true) as Alias;
|
||||
alias2 = Alias.create('testAlias2', note, false) as Alias;
|
||||
(await note.aliases).push(
|
||||
Alias.create('testAlias2', note, false) as Alias,
|
||||
);
|
||||
});
|
||||
|
||||
it('mark the alias as primary', async () => {
|
||||
jest
|
||||
.spyOn(aliasRepo, 'findOne')
|
||||
|
@ -224,10 +239,10 @@ describe('AliasService', () => {
|
|||
where: () => createQueryBuilder,
|
||||
orWhere: () => createQueryBuilder,
|
||||
setParameter: () => createQueryBuilder,
|
||||
getOne: () => {
|
||||
getOne: async () => {
|
||||
return {
|
||||
...note,
|
||||
aliases: note.aliases.map((anAlias) => {
|
||||
aliases: (await note.aliases).map((anAlias) => {
|
||||
if (anAlias.primary) {
|
||||
anAlias.primary = false;
|
||||
}
|
||||
|
|
|
@ -62,13 +62,13 @@ export class AliasService {
|
|||
);
|
||||
}
|
||||
let newAlias;
|
||||
if (note.aliases.length === 0) {
|
||||
if ((await note.aliases).length === 0) {
|
||||
// the first alias is automatically made the primary alias
|
||||
newAlias = Alias.create(alias, note, true);
|
||||
} else {
|
||||
newAlias = Alias.create(alias, note, false);
|
||||
}
|
||||
note.aliases.push(newAlias as Alias);
|
||||
(await note.aliases).push(newAlias as Alias);
|
||||
|
||||
await this.noteRepository.save(note);
|
||||
return newAlias as Alias;
|
||||
|
@ -87,7 +87,7 @@ export class AliasService {
|
|||
let oldPrimaryId = '';
|
||||
let newPrimaryId = '';
|
||||
|
||||
for (const anAlias of note.aliases) {
|
||||
for (const anAlias of await note.aliases) {
|
||||
// found old primary
|
||||
if (anAlias.primary) {
|
||||
oldPrimaryId = anAlias.id;
|
||||
|
@ -134,9 +134,9 @@ export class AliasService {
|
|||
* @throws {PrimaryAliasDeletionForbiddenError} the primary alias can only be deleted if it's the only alias
|
||||
*/
|
||||
async removeAlias(note: Note, alias: string): Promise<Note> {
|
||||
const primaryAlias = getPrimaryAlias(note);
|
||||
const primaryAlias = await getPrimaryAlias(note);
|
||||
|
||||
if (primaryAlias === alias && note.aliases.length !== 1) {
|
||||
if (primaryAlias === alias && (await note.aliases).length !== 1) {
|
||||
this.logger.debug(
|
||||
`The alias '${alias}' is the primary alias, which can only be removed if it's the only alias.`,
|
||||
'removeAlias',
|
||||
|
@ -146,10 +146,10 @@ export class AliasService {
|
|||
);
|
||||
}
|
||||
|
||||
const filteredAliases = note.aliases.filter(
|
||||
const filteredAliases = (await note.aliases).filter(
|
||||
(anAlias) => anAlias.name !== alias,
|
||||
);
|
||||
if (note.aliases.length === filteredAliases.length) {
|
||||
if ((await note.aliases).length === filteredAliases.length) {
|
||||
this.logger.debug(
|
||||
`The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`,
|
||||
'removeAlias',
|
||||
|
@ -158,13 +158,13 @@ export class AliasService {
|
|||
`The alias '${alias}' is not used by this note or is the primary alias, which can't be removed.`,
|
||||
);
|
||||
}
|
||||
const aliasToDelete = note.aliases.find(
|
||||
const aliasToDelete = (await note.aliases).find(
|
||||
(anAlias) => anAlias.name === alias,
|
||||
);
|
||||
if (aliasToDelete !== undefined) {
|
||||
await this.aliasRepository.remove(aliasToDelete);
|
||||
}
|
||||
note.aliases = filteredAliases;
|
||||
note.aliases = Promise.resolve(filteredAliases);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,21 +36,21 @@ export class Note {
|
|||
(alias) => alias.note,
|
||||
{ cascade: true }, // This ensures that embedded Aliases are automatically saved to the database
|
||||
)
|
||||
aliases: Alias[];
|
||||
aliases: Promise<Alias[]>;
|
||||
|
||||
@OneToMany(
|
||||
(_) => NoteGroupPermission,
|
||||
(groupPermission) => groupPermission.note,
|
||||
{ cascade: true }, // This ensures that embedded NoteGroupPermissions are automatically saved to the database
|
||||
)
|
||||
groupPermissions: NoteGroupPermission[];
|
||||
groupPermissions: Promise<NoteGroupPermission[]>;
|
||||
|
||||
@OneToMany(
|
||||
(_) => NoteUserPermission,
|
||||
(userPermission) => userPermission.note,
|
||||
{ cascade: true }, // This ensures that embedded NoteUserPermission are automatically saved to the database
|
||||
)
|
||||
userPermissions: NoteUserPermission[];
|
||||
userPermissions: Promise<NoteUserPermission[]>;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
|
@ -62,16 +62,16 @@ export class Note {
|
|||
onDelete: 'CASCADE', // This deletes the Note, when the associated User is deleted
|
||||
nullable: true,
|
||||
})
|
||||
owner: User | null;
|
||||
owner: Promise<User | null>;
|
||||
|
||||
@OneToMany((_) => Revision, (revision) => revision.note, { cascade: true })
|
||||
revisions: Promise<Revision[]>;
|
||||
|
||||
@OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user)
|
||||
historyEntries: HistoryEntry[];
|
||||
historyEntries: Promise<HistoryEntry[]>;
|
||||
|
||||
@OneToMany((_) => MediaUpload, (mediaUpload) => mediaUpload.note)
|
||||
mediaUploads: MediaUpload[];
|
||||
mediaUploads: Promise<MediaUpload[]>;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
|
@ -87,7 +87,7 @@ export class Note {
|
|||
|
||||
@ManyToMany((_) => Tag, (tag) => tag.notes, { eager: true, cascade: true })
|
||||
@JoinTable()
|
||||
tags: Tag[];
|
||||
tags: Promise<Tag[]>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
|
@ -101,18 +101,18 @@ export class Note {
|
|||
const newNote = new Note();
|
||||
newNote.publicId = generatePublicId();
|
||||
newNote.aliases = alias
|
||||
? [Alias.create(alias, newNote, true) as Alias]
|
||||
: [];
|
||||
newNote.userPermissions = [];
|
||||
newNote.groupPermissions = [];
|
||||
? Promise.resolve([Alias.create(alias, newNote, true) as Alias])
|
||||
: Promise.resolve([]);
|
||||
newNote.userPermissions = Promise.resolve([]);
|
||||
newNote.groupPermissions = Promise.resolve([]);
|
||||
newNote.viewCount = 0;
|
||||
newNote.owner = owner;
|
||||
newNote.owner = Promise.resolve(owner);
|
||||
newNote.revisions = Promise.resolve([]);
|
||||
newNote.historyEntries = [];
|
||||
newNote.mediaUploads = [];
|
||||
newNote.historyEntries = Promise.resolve([]);
|
||||
newNote.mediaUploads = Promise.resolve([]);
|
||||
newNote.description = null;
|
||||
newNote.title = null;
|
||||
newNote.tags = [];
|
||||
newNote.tags = Promise.resolve([]);
|
||||
return newNote;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -168,51 +168,51 @@ describe('NotesService', () => {
|
|||
const revisions = await newNote.revisions;
|
||||
expect(revisions).toHaveLength(1);
|
||||
expect(revisions[0].content).toEqual(content);
|
||||
expect(newNote.historyEntries).toHaveLength(0);
|
||||
expect(newNote.userPermissions).toHaveLength(0);
|
||||
expect(newNote.groupPermissions).toHaveLength(0);
|
||||
expect(newNote.tags).toHaveLength(0);
|
||||
expect(newNote.owner).toBeNull();
|
||||
expect(newNote.aliases).toHaveLength(0);
|
||||
expect(await newNote.historyEntries).toHaveLength(0);
|
||||
expect(await newNote.userPermissions).toHaveLength(0);
|
||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
||||
expect(await newNote.tags).toHaveLength(0);
|
||||
expect(await newNote.owner).toBeNull();
|
||||
expect(await newNote.aliases).toHaveLength(0);
|
||||
});
|
||||
it('without alias, with owner', async () => {
|
||||
const newNote = await service.createNote(content, user);
|
||||
const revisions = await newNote.revisions;
|
||||
expect(revisions).toHaveLength(1);
|
||||
expect(revisions[0].content).toEqual(content);
|
||||
expect(newNote.historyEntries).toHaveLength(1);
|
||||
expect(newNote.historyEntries[0].user).toEqual(user);
|
||||
expect(newNote.userPermissions).toHaveLength(0);
|
||||
expect(newNote.groupPermissions).toHaveLength(0);
|
||||
expect(newNote.tags).toHaveLength(0);
|
||||
expect(newNote.owner).toEqual(user);
|
||||
expect(newNote.aliases).toHaveLength(0);
|
||||
expect(await newNote.historyEntries).toHaveLength(1);
|
||||
expect((await newNote.historyEntries)[0].user).toEqual(user);
|
||||
expect(await newNote.userPermissions).toHaveLength(0);
|
||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
||||
expect(await newNote.tags).toHaveLength(0);
|
||||
expect(await newNote.owner).toEqual(user);
|
||||
expect(await newNote.aliases).toHaveLength(0);
|
||||
});
|
||||
it('with alias, without owner', async () => {
|
||||
const newNote = await service.createNote(content, null, alias);
|
||||
const revisions = await newNote.revisions;
|
||||
expect(revisions).toHaveLength(1);
|
||||
expect(revisions[0].content).toEqual(content);
|
||||
expect(newNote.historyEntries).toHaveLength(0);
|
||||
expect(newNote.userPermissions).toHaveLength(0);
|
||||
expect(newNote.groupPermissions).toHaveLength(0);
|
||||
expect(newNote.tags).toHaveLength(0);
|
||||
expect(newNote.owner).toBeNull();
|
||||
expect(newNote.aliases).toHaveLength(1);
|
||||
expect(await newNote.historyEntries).toHaveLength(0);
|
||||
expect(await newNote.userPermissions).toHaveLength(0);
|
||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
||||
expect(await newNote.tags).toHaveLength(0);
|
||||
expect(await newNote.owner).toBeNull();
|
||||
expect(await newNote.aliases).toHaveLength(1);
|
||||
});
|
||||
it('with alias, with owner', async () => {
|
||||
const newNote = await service.createNote(content, user, alias);
|
||||
const revisions = await newNote.revisions;
|
||||
expect(revisions).toHaveLength(1);
|
||||
expect(revisions[0].content).toEqual(content);
|
||||
expect(newNote.historyEntries).toHaveLength(1);
|
||||
expect(newNote.historyEntries[0].user).toEqual(user);
|
||||
expect(newNote.userPermissions).toHaveLength(0);
|
||||
expect(newNote.groupPermissions).toHaveLength(0);
|
||||
expect(newNote.tags).toHaveLength(0);
|
||||
expect(newNote.owner).toEqual(user);
|
||||
expect(newNote.aliases).toHaveLength(1);
|
||||
expect(newNote.aliases[0].name).toEqual(alias);
|
||||
expect(await newNote.historyEntries).toHaveLength(1);
|
||||
expect((await newNote.historyEntries)[0].user).toEqual(user);
|
||||
expect(await newNote.userPermissions).toHaveLength(0);
|
||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
||||
expect(await newNote.tags).toHaveLength(0);
|
||||
expect(await newNote.owner).toEqual(user);
|
||||
expect(await newNote.aliases).toHaveLength(1);
|
||||
expect((await newNote.aliases)[0].name).toEqual(alias);
|
||||
});
|
||||
});
|
||||
describe('fails:', () => {
|
||||
|
@ -379,8 +379,8 @@ describe('NotesService', () => {
|
|||
sharedToUsers: [],
|
||||
sharedToGroups: [],
|
||||
});
|
||||
expect(savedNote.userPermissions).toHaveLength(0);
|
||||
expect(savedNote.groupPermissions).toHaveLength(0);
|
||||
expect(await savedNote.userPermissions).toHaveLength(0);
|
||||
expect(await savedNote.groupPermissions).toHaveLength(0);
|
||||
});
|
||||
it('with empty GroupPermissions and with new UserPermissions', async () => {
|
||||
jest
|
||||
|
@ -393,24 +393,24 @@ describe('NotesService', () => {
|
|||
sharedToUsers: [userPermissionUpdate],
|
||||
sharedToGroups: [],
|
||||
});
|
||||
expect(savedNote.userPermissions).toHaveLength(1);
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect(await savedNote.userPermissions).toHaveLength(1);
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions).toHaveLength(0);
|
||||
expect(await savedNote.groupPermissions).toHaveLength(0);
|
||||
});
|
||||
it('with empty GroupPermissions and with existing UserPermissions', async () => {
|
||||
const noteWithPreexistingPermissions: Note = { ...note };
|
||||
noteWithPreexistingPermissions.userPermissions = [
|
||||
noteWithPreexistingPermissions.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithPreexistingPermissions,
|
||||
user: user,
|
||||
canEdit: !userPermissionUpdate.canEdit,
|
||||
},
|
||||
];
|
||||
]);
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
.mockImplementationOnce(async (entry: Note) => {
|
||||
|
@ -421,14 +421,14 @@ describe('NotesService', () => {
|
|||
sharedToUsers: [userPermissionUpdate],
|
||||
sharedToGroups: [],
|
||||
});
|
||||
expect(savedNote.userPermissions).toHaveLength(1);
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect(await savedNote.userPermissions).toHaveLength(1);
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions).toHaveLength(0);
|
||||
expect(await savedNote.groupPermissions).toHaveLength(0);
|
||||
});
|
||||
it('with new GroupPermissions and with empty UserPermissions', async () => {
|
||||
jest
|
||||
|
@ -441,11 +441,11 @@ describe('NotesService', () => {
|
|||
sharedToUsers: [],
|
||||
sharedToGroups: [groupPermissionUpate],
|
||||
});
|
||||
expect(savedNote.userPermissions).toHaveLength(0);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect(await savedNote.userPermissions).toHaveLength(0);
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
|
@ -461,28 +461,28 @@ describe('NotesService', () => {
|
|||
sharedToUsers: [userPermissionUpdate],
|
||||
sharedToGroups: [groupPermissionUpate],
|
||||
});
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
it('with new GroupPermissions and with existing UserPermissions', async () => {
|
||||
const noteWithUserPermission: Note = { ...note };
|
||||
noteWithUserPermission.userPermissions = [
|
||||
noteWithUserPermission.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithUserPermission,
|
||||
user: user,
|
||||
canEdit: !userPermissionUpdate.canEdit,
|
||||
},
|
||||
];
|
||||
]);
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
.mockImplementationOnce(async (entry: Note) => {
|
||||
|
@ -497,28 +497,28 @@ describe('NotesService', () => {
|
|||
sharedToGroups: [groupPermissionUpate],
|
||||
},
|
||||
);
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
it('with existing GroupPermissions and with empty UserPermissions', async () => {
|
||||
const noteWithPreexistingPermissions: Note = { ...note };
|
||||
noteWithPreexistingPermissions.groupPermissions = [
|
||||
noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithPreexistingPermissions,
|
||||
group: group,
|
||||
canEdit: !groupPermissionUpate.canEdit,
|
||||
},
|
||||
];
|
||||
]);
|
||||
jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(group);
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
|
@ -532,23 +532,23 @@ describe('NotesService', () => {
|
|||
sharedToGroups: [groupPermissionUpate],
|
||||
},
|
||||
);
|
||||
expect(savedNote.userPermissions).toHaveLength(0);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect(await savedNote.userPermissions).toHaveLength(0);
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
it('with existing GroupPermissions and with new UserPermissions', async () => {
|
||||
const noteWithPreexistingPermissions: Note = { ...note };
|
||||
noteWithPreexistingPermissions.groupPermissions = [
|
||||
noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithPreexistingPermissions,
|
||||
group: group,
|
||||
canEdit: !groupPermissionUpate.canEdit,
|
||||
},
|
||||
];
|
||||
]);
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
.mockImplementationOnce(async (entry: Note) => {
|
||||
|
@ -563,35 +563,35 @@ describe('NotesService', () => {
|
|||
sharedToGroups: [groupPermissionUpate],
|
||||
},
|
||||
);
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
it('with existing GroupPermissions and with existing UserPermissions', async () => {
|
||||
const noteWithPreexistingPermissions: Note = { ...note };
|
||||
noteWithPreexistingPermissions.groupPermissions = [
|
||||
noteWithPreexistingPermissions.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithPreexistingPermissions,
|
||||
group: group,
|
||||
canEdit: !groupPermissionUpate.canEdit,
|
||||
},
|
||||
];
|
||||
noteWithPreexistingPermissions.userPermissions = [
|
||||
]);
|
||||
noteWithPreexistingPermissions.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: noteWithPreexistingPermissions,
|
||||
user: user,
|
||||
canEdit: !userPermissionUpdate.canEdit,
|
||||
},
|
||||
];
|
||||
]);
|
||||
jest
|
||||
.spyOn(noteRepo, 'save')
|
||||
.mockImplementationOnce(async (entry: Note) => {
|
||||
|
@ -606,16 +606,16 @@ describe('NotesService', () => {
|
|||
sharedToGroups: [groupPermissionUpate],
|
||||
},
|
||||
);
|
||||
expect(savedNote.userPermissions[0].user.username).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].user.username).toEqual(
|
||||
userPermissionUpdate.username,
|
||||
);
|
||||
expect(savedNote.userPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.userPermissions)[0].canEdit).toEqual(
|
||||
userPermissionUpdate.canEdit,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].group.name).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].group.name).toEqual(
|
||||
groupPermissionUpate.groupname,
|
||||
);
|
||||
expect(savedNote.groupPermissions[0].canEdit).toEqual(
|
||||
expect((await savedNote.groupPermissions)[0].canEdit).toEqual(
|
||||
groupPermissionUpate.canEdit,
|
||||
);
|
||||
});
|
||||
|
@ -653,16 +653,16 @@ describe('NotesService', () => {
|
|||
describe('toTagList', () => {
|
||||
it('works', async () => {
|
||||
const note = {} as Note;
|
||||
note.tags = [
|
||||
note.tags = Promise.resolve([
|
||||
{
|
||||
id: 1,
|
||||
name: 'testTag',
|
||||
notes: [note],
|
||||
},
|
||||
];
|
||||
const tagList = service.toTagList(note);
|
||||
]);
|
||||
const tagList = await service.toTagList(note);
|
||||
expect(tagList).toHaveLength(1);
|
||||
expect(tagList[0]).toEqual(note.tags[0].name);
|
||||
expect(tagList[0]).toEqual((await note.tags)[0].name);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -671,21 +671,21 @@ describe('NotesService', () => {
|
|||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
const group = Group.create('testGroup', 'testGroup', false) as Group;
|
||||
const note = Note.create(user) as Note;
|
||||
note.userPermissions = [
|
||||
note.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
user: user,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
note.groupPermissions = [
|
||||
]);
|
||||
note.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
group: group,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
const permissions = service.toNotePermissionsDto(note);
|
||||
]);
|
||||
const permissions = await service.toNotePermissionsDto(note);
|
||||
expect(permissions.owner).not.toEqual(null);
|
||||
expect(permissions.owner?.username).toEqual(user.username);
|
||||
expect(permissions.sharedToUsers).toHaveLength(1);
|
||||
|
@ -740,36 +740,38 @@ describe('NotesService', () => {
|
|||
// @ts-ignore
|
||||
.mockImplementation(() => createQueryBuilder);
|
||||
note.publicId = 'testId';
|
||||
note.aliases = [Alias.create('testAlias', note, true) as Alias];
|
||||
note.aliases = Promise.resolve([
|
||||
Alias.create('testAlias', note, true) as Alias,
|
||||
]);
|
||||
note.title = 'testTitle';
|
||||
note.description = 'testDescription';
|
||||
note.owner = user;
|
||||
note.userPermissions = [
|
||||
note.owner = Promise.resolve(user);
|
||||
note.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
user: user,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
note.groupPermissions = [
|
||||
]);
|
||||
note.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
group: group,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
note.tags = [
|
||||
]);
|
||||
note.tags = Promise.resolve([
|
||||
{
|
||||
id: 1,
|
||||
name: 'testTag',
|
||||
notes: [note],
|
||||
},
|
||||
];
|
||||
]);
|
||||
note.viewCount = 1337;
|
||||
const metadataDto = await service.toNoteMetadataDto(note);
|
||||
expect(metadataDto.id).toEqual(note.publicId);
|
||||
expect(metadataDto.aliases).toHaveLength(1);
|
||||
expect(metadataDto.aliases[0]).toEqual(note.aliases[0].name);
|
||||
expect(metadataDto.aliases[0]).toEqual((await note.aliases)[0].name);
|
||||
expect(metadataDto.title).toEqual(note.title);
|
||||
expect(metadataDto.createTime).toEqual(revisions[0].createdAt);
|
||||
expect(metadataDto.description).toEqual(note.description);
|
||||
|
@ -787,7 +789,7 @@ describe('NotesService', () => {
|
|||
).toEqual(group.displayName);
|
||||
expect(metadataDto.permissions.sharedToGroups[0].canEdit).toEqual(true);
|
||||
expect(metadataDto.tags).toHaveLength(1);
|
||||
expect(metadataDto.tags[0]).toEqual(note.tags[0].name);
|
||||
expect(metadataDto.tags[0]).toEqual((await note.tags)[0].name);
|
||||
expect(metadataDto.updateTime).toEqual(revisions[0].createdAt);
|
||||
expect(metadataDto.updateUser.username).toEqual(user.username);
|
||||
expect(metadataDto.viewCount).toEqual(note.viewCount);
|
||||
|
@ -840,36 +842,38 @@ describe('NotesService', () => {
|
|||
// @ts-ignore
|
||||
.mockImplementation(() => createQueryBuilder);
|
||||
note.publicId = 'testId';
|
||||
note.aliases = [Alias.create('testAlias', note, true) as Alias];
|
||||
note.aliases = Promise.resolve([
|
||||
Alias.create('testAlias', note, true) as Alias,
|
||||
]);
|
||||
note.title = 'testTitle';
|
||||
note.description = 'testDescription';
|
||||
note.owner = user;
|
||||
note.userPermissions = [
|
||||
note.owner = Promise.resolve(user);
|
||||
note.userPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
user: user,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
note.groupPermissions = [
|
||||
]);
|
||||
note.groupPermissions = Promise.resolve([
|
||||
{
|
||||
note: note,
|
||||
group: group,
|
||||
canEdit: true,
|
||||
},
|
||||
];
|
||||
note.tags = [
|
||||
]);
|
||||
note.tags = Promise.resolve([
|
||||
{
|
||||
id: 1,
|
||||
name: 'testTag',
|
||||
notes: [note],
|
||||
},
|
||||
];
|
||||
]);
|
||||
note.viewCount = 1337;
|
||||
const noteDto = await service.toNoteDto(note);
|
||||
expect(noteDto.metadata.id).toEqual(note.publicId);
|
||||
expect(noteDto.metadata.aliases).toHaveLength(1);
|
||||
expect(noteDto.metadata.aliases[0]).toEqual(note.aliases[0].name);
|
||||
expect(noteDto.metadata.aliases[0]).toEqual((await note.aliases)[0].name);
|
||||
expect(noteDto.metadata.title).toEqual(note.title);
|
||||
expect(noteDto.metadata.createTime).toEqual(revisions[0].createdAt);
|
||||
expect(noteDto.metadata.description).toEqual(note.description);
|
||||
|
@ -893,7 +897,7 @@ describe('NotesService', () => {
|
|||
true,
|
||||
);
|
||||
expect(noteDto.metadata.tags).toHaveLength(1);
|
||||
expect(noteDto.metadata.tags[0]).toEqual(note.tags[0].name);
|
||||
expect(noteDto.metadata.tags[0]).toEqual((await note.tags)[0].name);
|
||||
expect(noteDto.metadata.updateTime).toEqual(revisions[0].createdAt);
|
||||
expect(noteDto.metadata.updateUser.username).toEqual(user.username);
|
||||
expect(noteDto.metadata.viewCount).toEqual(note.viewCount);
|
||||
|
|
|
@ -100,9 +100,9 @@ export class NotesService {
|
|||
Revision.create(noteContent, noteContent, newNote as Note) as Revision,
|
||||
]);
|
||||
if (owner) {
|
||||
newNote.historyEntries = [
|
||||
newNote.historyEntries = Promise.resolve([
|
||||
HistoryEntry.create(owner, newNote as Note) as HistoryEntry,
|
||||
];
|
||||
]);
|
||||
}
|
||||
try {
|
||||
return await this.noteRepository.save(newNote);
|
||||
|
@ -262,8 +262,8 @@ export class NotesService {
|
|||
//TODO: Calculate patch
|
||||
revisions.push(Revision.create(noteContent, noteContent, note) as Revision);
|
||||
note.revisions = Promise.resolve(revisions);
|
||||
note.userPermissions = [];
|
||||
note.groupPermissions = [];
|
||||
note.userPermissions = Promise.resolve([]);
|
||||
note.groupPermissions = Promise.resolve([]);
|
||||
return await this.noteRepository.save(note);
|
||||
}
|
||||
|
||||
|
@ -298,8 +298,8 @@ export class NotesService {
|
|||
);
|
||||
}
|
||||
|
||||
note.userPermissions = [];
|
||||
note.groupPermissions = [];
|
||||
note.userPermissions = Promise.resolve([]);
|
||||
note.groupPermissions = Promise.resolve([]);
|
||||
|
||||
// Create new userPermissions
|
||||
for (const newUserPermission of newPermissions.sharedToUsers) {
|
||||
|
@ -312,7 +312,7 @@ export class NotesService {
|
|||
newUserPermission.canEdit,
|
||||
);
|
||||
createdPermission.note = note;
|
||||
note.userPermissions.push(createdPermission);
|
||||
(await note.userPermissions).push(createdPermission);
|
||||
}
|
||||
|
||||
// Create groupPermissions
|
||||
|
@ -326,7 +326,7 @@ export class NotesService {
|
|||
newGroupPermission.canEdit,
|
||||
);
|
||||
createdPermission.note = note;
|
||||
note.groupPermissions.push(createdPermission);
|
||||
(await note.groupPermissions).push(createdPermission);
|
||||
}
|
||||
|
||||
return await this.noteRepository.save(note);
|
||||
|
@ -348,7 +348,7 @@ export class NotesService {
|
|||
)[0].author.user;
|
||||
}
|
||||
// If there are no Edits, the owner is the updateUser
|
||||
return note.owner;
|
||||
return await note.owner;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -356,8 +356,8 @@ export class NotesService {
|
|||
* @param {Note} note - the note to use
|
||||
* @return {string[]} string array of tags names
|
||||
*/
|
||||
toTagList(note: Note): string[] {
|
||||
return note.tags.map((tag) => tag.name);
|
||||
async toTagList(note: Note): Promise<string[]> {
|
||||
return (await note.tags).map((tag) => tag.name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -365,14 +365,17 @@ export class NotesService {
|
|||
* @param {Note} note - the note to use
|
||||
* @return {NotePermissionsDto} the built NotePermissionDto
|
||||
*/
|
||||
toNotePermissionsDto(note: Note): NotePermissionsDto {
|
||||
async toNotePermissionsDto(note: Note): Promise<NotePermissionsDto> {
|
||||
const owner = await note.owner;
|
||||
const userPermissions = await note.userPermissions;
|
||||
const groupPermissions = await note.groupPermissions;
|
||||
return {
|
||||
owner: note.owner ? this.usersService.toUserDto(note.owner) : null,
|
||||
sharedToUsers: note.userPermissions.map((noteUserPermission) => ({
|
||||
owner: owner ? this.usersService.toUserDto(owner) : null,
|
||||
sharedToUsers: userPermissions.map((noteUserPermission) => ({
|
||||
user: this.usersService.toUserDto(noteUserPermission.user),
|
||||
canEdit: noteUserPermission.canEdit,
|
||||
})),
|
||||
sharedToGroups: note.groupPermissions.map((noteGroupPermission) => ({
|
||||
sharedToGroups: groupPermissions.map((noteGroupPermission) => ({
|
||||
group: this.groupsService.toGroupDto(noteGroupPermission.group),
|
||||
canEdit: noteGroupPermission.canEdit,
|
||||
})),
|
||||
|
@ -389,14 +392,16 @@ export class NotesService {
|
|||
const updateUser = await this.calculateUpdateUser(note);
|
||||
return {
|
||||
id: note.publicId,
|
||||
aliases: note.aliases.map((alias) => alias.name),
|
||||
primaryAlias: getPrimaryAlias(note) ?? null,
|
||||
aliases: await Promise.all(
|
||||
(await note.aliases).map((alias) => alias.name),
|
||||
),
|
||||
primaryAlias: (await getPrimaryAlias(note)) ?? null,
|
||||
title: note.title ?? '',
|
||||
createTime: (await this.getFirstRevision(note)).createdAt,
|
||||
description: note.description ?? '',
|
||||
editedBy: (await this.getAuthorUsers(note)).map((user) => user.username),
|
||||
permissions: this.toNotePermissionsDto(note),
|
||||
tags: this.toTagList(note),
|
||||
permissions: await this.toNotePermissionsDto(note),
|
||||
tags: await this.toTagList(note),
|
||||
updateTime: (await this.getLatestRevision(note)).createdAt,
|
||||
updateUser: updateUser ? this.usersService.toUserDto(updateUser) : null,
|
||||
viewCount: note.viewCount,
|
||||
|
|
|
@ -29,12 +29,12 @@ describe('getPrimaryAlias', () => {
|
|||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
note = Note.create(user, alias) as Note;
|
||||
});
|
||||
it('finds correct primary alias', () => {
|
||||
note.aliases.push(Alias.create('annother', note, false) as Alias);
|
||||
expect(getPrimaryAlias(note)).toEqual(alias);
|
||||
it('finds correct primary alias', async () => {
|
||||
(await note.aliases).push(Alias.create('annother', note, false) as Alias);
|
||||
expect(await getPrimaryAlias(note)).toEqual(alias);
|
||||
});
|
||||
it('returns undefined if there is no alias', () => {
|
||||
note.aliases[0].primary = false;
|
||||
expect(getPrimaryAlias(note)).toEqual(undefined);
|
||||
it('returns undefined if there is no alias', async () => {
|
||||
(await note.aliases)[0].primary = false;
|
||||
expect(await getPrimaryAlias(note)).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -22,8 +22,8 @@ export function generatePublicId(): string {
|
|||
* Extract the primary alias from a aliases of a note
|
||||
* @param {Note} note - the note from which the primary alias should be extracted
|
||||
*/
|
||||
export function getPrimaryAlias(note: Note): string | undefined {
|
||||
const listWithPrimaryAlias = note.aliases.filter(
|
||||
export async function getPrimaryAlias(note: Note): Promise<string | undefined> {
|
||||
const listWithPrimaryAlias = (await note.aliases).filter(
|
||||
(alias: Alias) => alias.primary,
|
||||
);
|
||||
if (listWithPrimaryAlias.length !== 1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue