-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BC-7971 - introduce room members module #5291
Changes from all commits
786ce74
66b10bc
91fe96e
199aed9
6d085a3
ac210c1
c2c60db
d1c367c
48ae35f
fb26f78
a04929d
1ae2f1e
72af51c
11dac30
eca91f3
bdb8a35
e8bc6a8
f58d452
d633c98
cc7d5b5
97eb5a9
ca1eaed
e3590ec
b6f00b9
0d6143f
4a9f7c4
c6fab6b
da969dc
100f17d
0b5dd82
7835361
74f4f10
23988d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Migration } from '@mikro-orm/migrations-mongodb'; | ||
|
||
export class Migration202410041210124 extends Migration { | ||
async up(): Promise<void> { | ||
// Add ROOM_VIEWER role | ||
await this.getCollection('roles').insertOne({ | ||
name: 'room_viewer', | ||
permissions: ['ROOM_VIEW'], | ||
}); | ||
console.info('Added ROOM_VIEWER role with ROOM_VIEW permission'); | ||
|
||
// Add ROOM_EDITOR role | ||
await this.getCollection('roles').insertOne({ | ||
name: 'room_editor', | ||
permissions: ['ROOM_VIEW', 'ROOM_EDIT'], | ||
}); | ||
console.info('Added ROOM_EDITOR role with ROOM_VIEW and ROOM_EDIT permissions'); | ||
} | ||
|
||
async down(): Promise<void> { | ||
// Remove ROOM_VIEWER role | ||
await this.getCollection('roles').deleteOne({ name: 'ROOM_VIEWER' }); | ||
console.info('Rollback: Removed ROOM_VIEWER role'); | ||
|
||
// Remove ROOM_EDITOR role | ||
await this.getCollection('roles').deleteOne({ name: 'ROOM_EDITOR' }); | ||
console.info('Rollback: Removed ROOM_EDITOR role'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export enum GroupTypeResponse { | ||
CLASS = 'class', | ||
COURSE = 'course', | ||
ROOM = 'room', | ||
OTHER = 'other', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export enum GroupTypes { | ||
CLASS = 'class', | ||
COURSE = 'course', | ||
ROOM = 'room', | ||
OTHER = 'other', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { GroupValidPeriodEmbeddable } from './group-valid-period.embeddable'; | |
export enum GroupEntityTypes { | ||
CLASS = 'class', | ||
COURSE = 'course', | ||
ROOM = 'room', | ||
OTHER = 'other', | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also similar to the response above we could use the |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,14 @@ import { GroupEntity, GroupEntityTypes, GroupUserEmbeddable, GroupValidPeriodEmb | |
const GroupEntityTypesToGroupTypesMapping: Record<GroupEntityTypes, GroupTypes> = { | ||
[GroupEntityTypes.CLASS]: GroupTypes.CLASS, | ||
[GroupEntityTypes.COURSE]: GroupTypes.COURSE, | ||
[GroupEntityTypes.ROOM]: GroupTypes.ROOM, | ||
[GroupEntityTypes.OTHER]: GroupTypes.OTHER, | ||
}; | ||
|
||
export const GroupTypesToGroupEntityTypesMapping: Record<GroupTypes, GroupEntityTypes> = { | ||
[GroupTypes.CLASS]: GroupEntityTypes.CLASS, | ||
[GroupTypes.COURSE]: GroupEntityTypes.COURSE, | ||
[GroupTypes.ROOM]: GroupEntityTypes.ROOM, | ||
[GroupTypes.OTHER]: GroupEntityTypes.OTHER, | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This kind of mapping wouldn't be necessary when we used |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { AuthorizationLoaderServiceGeneric } from '@modules/authorization'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { NotFoundLoggableException } from '@shared/common/loggable-exception'; | ||
import { Page } from '@shared/domain/domainobject'; | ||
import { IFindOptions } from '@shared/domain/interface'; | ||
import { IFindOptions, RoleName } from '@shared/domain/interface'; | ||
import { EntityId } from '@shared/domain/types'; | ||
import { Group, GroupDeletedEvent, GroupFilter } from '../domain'; | ||
import { RoleService } from '@src/modules/role'; | ||
import { UserService } from '@src/modules/user/service/user.service'; | ||
import { Group, GroupDeletedEvent, GroupFilter, GroupTypes, GroupUser } from '../domain'; | ||
import { GroupRepo } from '../repo'; | ||
|
||
@Injectable() | ||
export class GroupService implements AuthorizationLoaderServiceGeneric<Group> { | ||
constructor(private readonly groupRepo: GroupRepo, private readonly eventBus: EventBus) {} | ||
constructor( | ||
private readonly groupRepo: GroupRepo, | ||
private readonly userService: UserService, | ||
private readonly roleService: RoleService, | ||
private readonly eventBus: EventBus | ||
) {} | ||
|
||
public async findById(id: EntityId): Promise<Group> { | ||
const group: Group | null = await this.groupRepo.findGroupById(id); | ||
|
@@ -57,4 +65,31 @@ export class GroupService implements AuthorizationLoaderServiceGeneric<Group> { | |
|
||
await this.eventBus.publish(new GroupDeletedEvent(group)); | ||
} | ||
|
||
public async createGroup(name: string, type: GroupTypes, organizationId?: EntityId): Promise<Group> { | ||
const group = new Group({ | ||
name, | ||
users: [], | ||
id: new ObjectId().toHexString(), | ||
type, | ||
organizationId, | ||
}); | ||
|
||
await this.save(group); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For persistence operations it's maybe better to use the repo instead of the service methods. This can help prevent unwanted side effects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of the points discussed where that room members should not have access to the group internals. |
||
|
||
return group; | ||
} | ||
|
||
public async addUserToGroup(groupId: EntityId, userId: EntityId, roleName: RoleName): Promise<void> { | ||
const role = await this.roleService.findByName(roleName); | ||
if (!role.id) throw new BadRequestException('Role has no id.'); | ||
const group = await this.findById(groupId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the repo (see above)? |
||
const user = await this.userService.findById(userId); | ||
// user must have an id, because we are fetching it by id -> fix in service | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const groupUser = new GroupUser({ roleId: role.id, userId: user.id! }); | ||
|
||
group.addUser(groupUser); | ||
await this.save(group); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the repo (see above)? |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW I'm wondering why we aren't using the domain enum for the group type in the response. That should actually be possible and valid IMHO. But that's not a subject of this PR.