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