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

@ -2,12 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { dbOrTx } from '@docmost/db/utils';
import {
GroupUser,
InsertableGroupUser,
User,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { GroupUser, InsertableGroupUser } from '@docmost/db/types/entity.types';
import { PaginationOptions } from '../../pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
@ -45,7 +40,7 @@ export class GroupUserRepo {
let query = this.db
.selectFrom('groupUsers')
.innerJoin('users', 'users.id', 'groupUsers.userId')
.select(sql<User>`users.*` as any)
.selectAll('users')
.where('groupId', '=', groupId)
.orderBy('createdAt', 'asc');
@ -55,11 +50,15 @@ export class GroupUserRepo {
);
}
const result = executeWithPagination(query, {
const result = await executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
result.items.map((user) => {
delete user.password;
});
return result;
}

View File

@ -16,38 +16,29 @@ import { executeWithPagination } from '@docmost/db/pagination/pagination';
export class GroupRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Group> = [
'id',
'name',
'description',
'isDefault',
'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> {
async findById(
groupId: string,
workspaceId: string,
opts?: { includeMemberCount: boolean },
): Promise<Group> {
return await this.db
.selectFrom('groups')
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.selectAll('groups')
.$if(opts?.includeMemberCount, (qb) => qb.select(this.withMemberCount))
.where('id', '=', groupId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async findByName(groupName: string, workspaceId: string): Promise<Group> {
async findByName(
groupName: string,
workspaceId: string,
opts?: { includeMemberCount: boolean },
): Promise<Group> {
return await this.db
.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('workspaceId', '=', workspaceId)
.executeTakeFirst();
@ -83,19 +74,24 @@ export class GroupRepo {
trx: KyselyTransaction,
): Promise<Group> {
const db = dbOrTx(this.db, trx);
return db
.selectFrom('groups')
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.where('isDefault', '=', true)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
return (
db
.selectFrom('groups')
.selectAll()
// .select((eb) => this.withMemberCount(eb))
.where('isDefault', '=', true)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst()
);
}
async getGroupsPaginated(workspaceId: string, pagination: PaginationOptions) {
let query = this.db
.selectFrom('groups')
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.selectAll('groups')
.select((eb) => this.withMemberCount(eb))
.where('workspaceId', '=', workspaceId)
.orderBy('memberCount', 'desc')
.orderBy('createdAt', 'asc');
if (pagination.query) {
@ -116,6 +112,14 @@ export class GroupRepo {
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> {
await this.db
.deleteFrom('groups')