forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteams.service.ts
More file actions
60 lines (53 loc) · 1.89 KB
/
Copy pathteams.service.ts
File metadata and controls
60 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Bounty, Team, TeamMemberSplit } from '../common/entities';
import { CreateTeamDto } from './dto/create-team.dto';
import { validateSplitPercentages } from './team-split.util';
@Injectable()
export class TeamsService {
constructor(
@InjectRepository(Team) private readonly teamRepo: Repository<Team>,
@InjectRepository(TeamMemberSplit)
private readonly splitRepo: Repository<TeamMemberSplit>,
@InjectRepository(Bounty) private readonly bountyRepo: Repository<Bounty>,
) {}
async create(dto: CreateTeamDto): Promise<Team> {
validateSplitPercentages(dto.members);
const team = await this.teamRepo.save(
this.teamRepo.create({
name: dto.name,
createdById: dto.createdById ?? null,
}),
);
team.splits = await Promise.all(
dto.members.map((m) =>
this.splitRepo.save(
this.splitRepo.create({
teamId: team.id,
userId: m.userId,
role: m.role ?? null,
percentage: m.percentage.toFixed(2),
}),
),
),
);
return team;
}
async findOne(id: string): Promise<Team> {
const team = await this.teamRepo.findOne({
where: { id },
relations: { splits: true },
});
if (!team) throw new NotFoundException(`Team ${id} not found`);
return team;
}
/** Attaches an existing team to a bounty so its payout is split on merge. */
async assignToBounty(teamId: string, bountyId: string): Promise<Bounty> {
await this.findOne(teamId); // ensures team exists
const bounty = await this.bountyRepo.findOne({ where: { id: bountyId } });
if (!bounty) throw new NotFoundException(`Bounty ${bountyId} not found`);
bounty.teamId = teamId;
return this.bountyRepo.save(bounty);
}
}