diff --git a/apps/server/src/core/group/services/group-user.service.ts b/apps/server/src/core/group/services/group-user.service.ts index 2913fefd..469f3ed9 100644 --- a/apps/server/src/core/group/services/group-user.service.ts +++ b/apps/server/src/core/group/services/group-user.service.ts @@ -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 { - 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()) diff --git a/apps/server/src/core/group/services/group.service.ts b/apps/server/src/core/group/services/group.service.ts index 62db153a..e0e98a9a 100644 --- a/apps/server/src/core/group/services/group.service.ts +++ b/apps/server/src/core/group/services/group.service.ts @@ -151,8 +151,11 @@ export class GroupService { async findAndValidateGroup( groupId: string, workspaceId: string, + trx?: KyselyTransaction, ): Promise { - 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'); } diff --git a/apps/server/src/database/repos/group/group.repo.ts b/apps/server/src/database/repos/group/group.repo.ts index 67aaa94a..1cc030d4 100644 --- a/apps/server/src/database/repos/group/group.repo.ts +++ b/apps/server/src/database/repos/group/group.repo.ts @@ -51,8 +51,11 @@ export class GroupRepo { updatableGroup: UpdatableGroup, groupId: string, workspaceId: string, + trx?: KyselyTransaction, ): Promise { - await this.db + const db = dbOrTx(this.db, trx); + + await db .updateTable('groups') .set({ ...updatableGroup, updatedAt: new Date() }) .where('id', '=', groupId)