Skip to content

Commit 3458699

Browse files
authored
Creating teams clears requests. Don't accept team-owners (#129)
1 parent 49c2d9e commit 3458699

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

backend/src/services/team-service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ export class TeamService implements ITeamService {
167167
throw new Error("Team description cannot be empty");
168168
}
169169

170-
// TODO leaving team should make someone else owner
171-
// TODO order of team.users not guaranteed anymore I guess,
172-
// - you can only own one team, just like you can only be part of one team
173-
174170
if (user.team) {
175171
throw new Error("You are already part of a team");
176172
}
@@ -185,6 +181,7 @@ export class TeamService implements ITeamService {
185181
const createdTeam = await this._teams.save(team);
186182

187183
user.team = createdTeam;
184+
user.teamRequest = null;
188185
await this._users.save(user);
189186

190187
// Every team gets one project by default
@@ -316,7 +313,18 @@ export class TeamService implements ITeamService {
316313
}
317314

318315
if (!team.requestUserIds().includes(userId)) {
319-
throw new Error(`user ${userId} did not request to join team ${teamId}`);
316+
throw new Error(`User ${userId} did not request to join team ${teamId}`);
317+
}
318+
319+
const oldTeam = await this._teams.findOne({
320+
where: { id: userId },
321+
relations: ["users", "requests", "owner"],
322+
});
323+
324+
if (oldTeam?.owner?.id === userId) {
325+
throw new Error(
326+
"The user needs to select a new owner for their old team first",
327+
);
320328
}
321329

322330
await this._users.update({ id: userId }, { team, teamRequest: null });

backend/test/services/team-service.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ describe("TeamService", () => {
7777
expect(foundTeam!.owner.id).toEqual(user.id);
7878
});
7979

80+
it("clears the users team request", async () => {
81+
expect.assertions(1);
82+
83+
const team = await teamRepo.save(makeTeam("Team 1"));
84+
85+
const user = await userRepo.save({
86+
...makeUser("member@test.com"),
87+
teamRequest: team,
88+
});
89+
await teamService.createTeam(makeTeam(), user);
90+
91+
const updatedUser = await userRepo.findOne({ where: { id: user.id } });
92+
expect(updatedUser!.teamRequest).toBeNull();
93+
});
94+
8095
it("assigns the newly created team to the user", async () => {
8196
expect.assertions(1);
8297

@@ -106,7 +121,7 @@ describe("TeamService", () => {
106121
});
107122

108123
describe("acceptUserToTeam", () => {
109-
it("throws when the requester is neither owner nor admin", async () => {
124+
it("throws when the accepting user is neither owner nor admin", async () => {
110125
expect.assertions(1);
111126

112127
const owner = await userRepo.save(makeUser("owner@test.com"));
@@ -126,6 +141,24 @@ describe("TeamService", () => {
126141
).rejects.toThrow("You are not the owner of this team");
127142
});
128143

144+
it("throws when the requester is owner of another team", async () => {
145+
expect.assertions(1);
146+
147+
const team1Owner = await userRepo.save(makeUser("owner@test.com"));
148+
const team1 = await teamService.createTeam(makeTeam(), team1Owner);
149+
150+
const team2Owner = await userRepo.save(makeUser("req@test.com"));
151+
const team2 = await teamService.createTeam(makeTeam(), team2Owner);
152+
153+
await userRepo.save({ ...team2Owner, team: team2, teamRequest: team1 });
154+
155+
await expect(
156+
teamService.acceptUserToTeam(team1.id, team2Owner.id, team1Owner),
157+
).rejects.toThrow(
158+
"The user needs to select a new owner for their old team first",
159+
);
160+
});
161+
129162
it("allows the team owner to accept a join request", async () => {
130163
expect.assertions(2);
131164

0 commit comments

Comments
 (0)