mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-14 00:31:12 +10:00
fix groups
This commit is contained in:
@ -1,4 +1,9 @@
|
|||||||
import { PartialType } from '@nestjs/mapped-types';
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
import { CreateGroupDto } from './create-group.dto';
|
import { CreateGroupDto } from './create-group.dto';
|
||||||
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
export class UpdateGroupDto extends PartialType(CreateGroupDto) {}
|
export class UpdateGroupDto extends PartialType(CreateGroupDto) {
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsUUID()
|
||||||
|
groupId: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -47,4 +47,6 @@ export class Group {
|
|||||||
|
|
||||||
@OneToMany(() => GroupUser, (groupUser) => groupUser.group)
|
@OneToMany(() => GroupUser, (groupUser) => groupUser.group)
|
||||||
groupUsers: GroupUser[];
|
groupUsers: GroupUser[];
|
||||||
|
|
||||||
|
userCount?: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,20 +6,20 @@ import {
|
|||||||
import { DataSource, EntityManager } from 'typeorm';
|
import { DataSource, EntityManager } from 'typeorm';
|
||||||
import { GroupUserRepository } from '../respositories/group-user.repository';
|
import { GroupUserRepository } from '../respositories/group-user.repository';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
||||||
import {
|
import { WorkspaceUser } from '../../workspace/entities/workspace-user.entity';
|
||||||
WorkspaceUser,
|
|
||||||
WorkspaceUserRole,
|
|
||||||
} from '../../workspace/entities/workspace-user.entity';
|
|
||||||
import { transactionWrapper } from '../../../helpers/db.helper';
|
import { transactionWrapper } from '../../../helpers/db.helper';
|
||||||
import { User } from '../../user/entities/user.entity';
|
import { User } from '../../user/entities/user.entity';
|
||||||
import { GroupUser } from '../entities/group-user.entity';
|
import { GroupUser } from '../entities/group-user.entity';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
||||||
|
import { Group } from '../entities/group.entity';
|
||||||
|
import { GroupService } from './group.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupUserService {
|
export class GroupUserService {
|
||||||
constructor(
|
constructor(
|
||||||
private groupUserRepository: GroupUserRepository,
|
private groupUserRepository: GroupUserRepository,
|
||||||
|
private groupService: GroupService,
|
||||||
private dataSource: DataSource,
|
private dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@ -28,9 +28,12 @@ export class GroupUserService {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
paginationOptions: PaginationOptions,
|
||||||
): Promise<PaginatedResult<User>> {
|
): Promise<PaginatedResult<User>> {
|
||||||
|
await this.groupService.validateGroup(groupId, workspaceId);
|
||||||
|
|
||||||
const [groupUsers, count] = await this.groupUserRepository.findAndCount({
|
const [groupUsers, count] = await this.groupUserRepository.findAndCount({
|
||||||
relations: ['user'],
|
relations: ['user'],
|
||||||
where: {
|
where: {
|
||||||
|
groupId: groupId,
|
||||||
group: {
|
group: {
|
||||||
workspaceId: workspaceId,
|
workspaceId: workspaceId,
|
||||||
},
|
},
|
||||||
@ -57,7 +60,15 @@ export class GroupUserService {
|
|||||||
|
|
||||||
await transactionWrapper(
|
await transactionWrapper(
|
||||||
async (manager) => {
|
async (manager) => {
|
||||||
// TODO: make duplicate code reusable
|
const group = await manager.findOneBy(Group, {
|
||||||
|
id: groupId,
|
||||||
|
workspaceId: workspaceId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
throw new NotFoundException('Group not found');
|
||||||
|
}
|
||||||
|
|
||||||
const userExists = await manager.exists(User, {
|
const userExists = await manager.exists(User, {
|
||||||
where: { id: userId },
|
where: { id: userId },
|
||||||
});
|
});
|
||||||
@ -100,7 +111,7 @@ export class GroupUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async removeUserFromGroup(userId: string, groupId: string): Promise<void> {
|
async removeUserFromGroup(userId: string, groupId: string): Promise<void> {
|
||||||
const groupUser = await this.findGroupUser(userId, groupId);
|
const groupUser = await this.getGroupUser(userId, groupId);
|
||||||
|
|
||||||
if (!groupUser) {
|
if (!groupUser) {
|
||||||
throw new BadRequestException('Group member not found');
|
throw new BadRequestException('Group member not found');
|
||||||
@ -112,10 +123,18 @@ export class GroupUserService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findGroupUser(userId: string, groupId: string): Promise<GroupUser> {
|
async getGroupUser(userId: string, groupId: string): Promise<GroupUser> {
|
||||||
return await this.groupUserRepository.findOneBy({
|
return await this.groupUserRepository.findOneBy({
|
||||||
userId,
|
userId,
|
||||||
groupId,
|
groupId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getGroupUserCount(groupId: string): Promise<number> {
|
||||||
|
return await this.groupUserRepository.count({
|
||||||
|
where: {
|
||||||
|
groupId: groupId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,14 @@ export class GroupService {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
updateGroupDto: UpdateGroupDto,
|
updateGroupDto: UpdateGroupDto,
|
||||||
): Promise<Group> {
|
): Promise<Group> {
|
||||||
const group = new Group();
|
const group = await this.groupRepository.findOneBy({
|
||||||
|
id: updateGroupDto.groupId,
|
||||||
|
workspaceId: workspaceId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
throw new NotFoundException('Group not found');
|
||||||
|
}
|
||||||
|
|
||||||
if (updateGroupDto.name) {
|
if (updateGroupDto.name) {
|
||||||
group.name = updateGroupDto.name;
|
group.name = updateGroupDto.name;
|
||||||
@ -43,12 +50,16 @@ export class GroupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getGroup(groupId: string, workspaceId: string): Promise<Group> {
|
async getGroup(groupId: string, workspaceId: string): Promise<Group> {
|
||||||
const group = await this.groupRepository.findOneBy({
|
const group = await this.groupRepository
|
||||||
id: groupId,
|
.createQueryBuilder('group')
|
||||||
workspaceId: workspaceId,
|
.where('group.id = :groupId', { groupId })
|
||||||
});
|
.andWhere('group.workspaceId = :workspaceId', { workspaceId })
|
||||||
|
.loadRelationCountAndMap(
|
||||||
//TODO: get group member count
|
'group.userCount',
|
||||||
|
'group.groupUsers',
|
||||||
|
'groupUsers',
|
||||||
|
)
|
||||||
|
.getOne();
|
||||||
|
|
||||||
if (!group) {
|
if (!group) {
|
||||||
throw new NotFoundException('Group not found');
|
throw new NotFoundException('Group not found');
|
||||||
@ -61,22 +72,37 @@ export class GroupService {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
paginationOptions: PaginationOptions,
|
||||||
): Promise<PaginatedResult<Group>> {
|
): Promise<PaginatedResult<Group>> {
|
||||||
const [groupsInWorkspace, count] = await this.groupRepository.findAndCount({
|
const [groupsInWorkspace, count] = await this.groupRepository
|
||||||
where: {
|
.createQueryBuilder('group')
|
||||||
workspaceId: workspaceId,
|
.where('group.workspaceId = :workspaceId', { workspaceId })
|
||||||
},
|
.loadRelationCountAndMap(
|
||||||
|
'group.userCount',
|
||||||
take: paginationOptions.limit,
|
'group.groupUsers',
|
||||||
skip: paginationOptions.skip,
|
'groupUsers',
|
||||||
});
|
)
|
||||||
|
.take(paginationOptions.limit)
|
||||||
|
.skip(paginationOptions.skip)
|
||||||
|
.getManyAndCount();
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
||||||
|
|
||||||
return new PaginatedResult(groupsInWorkspace, paginationMeta);
|
return new PaginatedResult(groupsInWorkspace, paginationMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteGroup(groupId: string, workspaceId: string) {
|
async deleteGroup(groupId: string, workspaceId: string): Promise<void> {
|
||||||
await this.getGroup(groupId, workspaceId);
|
await this.validateGroup(groupId, workspaceId);
|
||||||
await this.groupRepository.delete(groupId);
|
await this.groupRepository.delete(groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async validateGroup(groupId: string, workspaceId: string): Promise<void> {
|
||||||
|
const groupExists = await this.groupRepository.exists({
|
||||||
|
where: {
|
||||||
|
id: groupId,
|
||||||
|
workspaceId: workspaceId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!groupExists) {
|
||||||
|
throw new NotFoundException('Group not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,10 +60,7 @@ export class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getUserInstance(userId: string) {
|
async getUserInstance(userId: string) {
|
||||||
console.log('what is')
|
|
||||||
|
|
||||||
const user: User = await this.findById(userId);
|
const user: User = await this.findById(userId);
|
||||||
console.log('what one')
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new NotFoundException('User not found');
|
throw new NotFoundException('User not found');
|
||||||
|
|||||||
Reference in New Issue
Block a user