Skip to content

Commit 04d9260

Browse files
committed
#1182 - flesh out endpoint
1 parent bba633b commit 04d9260

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

src/backend/src/controllers/teams.controllers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export default class TeamsController {
6565

6666
static async createTeam(req: Request, res: Response, next: NextFunction) {
6767
try {
68-
68+
const { teamName, headId, slackId, description } = req.body;
69+
const submitter = await getCurrentUser(res);
70+
const team = await TeamsService.createTeam(submitter, teamName, headId, slackId, description);
71+
return res.status(200).json(team);
6972
} catch (error: unknown) {
7073
next(error);
7174
}

src/backend/src/routes/teams.routes.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ teamsRouter.post(
2424
teamsRouter.post('/:teamId/set-head', intMinZero(body('userId')), validateInputs, TeamsController.setTeamHead);
2525
teamsRouter.post(
2626
'/create',
27-
body('teamId').isString(),
2827
body('teamName').isString(),
29-
intMinZero(body('head')),
28+
intMinZero(body('headId')),
3029
body('slackId').isString(),
3130
body('description').isString(),
32-
body('members').isArray(),
33-
body('projects').isArray(),
34-
body('leads').isArray(),
3531
TeamsController.createTeam
3632
);
3733

src/backend/src/services/teams.services.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,49 @@ export default class TeamsService {
165165
/**
166166
* Create a new team.
167167
*/
168-
static async createTeam(submitter: User): Promise<Team> {
168+
static async createTeam(
169+
submitter: User,
170+
teamName: string,
171+
headId: number,
172+
slackId: string,
173+
description: string
174+
): Promise<Team> {
169175
if (!isAdmin(submitter.role)) {
170176
throw new AccessDeniedException('You must be an admin or higher to create a new team!');
171177
}
172-
const team = await prisma.team.create({});
173178

174-
return team;
179+
if (!isUnderWordCount(description, 300)) throw new HttpException(400, 'Description must be less than 300 words');
180+
181+
const newHead = await prisma.user.findUnique({
182+
where: { userId: headId }
183+
});
184+
185+
if (!newHead) throw new NotFoundException('User', headId);
186+
if (!isHead(newHead.role)) throw new HttpException(400, 'The team head must be at least a head');
187+
188+
// checking to see if any other teams have the new head as their current head or lead
189+
const newHeadTeam = await prisma.team.findFirst({
190+
where: { OR: [{ headId }, { leads: { some: { userId: headId } } }] }
191+
});
192+
193+
if (newHeadTeam) throw new HttpException(400, 'The new team head must not be a head or lead of another team');
194+
195+
const duplicateName = await prisma.team.findFirst({
196+
where: { teamName }
197+
});
198+
199+
if (duplicateName) throw new HttpException(400, 'The new team head must not be a head or lead of another team');
200+
201+
const createdTeam = await prisma.team.create({
202+
data: {
203+
teamName,
204+
slackId,
205+
description,
206+
head: { connect: { userId: headId } }
207+
},
208+
...teamQueryArgs
209+
});
210+
211+
return teamTransformer(createdTeam);
175212
}
176213
}

0 commit comments

Comments
 (0)