Work on groups

* add batch group member insertion
* allow adding members on group creation
* fixes
This commit is contained in:
Philipinho
2024-04-04 21:17:28 +01:00
parent b58399445e
commit 7d14a353cc
8 changed files with 151 additions and 65 deletions

View File

@ -1,8 +1,16 @@
import { IsNotEmpty, IsUUID } from 'class-validator';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsUUID } from 'class-validator';
import { GroupIdDto } from './group-id.dto';
export class AddGroupUserDto extends GroupIdDto {
@IsNotEmpty()
@IsUUID()
userId: string;
// @IsOptional()
// @IsUUID()
// userId: string;
@IsArray()
@ArrayMaxSize(50, {
message: 'userIds must an array with no more than 50 elements',
})
@ArrayMinSize(1)
@IsUUID(4, { each: true })
userIds: string[];
}

View File

@ -1,16 +1,31 @@
import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
import {
ArrayMaxSize,
IsArray,
IsOptional,
IsString,
IsUUID,
MaxLength,
MinLength,
} from 'class-validator';
export class CreateGroupDto {
@MinLength(2)
@MaxLength(64)
@MaxLength(50)
@IsString()
name: string;
@IsOptional()
@IsString()
description?: string;
@IsOptional()
@IsArray()
@ArrayMaxSize(50)
@IsUUID(4, { each: true })
userIds?: string[];
}
export enum DefaultGroup {
EVERYONE = 'internal_users',
EVERYONE = 'Everyone',
DESCRIPTION = 'Group for all users in this workspace.',
}

View File

@ -1,3 +1,7 @@
import { AddGroupUserDto } from './add-group-user.dto';
import { GroupIdDto } from './group-id.dto';
import { IsUUID } from 'class-validator';
export class RemoveGroupUserDto extends AddGroupUserDto {}
export class RemoveGroupUserDto extends GroupIdDto {
@IsUUID()
userId: string;
}

View File

@ -12,7 +12,7 @@ import { AuthUser } from '../../decorators/auth-user.decorator';
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
import { GroupUserService } from './services/group-user.service';
import { GroupIdDto } from './dto/group-id.dto';
import { PaginationOptions } from '../../kysely/pagination/pagination-options';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { AddGroupUserDto } from './dto/add-group-user.dto';
import { RemoveGroupUserDto } from './dto/remove-group-user.dto';
import { UpdateGroupDto } from './dto/update-group.dto';
@ -62,6 +62,7 @@ export class GroupController {
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
console.log(createGroupDto);
return this.groupService.createGroup(user, workspace.id, createGroupDto);
}
@ -104,8 +105,8 @@ export class GroupController {
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
return this.groupUserService.addUserToGroup(
addGroupUserDto.userId,
return this.groupUserService.addUsersToGroupBatch(
addGroupUserDto.userIds,
addGroupUserDto.groupId,
workspace.id,
);

View File

@ -1,9 +1,11 @@
import {
BadRequestException,
forwardRef,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { GroupService } from './group.service';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
@ -17,8 +19,9 @@ export class GroupUserService {
constructor(
private groupRepo: GroupRepo,
private groupUserRepo: GroupUserRepo,
private groupService: GroupService,
private userRepo: UserRepo,
@Inject(forwardRef(() => GroupService))
private groupService: GroupService,
@InjectKysely() private readonly db: KyselyDB,
) {}
@ -55,6 +58,38 @@ export class GroupUserService {
);
}
async addUsersToGroupBatch(
userIds: string[],
groupId: string,
workspaceId: string,
): Promise<void> {
await this.groupService.findAndValidateGroup(groupId, workspaceId);
// make sure we have valid workspace users
const validUsers = await this.db
.selectFrom('users')
.select(['id', 'name'])
.where('users.id', 'in', userIds)
.where('users.workspaceId', '=', workspaceId)
.execute();
// prepare users to add to group
const groupUsersToInsert = [];
for (const user of validUsers) {
groupUsersToInsert.push({
userId: user.id,
groupId: groupId,
});
}
// batch insert new group users
await this.db
.insertInto('groupUsers')
.values(groupUsersToInsert)
.onConflict((oc) => oc.columns(['userId', 'groupId']).doNothing())
.execute();
}
async addUserToGroup(
userId: string,
groupId: string,

View File

@ -1,19 +1,38 @@
import {
BadRequestException,
forwardRef,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CreateGroupDto, DefaultGroup } from '../dto/create-group.dto';
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { UpdateGroupDto } from '../dto/update-group.dto';
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
import { Group, InsertableGroup, User } from '@docmost/db/types/entity.types';
import { PaginationResult } from '@docmost/db/pagination/pagination';
import { GroupUserService } from './group-user.service';
@Injectable()
export class GroupService {
constructor(private groupRepo: GroupRepo) {}
constructor(
private groupRepo: GroupRepo,
@Inject(forwardRef(() => GroupUserService))
private groupUserService: GroupUserService,
) {}
async getGroupInfo(groupId: string, workspaceId: string): Promise<Group> {
const group = await this.groupRepo.findById(groupId, workspaceId, {
includeMemberCount: true,
});
if (!group) {
throw new NotFoundException('Group not found');
}
return group;
}
async createGroup(
authUser: User,
@ -36,7 +55,17 @@ export class GroupService {
workspaceId: workspaceId,
};
return await this.groupRepo.insertGroup(insertableGroup, trx);
const createdGroup = await this.groupRepo.insertGroup(insertableGroup, trx);
if (createGroupDto?.userIds && createGroupDto.userIds.length > 0) {
await this.groupUserService.addUsersToGroupBatch(
createGroupDto.userIds,
createdGroup.id,
workspaceId,
);
}
return createdGroup;
}
async createDefaultGroup(
@ -60,6 +89,7 @@ export class GroupService {
const group = await this.groupRepo.findById(
updateGroupDto.groupId,
workspaceId,
{ includeMemberCount: true },
);
if (!group) {
@ -99,16 +129,6 @@ export class GroupService {
return group;
}
async getGroupInfo(groupId: string, workspaceId: string): Promise<Group> {
const group = await this.groupRepo.findById(groupId, workspaceId);
if (!group) {
throw new NotFoundException('Group not found');
}
return group;
}
async getWorkspaceGroups(
workspaceId: string,
paginationOptions: PaginationOptions,