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