|
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + NotFoundException, |
| 4 | + ForbiddenException, |
| 5 | + ConflictException, |
| 6 | +} from '@nestjs/common'; |
| 7 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 8 | +import { Repository } from 'typeorm'; |
| 9 | +import { randomBytes } from 'crypto'; |
| 10 | +import { TeamEntity } from '../storage/entities/team.entity'; |
| 11 | +import { TeamMemberEntity } from '../storage/entities/team-member.entity'; |
| 12 | +import { DeveloperEntity } from '../storage/entities/developer.entity'; |
| 13 | + |
| 14 | +@Injectable() |
| 15 | +export class TeamsService { |
| 16 | + constructor( |
| 17 | + @InjectRepository(TeamEntity) |
| 18 | + private readonly teamRepo: Repository<TeamEntity>, |
| 19 | + @InjectRepository(TeamMemberEntity) |
| 20 | + private readonly memberRepo: Repository<TeamMemberEntity>, |
| 21 | + @InjectRepository(DeveloperEntity) |
| 22 | + private readonly developerRepo: Repository<DeveloperEntity>, |
| 23 | + ) {} |
| 24 | + |
| 25 | + async listForDeveloper(developerId: string) { |
| 26 | + const memberships = await this.memberRepo.find({ |
| 27 | + where: { developerId }, |
| 28 | + relations: ['team'], |
| 29 | + }); |
| 30 | + return memberships.map((m) => ({ ...m.team, role: m.role })); |
| 31 | + } |
| 32 | + |
| 33 | + async create(developerId: string, name: string, slug?: string) { |
| 34 | + const finalSlug = slug || name.replace(/[^a-z0-9-]/gi, '-').toLowerCase(); |
| 35 | + const existing = await this.teamRepo.findOneBy({ slug: finalSlug }); |
| 36 | + if (existing) { |
| 37 | + throw new ConflictException('Slug already taken'); |
| 38 | + } |
| 39 | + |
| 40 | + const team = await this.teamRepo.save( |
| 41 | + this.teamRepo.create({ |
| 42 | + name, |
| 43 | + slug: finalSlug + '-' + randomBytes(3).toString('hex'), |
| 44 | + ownerId: developerId, |
| 45 | + plan: 'free', |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + await this.memberRepo.save( |
| 50 | + this.memberRepo.create({ |
| 51 | + teamId: team.id, |
| 52 | + developerId, |
| 53 | + role: 'owner', |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + return team; |
| 58 | + } |
| 59 | + |
| 60 | + async getById(teamId: string) { |
| 61 | + const team = await this.teamRepo.findOneBy({ id: teamId }); |
| 62 | + if (!team) throw new NotFoundException('Team not found'); |
| 63 | + return team; |
| 64 | + } |
| 65 | + |
| 66 | + async update(teamId: string, updates: { name?: string; slug?: string }) { |
| 67 | + const team = await this.getById(teamId); |
| 68 | + if (updates.slug) { |
| 69 | + const existing = await this.teamRepo.findOneBy({ slug: updates.slug }); |
| 70 | + if (existing && existing.id !== teamId) { |
| 71 | + throw new ConflictException('Slug already taken'); |
| 72 | + } |
| 73 | + } |
| 74 | + Object.assign(team, updates); |
| 75 | + return this.teamRepo.save(team); |
| 76 | + } |
| 77 | + |
| 78 | + async delete(teamId: string, developerId: string) { |
| 79 | + const team = await this.getById(teamId); |
| 80 | + if (team.ownerId !== developerId) { |
| 81 | + throw new ForbiddenException('Only the owner can delete a team'); |
| 82 | + } |
| 83 | + await this.teamRepo.remove(team); |
| 84 | + return { deleted: true }; |
| 85 | + } |
| 86 | + |
| 87 | + async listMembers(teamId: string) { |
| 88 | + return this.memberRepo.find({ |
| 89 | + where: { teamId }, |
| 90 | + relations: ['developer'], |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + async addMember(teamId: string, email: string, role: string = 'member') { |
| 95 | + const developer = await this.developerRepo.findOneBy({ email }); |
| 96 | + if (!developer) throw new NotFoundException('Developer not found'); |
| 97 | + |
| 98 | + const existing = await this.memberRepo.findOne({ |
| 99 | + where: { teamId, developerId: developer.id }, |
| 100 | + }); |
| 101 | + if (existing) throw new ConflictException('Already a member'); |
| 102 | + |
| 103 | + return this.memberRepo.save( |
| 104 | + this.memberRepo.create({ |
| 105 | + teamId, |
| 106 | + developerId: developer.id, |
| 107 | + role, |
| 108 | + }), |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + async updateMemberRole(teamId: string, memberId: string, role: string) { |
| 113 | + const member = await this.memberRepo.findOne({ |
| 114 | + where: { id: memberId, teamId }, |
| 115 | + }); |
| 116 | + if (!member) throw new NotFoundException('Member not found'); |
| 117 | + if (member.role === 'owner') throw new ForbiddenException('Cannot change owner role'); |
| 118 | + member.role = role; |
| 119 | + return this.memberRepo.save(member); |
| 120 | + } |
| 121 | + |
| 122 | + async removeMember(teamId: string, memberId: string) { |
| 123 | + const member = await this.memberRepo.findOne({ |
| 124 | + where: { id: memberId, teamId }, |
| 125 | + }); |
| 126 | + if (!member) throw new NotFoundException('Member not found'); |
| 127 | + if (member.role === 'owner') throw new ForbiddenException('Cannot remove the owner'); |
| 128 | + await this.memberRepo.remove(member); |
| 129 | + return { removed: true }; |
| 130 | + } |
| 131 | +} |
0 commit comments