|
@@ -3,17 +3,33 @@ import { FastifyInstance } from 'fastify'
|
|
|
import { Team } from '../entities/team.entity'
|
|
import { Team } from '../entities/team.entity'
|
|
|
import { PaginationResponse } from '../dto/common.dto'
|
|
import { PaginationResponse } from '../dto/common.dto'
|
|
|
import { CreateTeamBody, UpdateTeamBody, ListTeamQuery } from '../dto/team.dto'
|
|
import { CreateTeamBody, UpdateTeamBody, ListTeamQuery } from '../dto/team.dto'
|
|
|
|
|
+import { UserService } from './user.service'
|
|
|
|
|
+import { UserRole } from '../entities/user.entity'
|
|
|
|
|
|
|
|
export class TeamService {
|
|
export class TeamService {
|
|
|
private teamRepository: Repository<Team>
|
|
private teamRepository: Repository<Team>
|
|
|
|
|
+ private userService: UserService
|
|
|
|
|
|
|
|
constructor(app: FastifyInstance) {
|
|
constructor(app: FastifyInstance) {
|
|
|
this.teamRepository = app.dataSource.getRepository(Team)
|
|
this.teamRepository = app.dataSource.getRepository(Team)
|
|
|
|
|
+ this.userService = new UserService(app)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async create(data: CreateTeamBody): Promise<Team> {
|
|
|
|
|
- const team = this.teamRepository.create(data)
|
|
|
|
|
- return this.teamRepository.save(team)
|
|
|
|
|
|
|
+ async create(data: CreateTeamBody, creatorId: number): Promise<Team> {
|
|
|
|
|
+ const { password, ...teamData } = data
|
|
|
|
|
+
|
|
|
|
|
+ const existingUser = await this.userService.findByName(teamData.name)
|
|
|
|
|
+ if (existingUser) {
|
|
|
|
|
+ throw new Error('团队已存在')
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const team = this.teamRepository.create(teamData)
|
|
|
|
|
+ const savedTeam = await this.teamRepository.save(team)
|
|
|
|
|
+
|
|
|
|
|
+ const userPassword = password || 'password123'
|
|
|
|
|
+ await this.userService.create(userPassword, teamData.name, UserRole.TEAM, creatorId)
|
|
|
|
|
+
|
|
|
|
|
+ return savedTeam
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async findById(id: number): Promise<Team> {
|
|
async findById(id: number): Promise<Team> {
|
|
@@ -22,9 +38,9 @@ export class TeamService {
|
|
|
|
|
|
|
|
async findAll(query: ListTeamQuery): Promise<PaginationResponse<Team>> {
|
|
async findAll(query: ListTeamQuery): Promise<PaginationResponse<Team>> {
|
|
|
const { page, size, name } = query
|
|
const { page, size, name } = query
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const where: any = {}
|
|
const where: any = {}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (name) {
|
|
if (name) {
|
|
|
where.name = Like(`%${name}%`)
|
|
where.name = Like(`%${name}%`)
|
|
|
}
|
|
}
|
|
@@ -58,13 +74,13 @@ export class TeamService {
|
|
|
|
|
|
|
|
async updateRevenue(id: number, amount: number, type: 'total' | 'today'): Promise<Team> {
|
|
async updateRevenue(id: number, amount: number, type: 'total' | 'today'): Promise<Team> {
|
|
|
const team = await this.findById(id)
|
|
const team = await this.findById(id)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (type === 'total') {
|
|
if (type === 'total') {
|
|
|
team.totalRevenue = Number(team.totalRevenue) + amount
|
|
team.totalRevenue = Number(team.totalRevenue) + amount
|
|
|
} else {
|
|
} else {
|
|
|
team.todayRevenue = Number(team.todayRevenue) + amount
|
|
team.todayRevenue = Number(team.todayRevenue) + amount
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return this.teamRepository.save(team)
|
|
return this.teamRepository.save(team)
|
|
|
}
|
|
}
|
|
|
|
|
|