@@ -164,6 +164,61 @@ export default class TeamsService {
164164 }
165165
166166 /**
167+ * Creates a new team in the database
168+ * @param submitter The submitter who is trying to create a new team
169+ * @param teamName the name of the new team
170+ * @param headId the id of the user who will be the head on the new team
171+ * @param slackId the slack id for the slack channel for the team
172+ * @param description a short description of the team (must be less than 300 words)
173+ * @returns The newly created team
174+ */
175+ static async createTeam (
176+ submitter : User ,
177+ teamName : string ,
178+ headId : number ,
179+ slackId : string ,
180+ description : string
181+ ) : Promise < Team > {
182+ if ( ! isAdmin ( submitter . role ) ) {
183+ throw new AccessDeniedException ( 'You must be an admin or higher to create a new team!' ) ;
184+ }
185+
186+ if ( ! isUnderWordCount ( description , 300 ) ) throw new HttpException ( 400 , 'Description must be less than 300 words' ) ;
187+
188+ const newHead = await prisma . user . findUnique ( {
189+ where : { userId : headId }
190+ } ) ;
191+
192+ if ( ! newHead ) throw new NotFoundException ( 'User' , headId ) ;
193+ if ( ! isHead ( newHead . role ) ) throw new HttpException ( 400 , 'The team head must be at least a head' ) ;
194+
195+ // checking to see if any other teams have the new head as their current head
196+ const newHeadTeam = await prisma . team . findFirst ( {
197+ where : { headId }
198+ } ) ;
199+
200+ if ( newHeadTeam ) throw new HttpException ( 400 , 'The new team head must not be a head of another team' ) ;
201+
202+ const duplicateName = await prisma . team . findFirst ( {
203+ where : { teamName }
204+ } ) ;
205+
206+ if ( duplicateName ) throw new HttpException ( 400 , 'The new team name must not be the name of another team' ) ;
207+
208+ const createdTeam = await prisma . team . create ( {
209+ data : {
210+ teamName,
211+ slackId,
212+ description,
213+ head : { connect : { userId : headId } }
214+ } ,
215+ ...teamQueryArgs
216+ } ) ;
217+
218+ return teamTransformer ( createdTeam ) ;
219+ }
220+
221+ /*
167222 * Update the given teamId's team's leads
168223 * @param submitter a user who's making this request
169224 * @param teamId a id of team to be updated
0 commit comments