fix groups

This commit is contained in:
Philipinho
2024-03-06 17:18:37 +00:00
parent 528b9d70b1
commit 3e174b3838
5 changed files with 77 additions and 28 deletions

View File

@ -1,4 +1,9 @@
import { PartialType } from '@nestjs/mapped-types';
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;
}

View File

@ -47,4 +47,6 @@ export class Group {
@OneToMany(() => GroupUser, (groupUser) => groupUser.group)
groupUsers: GroupUser[];
userCount?: number;
}

View File

@ -6,20 +6,20 @@ import {
import { DataSource, EntityManager } from 'typeorm';
import { GroupUserRepository } from '../respositories/group-user.repository';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
import {
WorkspaceUser,
WorkspaceUserRole,
} from '../../workspace/entities/workspace-user.entity';
import { WorkspaceUser } from '../../workspace/entities/workspace-user.entity';
import { transactionWrapper } from '../../../helpers/db.helper';
import { User } from '../../user/entities/user.entity';
import { GroupUser } from '../entities/group-user.entity';
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
import { Group } from '../entities/group.entity';
import { GroupService } from './group.service';
@Injectable()
export class GroupUserService {
constructor(
private groupUserRepository: GroupUserRepository,
private groupService: GroupService,
private dataSource: DataSource,
) {}
@ -28,9 +28,12 @@ export class GroupUserService {
workspaceId: string,
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<User>> {
await this.groupService.validateGroup(groupId, workspaceId);
const [groupUsers, count] = await this.groupUserRepository.findAndCount({
relations: ['user'],
where: {
groupId: groupId,
group: {
workspaceId: workspaceId,
},
@ -57,7 +60,15 @@ export class GroupUserService {
await transactionWrapper(
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, {
where: { id: userId },
});
@ -100,7 +111,7 @@ export class GroupUserService {
}
async removeUserFromGroup(userId: string, groupId: string): Promise<void> {
const groupUser = await this.findGroupUser(userId, groupId);
const groupUser = await this.getGroupUser(userId, groupId);
if (!groupUser) {
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({
userId,
groupId,
});
}
async getGroupUserCount(groupId: string): Promise<number> {
return await this.groupUserRepository.count({
where: {
groupId: groupId,
},
});
}
}

View File

@ -29,7 +29,14 @@ export class GroupService {
workspaceId: string,
updateGroupDto: UpdateGroupDto,
): 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) {
group.name = updateGroupDto.name;
@ -43,12 +50,16 @@ export class GroupService {
}
async getGroup(groupId: string, workspaceId: string): Promise<Group> {
const group = await this.groupRepository.findOneBy({
id: groupId,
workspaceId: workspaceId,
});
//TODO: get group member count
const group = await this.groupRepository
.createQueryBuilder('group')
.where('group.id = :groupId', { groupId })
.andWhere('group.workspaceId = :workspaceId', { workspaceId })
.loadRelationCountAndMap(
'group.userCount',
'group.groupUsers',
'groupUsers',
)
.getOne();
if (!group) {
throw new NotFoundException('Group not found');
@ -61,22 +72,37 @@ export class GroupService {
workspaceId: string,
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<Group>> {
const [groupsInWorkspace, count] = await this.groupRepository.findAndCount({
where: {
workspaceId: workspaceId,
},
take: paginationOptions.limit,
skip: paginationOptions.skip,
});
const [groupsInWorkspace, count] = await this.groupRepository
.createQueryBuilder('group')
.where('group.workspaceId = :workspaceId', { workspaceId })
.loadRelationCountAndMap(
'group.userCount',
'group.groupUsers',
'groupUsers',
)
.take(paginationOptions.limit)
.skip(paginationOptions.skip)
.getManyAndCount();
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(groupsInWorkspace, paginationMeta);
}
async deleteGroup(groupId: string, workspaceId: string) {
await this.getGroup(groupId, workspaceId);
async deleteGroup(groupId: string, workspaceId: string): Promise<void> {
await this.validateGroup(groupId, workspaceId);
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');
}
}
}

View File

@ -60,10 +60,7 @@ export class UserService {
}
async getUserInstance(userId: string) {
console.log('what is')
const user: User = await this.findById(userId);
console.log('what one')
if (!user) {
throw new NotFoundException('User not found');