mirror of
https://github.com/docmost/docmost.git
synced 2025-11-13 18:32:39 +10:00
accept db transaction
This commit is contained in:
@ -7,10 +7,11 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||
import { GroupService } from './group.service';
|
||||
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
|
||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||
import { dbOrTx } from '@docmost/db/utils';
|
||||
|
||||
@Injectable()
|
||||
export class GroupUserService {
|
||||
@ -41,17 +42,21 @@ export class GroupUserService {
|
||||
userIds: string[],
|
||||
groupId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await this.groupService.findAndValidateGroup(groupId, workspaceId);
|
||||
const db = dbOrTx(this.db, trx);
|
||||
await this.groupService.findAndValidateGroup(groupId, workspaceId, trx);
|
||||
|
||||
// make sure we have valid workspace users
|
||||
const validUsers = await this.db
|
||||
const validUsers = await db
|
||||
.selectFrom('users')
|
||||
.select(['id', 'name'])
|
||||
.where('users.id', 'in', userIds)
|
||||
.where('users.workspaceId', '=', workspaceId)
|
||||
.execute();
|
||||
|
||||
if (validUsers.length <= 0) return;
|
||||
|
||||
// prepare users to add to group
|
||||
const groupUsersToInsert = [];
|
||||
for (const user of validUsers) {
|
||||
@ -62,7 +67,7 @@ export class GroupUserService {
|
||||
}
|
||||
|
||||
// batch insert new group users
|
||||
await this.db
|
||||
await db
|
||||
.insertInto('groupUsers')
|
||||
.values(groupUsersToInsert)
|
||||
.onConflict((oc) => oc.columns(['userId', 'groupId']).doNothing())
|
||||
|
||||
@ -151,8 +151,11 @@ export class GroupService {
|
||||
async findAndValidateGroup(
|
||||
groupId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<Group> {
|
||||
const group = await this.groupRepo.findById(groupId, workspaceId);
|
||||
const group = await this.groupRepo.findById(groupId, workspaceId, {
|
||||
trx,
|
||||
});
|
||||
if (!group) {
|
||||
throw new NotFoundException('Group not found');
|
||||
}
|
||||
|
||||
@ -51,8 +51,11 @@ export class GroupRepo {
|
||||
updatableGroup: UpdatableGroup,
|
||||
groupId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await this.db
|
||||
const db = dbOrTx(this.db, trx);
|
||||
|
||||
await db
|
||||
.updateTable('groups')
|
||||
.set({ ...updatableGroup, updatedAt: new Date() })
|
||||
.where('id', '=', groupId)
|
||||
|
||||
Reference in New Issue
Block a user