server: refactor pagination

* fix transaction usgae in repos
* other bug fixes
This commit is contained in:
Philipinho
2024-04-01 01:23:52 +01:00
parent ade3a5b589
commit 4913975e99
38 changed files with 648 additions and 756 deletions

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 '../../helpers/pagination/pagination-options';
import { PaginationOptions } from '../../kysely/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';

View File

@ -1,14 +1,16 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
import { GroupService } from './group.service';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import { InjectKysely } from 'nestjs-kysely';
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
import { User } from '@docmost/db/types/entity.types';
import { UserRepo } from '@docmost/db/repos/user/user.repo';
@Injectable()
export class GroupUserService {
@ -16,24 +18,23 @@ export class GroupUserService {
private groupRepo: GroupRepo,
private groupUserRepo: GroupUserRepo,
private groupService: GroupService,
private userRepo: UserRepo,
@InjectKysely() private readonly db: KyselyDB,
) {}
async getGroupUsers(
groupId: string,
workspaceId: string,
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<User>> {
pagination: PaginationOptions,
) {
await this.groupService.findAndValidateGroup(groupId, workspaceId);
const { users, count } = await this.groupUserRepo.getGroupUsersPaginated(
const groupUsers = await this.groupUserRepo.getGroupUsersPaginated(
groupId,
paginationOptions,
pagination,
);
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(users, paginationMeta);
return groupUsers;
}
async addUserToDefaultGroup(
@ -63,7 +64,18 @@ export class GroupUserService {
await executeTx(
this.db,
async (trx) => {
// await this.groupService.findAndValidateGroup(groupId, workspaceId);
await this.groupService.findAndValidateGroup(groupId, workspaceId);
const user = await this.userRepo.findById(
userId,
workspaceId,
false,
trx,
);
if (!user) {
throw new NotFoundException('User not found');
}
const groupUserExists = await this.groupUserRepo.getGroupUserById(
userId,
groupId,
@ -98,6 +110,12 @@ export class GroupUserService {
workspaceId,
);
const user = await this.userRepo.findById(userId, workspaceId);
if (!user) {
throw new NotFoundException('User not found');
}
if (group.isDefault) {
throw new BadRequestException(
'You cannot remove users from a default group',

View File

@ -4,13 +4,12 @@ import {
NotFoundException,
} from '@nestjs/common';
import { CreateGroupDto, DefaultGroup } from '../dto/create-group.dto';
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
import { PaginationOptions } from '../../../kysely/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';
@Injectable()
export class GroupService {
@ -71,15 +70,16 @@ export class GroupService {
throw new BadRequestException('You cannot update a default group');
}
const groupExists = await this.groupRepo.findByName(
updateGroupDto.name,
workspaceId,
);
if (groupExists) {
throw new BadRequestException('Group name already exists');
}
if (updateGroupDto.name) {
const existingGroup = await this.groupRepo.findByName(
updateGroupDto.name,
workspaceId,
);
if (existingGroup && group.name !== existingGroup.name) {
throw new BadRequestException('Group name already exists');
}
group.name = updateGroupDto.name;
}
@ -100,7 +100,6 @@ export class GroupService {
}
async getGroupInfo(groupId: string, workspaceId: string): Promise<Group> {
// todo: add member count
const group = await this.groupRepo.findById(groupId, workspaceId);
if (!group) {
@ -113,15 +112,12 @@ export class GroupService {
async getWorkspaceGroups(
workspaceId: string,
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<Group>> {
const { groups, count } = await this.groupRepo.getGroupsPaginated(
): Promise<PaginationResult<Group>> {
const groups = await this.groupRepo.getGroupsPaginated(
workspaceId,
paginationOptions,
);
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(groups, paginationMeta);
return groups;
}
async deleteGroup(groupId: string, workspaceId: string): Promise<void> {