mirror of
https://github.com/docmost/docmost.git
synced 2025-11-22 01:41:09 +10:00
Work on groups
* add batch group member insertion * allow adding members on group creation * fixes
This commit is contained in:
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user