|
@@ -2,21 +2,39 @@ import { FastifyInstance } from 'fastify'
|
|
|
import { Like, Repository, Not } from 'typeorm'
|
|
import { Like, Repository, Not } from 'typeorm'
|
|
|
import Decimal from 'decimal.js'
|
|
import Decimal from 'decimal.js'
|
|
|
import { SysConfig } from '../entities/sys-config.entity'
|
|
import { SysConfig } from '../entities/sys-config.entity'
|
|
|
-import { CreateSysConfigBody } from '../dto/sys-config.dto'
|
|
|
|
|
-import { UpdateSysConfigBody } from '../dto/sys-config.dto'
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ CreateSysConfigBody,
|
|
|
|
|
+ UpdateSysConfigBody,
|
|
|
|
|
+ CreateTeamConfigBody,
|
|
|
|
|
+ UpdateTeamConfigBody,
|
|
|
|
|
+ ListTeamConfigQuery
|
|
|
|
|
+} from '../dto/sys-config.dto'
|
|
|
import { ConfigType } from '../entities/sys-config.entity'
|
|
import { ConfigType } from '../entities/sys-config.entity'
|
|
|
|
|
+import { User, UserRole } from '../entities/user.entity'
|
|
|
|
|
+import { Team } from '../entities/team.entity'
|
|
|
|
|
+import { TeamMembers } from '../entities/team-members.entity'
|
|
|
|
|
|
|
|
export class SysConfigService {
|
|
export class SysConfigService {
|
|
|
private app: FastifyInstance
|
|
private app: FastifyInstance
|
|
|
private sysConfigRepository: Repository<SysConfig>
|
|
private sysConfigRepository: Repository<SysConfig>
|
|
|
|
|
+ private userRepository: Repository<User>
|
|
|
|
|
+ private teamRepository: Repository<Team>
|
|
|
|
|
+ private teamMembersRepository: Repository<TeamMembers>
|
|
|
|
|
|
|
|
constructor(app: FastifyInstance) {
|
|
constructor(app: FastifyInstance) {
|
|
|
this.app = app
|
|
this.app = app
|
|
|
this.sysConfigRepository = app.dataSource.getRepository(SysConfig)
|
|
this.sysConfigRepository = app.dataSource.getRepository(SysConfig)
|
|
|
|
|
+ this.userRepository = app.dataSource.getRepository(User)
|
|
|
|
|
+ this.teamRepository = app.dataSource.getRepository(Team)
|
|
|
|
|
+ this.teamMembersRepository = app.dataSource.getRepository(TeamMembers)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async getSysConfig(name: string) {
|
|
|
|
|
- const sysConfig = await this.sysConfigRepository.findOneOrFail({ where: { name } })
|
|
|
|
|
|
|
+ async getSysConfig(name: string, teamId?: number) {
|
|
|
|
|
+ const where: any = { name }
|
|
|
|
|
+ if (teamId !== undefined) {
|
|
|
|
|
+ where.teamId = teamId
|
|
|
|
|
+ }
|
|
|
|
|
+ const sysConfig = await this.sysConfigRepository.findOneOrFail({ where })
|
|
|
return sysConfig
|
|
return sysConfig
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -71,7 +89,11 @@ export class SysConfigService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async create(data: CreateSysConfigBody) {
|
|
async create(data: CreateSysConfigBody) {
|
|
|
- const existingConfig = await this.sysConfigRepository.findOne({ where: { name: data.name } })
|
|
|
|
|
|
|
+ const where: any = { name: data.name }
|
|
|
|
|
+ if (data.teamId !== undefined) {
|
|
|
|
|
+ where.teamId = data.teamId
|
|
|
|
|
+ }
|
|
|
|
|
+ const existingConfig = await this.sysConfigRepository.findOne({ where })
|
|
|
if (existingConfig) {
|
|
if (existingConfig) {
|
|
|
throw new Error('配置名称已存在')
|
|
throw new Error('配置名称已存在')
|
|
|
}
|
|
}
|
|
@@ -79,18 +101,18 @@ export class SysConfigService {
|
|
|
return await this.sysConfigRepository.save(config)
|
|
return await this.sysConfigRepository.save(config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async update(name: string, data: UpdateSysConfigBody) {
|
|
|
|
|
- const config = await this.getSysConfig(name)
|
|
|
|
|
|
|
+ async update(name: string, data: UpdateSysConfigBody, teamId?: number) {
|
|
|
|
|
+ const config = await this.getSysConfig(name, teamId)
|
|
|
Object.assign(config, data)
|
|
Object.assign(config, data)
|
|
|
return await this.sysConfigRepository.save(config)
|
|
return await this.sysConfigRepository.save(config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async delete(name: string) {
|
|
|
|
|
- const config = await this.getSysConfig(name)
|
|
|
|
|
|
|
+ async delete(name: string, teamId?: number) {
|
|
|
|
|
+ const config = await this.getSysConfig(name, teamId)
|
|
|
return await this.sysConfigRepository.remove(config)
|
|
return await this.sysConfigRepository.remove(config)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async list(page: number = 0, size: number = 20, name?: string, type?: ConfigType) {
|
|
|
|
|
|
|
+ async list(page: number = 0, size: number = 20, name?: string, type?: ConfigType, teamId?: number) {
|
|
|
const where: any = {
|
|
const where: any = {
|
|
|
name: Not('sensitive_words')
|
|
name: Not('sensitive_words')
|
|
|
}
|
|
}
|
|
@@ -103,6 +125,10 @@ export class SysConfigService {
|
|
|
where.type = type
|
|
where.type = type
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (teamId !== undefined) {
|
|
|
|
|
+ where.teamId = teamId
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const [data, total] = await this.sysConfigRepository.findAndCount({
|
|
const [data, total] = await this.sysConfigRepository.findAndCount({
|
|
|
where,
|
|
where,
|
|
|
skip: page * size,
|
|
skip: page * size,
|
|
@@ -126,4 +152,116 @@ export class SysConfigService {
|
|
|
async getConfigTypes() {
|
|
async getConfigTypes() {
|
|
|
return Object.values(ConfigType)
|
|
return Object.values(ConfigType)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 获取用户的团队ID
|
|
|
|
|
+ async getUserTeamId(userId: number, userRole: UserRole): Promise<number> {
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ throw new Error('管理员不需要团队ID')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (userRole === UserRole.USER) {
|
|
|
|
|
+ throw new Error('普通用户无权限访问团队配置')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (userRole === UserRole.TEAM) {
|
|
|
|
|
+ // 队长从team表中获取teamId
|
|
|
|
|
+ const team = await this.teamRepository.findOne({ where: { userId } })
|
|
|
|
|
+ if (!team) {
|
|
|
|
|
+ throw new Error('未找到该用户的团队信息')
|
|
|
|
|
+ }
|
|
|
|
|
+ return team.id
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 队员从team-members表中获取teamId
|
|
|
|
|
+ const teamMember = await this.teamMembersRepository.findOne({ where: { userId } })
|
|
|
|
|
+ if (!teamMember) {
|
|
|
|
|
+ throw new Error('未找到该用户的团队成员信息')
|
|
|
|
|
+ }
|
|
|
|
|
+ return teamMember.teamId
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 团队配置相关方法
|
|
|
|
|
+ async createTeamConfig(data: CreateTeamConfigBody, userId: number, userRole: UserRole, adminTeamId?: number) {
|
|
|
|
|
+ let teamId: number
|
|
|
|
|
+
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ if (adminTeamId === undefined) {
|
|
|
|
|
+ throw new Error('管理员操作团队配置时必须指定teamId')
|
|
|
|
|
+ }
|
|
|
|
|
+ teamId = adminTeamId
|
|
|
|
|
+ } else {
|
|
|
|
|
+ teamId = await this.getUserTeamId(userId, userRole)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const existingConfig = await this.sysConfigRepository.findOne({
|
|
|
|
|
+ where: { name: data.name, teamId }
|
|
|
|
|
+ })
|
|
|
|
|
+ if (existingConfig) {
|
|
|
|
|
+ throw new Error('该团队配置名称已存在')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const config = this.sysConfigRepository.create({ ...data, teamId })
|
|
|
|
|
+ return await this.sysConfigRepository.save(config)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async updateTeamConfig(
|
|
|
|
|
+ name: string,
|
|
|
|
|
+ data: UpdateTeamConfigBody,
|
|
|
|
|
+ userId: number,
|
|
|
|
|
+ userRole: UserRole,
|
|
|
|
|
+ adminTeamId?: number
|
|
|
|
|
+ ) {
|
|
|
|
|
+ let teamId: number
|
|
|
|
|
+
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ if (adminTeamId === undefined) {
|
|
|
|
|
+ throw new Error('管理员操作团队配置时必须指定teamId')
|
|
|
|
|
+ }
|
|
|
|
|
+ teamId = adminTeamId
|
|
|
|
|
+ } else {
|
|
|
|
|
+ teamId = await this.getUserTeamId(userId, userRole)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const config = await this.getSysConfig(name, teamId)
|
|
|
|
|
+ Object.assign(config, data)
|
|
|
|
|
+ return await this.sysConfigRepository.save(config)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async deleteTeamConfig(name: string, userId: number, userRole: UserRole, adminTeamId?: number) {
|
|
|
|
|
+ let teamId: number
|
|
|
|
|
+
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ if (adminTeamId === undefined) {
|
|
|
|
|
+ throw new Error('管理员操作团队配置时必须指定teamId')
|
|
|
|
|
+ }
|
|
|
|
|
+ teamId = adminTeamId
|
|
|
|
|
+ } else {
|
|
|
|
|
+ teamId = await this.getUserTeamId(userId, userRole)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const config = await this.getSysConfig(name, teamId)
|
|
|
|
|
+ return await this.sysConfigRepository.remove(config)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async getTeamConfig(name: string, userId: number, userRole: UserRole, adminTeamId?: number) {
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ // 管理员查询:如果指定了teamId则查询特定团队,否则查询所有团队
|
|
|
|
|
+ return await this.getSysConfig(name, adminTeamId)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const teamId = await this.getUserTeamId(userId, userRole)
|
|
|
|
|
+ return await this.getSysConfig(name, teamId)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async listTeamConfigs(query: ListTeamConfigQuery, userId: number, userRole: UserRole, adminTeamId?: number) {
|
|
|
|
|
+ if (userRole === UserRole.ADMIN) {
|
|
|
|
|
+ // 管理员查询:如果指定了teamId则查询特定团队,否则查询所有团队
|
|
|
|
|
+ const { page = 0, size = 20, name, type } = query
|
|
|
|
|
+ return await this.list(page, size, name, type, adminTeamId)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const teamId = await this.getUserTeamId(userId, userRole)
|
|
|
|
|
+ const { page = 0, size = 20, name, type } = query
|
|
|
|
|
+ return await this.list(page, size, name, type, teamId)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|