NotePermissions: Remove default constructors

As discussed in #835 we don't want to have default constructors and prefer .create methods.
Because the created NoteGroupPermission and NoteUserPermission are not saved to the DB by themselves, but are saved via a change to the Note using a Pick<Class, attributes>-style return type is not helpful here as every single time the .create functions are called a full object is required.
The mock calls in the PermissionService test are not needed and break the .create calls so they got removed.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-20 11:38:50 +01:00
parent 353384435e
commit 577811be29
3 changed files with 21 additions and 9 deletions

View file

@ -18,4 +18,14 @@ export class NoteGroupPermission {
@Column()
canEdit: boolean;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static create(group: Group, canEdit: boolean): NoteGroupPermission {
const groupPermission = new NoteGroupPermission();
groupPermission.group = group;
groupPermission.canEdit = canEdit;
return groupPermission;
}
}

View file

@ -18,4 +18,14 @@ export class NoteUserPermission {
@Column()
canEdit: boolean;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static create(user: User, canEdit: boolean): NoteUserPermission {
const userPermission = new NoteUserPermission();
userPermission.user = user;
userPermission.canEdit = canEdit;
return userPermission;
}
}

View file

@ -23,11 +23,6 @@ import { Revision } from '../revisions/revision.entity';
import { Tag } from '../notes/tag.entity';
import { Group } from '../groups/group.entity';
jest.mock('../permissions/note-group-permission.entity.ts');
jest.mock('../groups/group.entity.ts');
jest.mock('../notes/note.entity.ts');
jest.mock('../users/user.entity.ts');
describe('PermissionsService', () => {
let permissionsService: PermissionsService;
@ -292,10 +287,7 @@ describe('PermissionsService', () => {
group: Group,
write: boolean,
): NoteGroupPermission {
const noteGroupPermission = new NoteGroupPermission();
noteGroupPermission.canEdit = write;
noteGroupPermission.group = group;
return noteGroupPermission;
return NoteGroupPermission.create(group, write);
}
const everybodyRead = createNoteGroupPermission(groups['everybody'], false);