mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-13 08:12:32 +10:00
Work on groups
* add batch group member insertion * allow adding members on group creation * fixes
This commit is contained in:
@ -1,8 +1,16 @@
|
|||||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
import { ArrayMaxSize, ArrayMinSize, IsArray, IsUUID } from 'class-validator';
|
||||||
import { GroupIdDto } from './group-id.dto';
|
import { GroupIdDto } from './group-id.dto';
|
||||||
|
|
||||||
export class AddGroupUserDto extends GroupIdDto {
|
export class AddGroupUserDto extends GroupIdDto {
|
||||||
@IsNotEmpty()
|
// @IsOptional()
|
||||||
@IsUUID()
|
// @IsUUID()
|
||||||
userId: string;
|
// userId: string;
|
||||||
|
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMaxSize(50, {
|
||||||
|
message: 'userIds must an array with no more than 50 elements',
|
||||||
|
})
|
||||||
|
@ArrayMinSize(1)
|
||||||
|
@IsUUID(4, { each: true })
|
||||||
|
userIds: string[];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 {
|
export class CreateGroupDto {
|
||||||
@MinLength(2)
|
@MinLength(2)
|
||||||
@MaxLength(64)
|
@MaxLength(50)
|
||||||
@IsString()
|
@IsString()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
description?: string;
|
description?: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMaxSize(50)
|
||||||
|
@IsUUID(4, { each: true })
|
||||||
|
userIds?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum DefaultGroup {
|
export enum DefaultGroup {
|
||||||
EVERYONE = 'internal_users',
|
EVERYONE = 'Everyone',
|
||||||
|
DESCRIPTION = 'Group for all users in this workspace.',
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import { AuthUser } from '../../decorators/auth-user.decorator';
|
|||||||
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
||||||
import { GroupUserService } from './services/group-user.service';
|
import { GroupUserService } from './services/group-user.service';
|
||||||
import { GroupIdDto } from './dto/group-id.dto';
|
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 { AddGroupUserDto } from './dto/add-group-user.dto';
|
||||||
import { RemoveGroupUserDto } from './dto/remove-group-user.dto';
|
import { RemoveGroupUserDto } from './dto/remove-group-user.dto';
|
||||||
import { UpdateGroupDto } from './dto/update-group.dto';
|
import { UpdateGroupDto } from './dto/update-group.dto';
|
||||||
@ -62,6 +62,7 @@ export class GroupController {
|
|||||||
@AuthUser() user: User,
|
@AuthUser() user: User,
|
||||||
@AuthWorkspace() workspace: Workspace,
|
@AuthWorkspace() workspace: Workspace,
|
||||||
) {
|
) {
|
||||||
|
console.log(createGroupDto);
|
||||||
return this.groupService.createGroup(user, workspace.id, createGroupDto);
|
return this.groupService.createGroup(user, workspace.id, createGroupDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,8 +105,8 @@ export class GroupController {
|
|||||||
@AuthUser() user: User,
|
@AuthUser() user: User,
|
||||||
@AuthWorkspace() workspace: Workspace,
|
@AuthWorkspace() workspace: Workspace,
|
||||||
) {
|
) {
|
||||||
return this.groupUserService.addUserToGroup(
|
return this.groupUserService.addUsersToGroupBatch(
|
||||||
addGroupUserDto.userId,
|
addGroupUserDto.userIds,
|
||||||
addGroupUserDto.groupId,
|
addGroupUserDto.groupId,
|
||||||
workspace.id,
|
workspace.id,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
forwardRef,
|
||||||
|
Inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { GroupService } from './group.service';
|
import { GroupService } from './group.service';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { executeTx } from '@docmost/db/utils';
|
||||||
@ -17,8 +19,9 @@ export class GroupUserService {
|
|||||||
constructor(
|
constructor(
|
||||||
private groupRepo: GroupRepo,
|
private groupRepo: GroupRepo,
|
||||||
private groupUserRepo: GroupUserRepo,
|
private groupUserRepo: GroupUserRepo,
|
||||||
private groupService: GroupService,
|
|
||||||
private userRepo: UserRepo,
|
private userRepo: UserRepo,
|
||||||
|
@Inject(forwardRef(() => GroupService))
|
||||||
|
private groupService: GroupService,
|
||||||
@InjectKysely() private readonly db: KyselyDB,
|
@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(
|
async addUserToGroup(
|
||||||
userId: string,
|
userId: string,
|
||||||
groupId: string,
|
groupId: string,
|
||||||
|
|||||||
@ -1,19 +1,38 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
forwardRef,
|
||||||
|
Inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { CreateGroupDto, DefaultGroup } from '../dto/create-group.dto';
|
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 { UpdateGroupDto } from '../dto/update-group.dto';
|
||||||
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
||||||
import { Group, InsertableGroup, User } from '@docmost/db/types/entity.types';
|
import { Group, InsertableGroup, User } from '@docmost/db/types/entity.types';
|
||||||
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
|
import { GroupUserService } from './group-user.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupService {
|
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(
|
async createGroup(
|
||||||
authUser: User,
|
authUser: User,
|
||||||
@ -36,7 +55,17 @@ export class GroupService {
|
|||||||
workspaceId: workspaceId,
|
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(
|
async createDefaultGroup(
|
||||||
@ -60,6 +89,7 @@ export class GroupService {
|
|||||||
const group = await this.groupRepo.findById(
|
const group = await this.groupRepo.findById(
|
||||||
updateGroupDto.groupId,
|
updateGroupDto.groupId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
|
{ includeMemberCount: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!group) {
|
if (!group) {
|
||||||
@ -99,16 +129,6 @@ export class GroupService {
|
|||||||
return group;
|
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(
|
async getWorkspaceGroups(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
paginationOptions: PaginationOptions,
|
||||||
|
|||||||
@ -2,12 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { dbOrTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import { GroupUser, InsertableGroupUser } from '@docmost/db/types/entity.types';
|
||||||
GroupUser,
|
|
||||||
InsertableGroupUser,
|
|
||||||
User,
|
|
||||||
} from '@docmost/db/types/entity.types';
|
|
||||||
import { sql } from 'kysely';
|
|
||||||
import { PaginationOptions } from '../../pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@ -45,7 +40,7 @@ export class GroupUserRepo {
|
|||||||
let query = this.db
|
let query = this.db
|
||||||
.selectFrom('groupUsers')
|
.selectFrom('groupUsers')
|
||||||
.innerJoin('users', 'users.id', 'groupUsers.userId')
|
.innerJoin('users', 'users.id', 'groupUsers.userId')
|
||||||
.select(sql<User>`users.*` as any)
|
.selectAll('users')
|
||||||
.where('groupId', '=', groupId)
|
.where('groupId', '=', groupId)
|
||||||
.orderBy('createdAt', 'asc');
|
.orderBy('createdAt', 'asc');
|
||||||
|
|
||||||
@ -55,11 +50,15 @@ export class GroupUserRepo {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = executeWithPagination(query, {
|
const result = await executeWithPagination(query, {
|
||||||
page: pagination.page,
|
page: pagination.page,
|
||||||
perPage: pagination.limit,
|
perPage: pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
result.items.map((user) => {
|
||||||
|
delete user.password;
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,38 +16,29 @@ import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
|||||||
export class GroupRepo {
|
export class GroupRepo {
|
||||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||||
|
|
||||||
private baseFields: Array<keyof Group> = [
|
async findById(
|
||||||
'id',
|
groupId: string,
|
||||||
'name',
|
workspaceId: string,
|
||||||
'description',
|
opts?: { includeMemberCount: boolean },
|
||||||
'isDefault',
|
): Promise<Group> {
|
||||||
'workspaceId',
|
|
||||||
'creatorId',
|
|
||||||
'createdAt',
|
|
||||||
'updatedAt',
|
|
||||||
];
|
|
||||||
|
|
||||||
countGroupMembers(eb: ExpressionBuilder<DB, 'groups'>) {
|
|
||||||
return eb
|
|
||||||
.selectFrom('groupUsers')
|
|
||||||
.select((eb) => eb.fn.countAll().as('count'))
|
|
||||||
.whereRef('groupUsers.groupId', '=', 'groups.id')
|
|
||||||
.as('memberCount');
|
|
||||||
}
|
|
||||||
|
|
||||||
async findById(groupId: string, workspaceId: string): Promise<Group> {
|
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
.selectAll('groups')
|
||||||
|
.$if(opts?.includeMemberCount, (qb) => qb.select(this.withMemberCount))
|
||||||
.where('id', '=', groupId)
|
.where('id', '=', groupId)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByName(groupName: string, workspaceId: string): Promise<Group> {
|
async findByName(
|
||||||
|
groupName: string,
|
||||||
|
workspaceId: string,
|
||||||
|
opts?: { includeMemberCount: boolean },
|
||||||
|
): Promise<Group> {
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
.selectAll('groups')
|
||||||
|
.$if(opts?.includeMemberCount, (qb) => qb.select(this.withMemberCount))
|
||||||
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
|
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
@ -83,19 +74,24 @@ export class GroupRepo {
|
|||||||
trx: KyselyTransaction,
|
trx: KyselyTransaction,
|
||||||
): Promise<Group> {
|
): Promise<Group> {
|
||||||
const db = dbOrTx(this.db, trx);
|
const db = dbOrTx(this.db, trx);
|
||||||
return db
|
return (
|
||||||
|
db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
.selectAll()
|
||||||
|
// .select((eb) => this.withMemberCount(eb))
|
||||||
.where('isDefault', '=', true)
|
.where('isDefault', '=', true)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupsPaginated(workspaceId: string, pagination: PaginationOptions) {
|
async getGroupsPaginated(workspaceId: string, pagination: PaginationOptions) {
|
||||||
let query = this.db
|
let query = this.db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
.selectAll('groups')
|
||||||
|
.select((eb) => this.withMemberCount(eb))
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.orderBy('memberCount', 'desc')
|
||||||
.orderBy('createdAt', 'asc');
|
.orderBy('createdAt', 'asc');
|
||||||
|
|
||||||
if (pagination.query) {
|
if (pagination.query) {
|
||||||
@ -116,6 +112,14 @@ export class GroupRepo {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withMemberCount(eb: ExpressionBuilder<DB, 'groups'>) {
|
||||||
|
return eb
|
||||||
|
.selectFrom('groupUsers')
|
||||||
|
.select((eb) => eb.fn.countAll().as('count'))
|
||||||
|
.whereRef('groupUsers.groupId', '=', 'groups.id')
|
||||||
|
.as('memberCount');
|
||||||
|
}
|
||||||
|
|
||||||
async delete(groupId: string, workspaceId: string): Promise<void> {
|
async delete(groupId: string, workspaceId: string): Promise<void> {
|
||||||
await this.db
|
await this.db
|
||||||
.deleteFrom('groups')
|
.deleteFrom('groups')
|
||||||
|
|||||||
Reference in New Issue
Block a user