diff --git a/server/src/teams/dto/results.dto.ts b/server/src/teams/dto/results.dto.ts index 726ea23d5..b0b404735 100644 --- a/server/src/teams/dto/results.dto.ts +++ b/server/src/teams/dto/results.dto.ts @@ -1,4 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; + import { Team } from '../teams.schema'; export class Results { diff --git a/server/src/teams/teams.controller.ts b/server/src/teams/teams.controller.ts index 4d203f079..ed1b6174e 100644 --- a/server/src/teams/teams.controller.ts +++ b/server/src/teams/teams.controller.ts @@ -6,13 +6,13 @@ import { Param, Post, Put, + Query, UseGuards, UsePipes, - Query, } from '@nestjs/common'; import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger'; -import { TeamsService } from './teams.service'; import mongoose from 'mongoose'; +import * as qs from 'qs'; import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard'; import { ValidationPipe } from '@/pipes/validation.pipe'; @@ -21,14 +21,14 @@ import { CreateTeamDto } from './dto/create-team.dto'; import { InviteToTeamDto } from './dto/invite-to-team.dto'; import { InviteToTeamResponseDto } from './dto/invite-to-team.response.dto'; import { TeamMembershipDTO } from './dto/membership.dto'; +import { Results } from './dto/results.dto'; import { StatusResponseDto } from './dto/status-response.dto'; import { TeamSearchDto } from './dto/team-search.dto'; import { TransferLeaderDto } from './dto/transfer-leader.dto'; -import { Results } from './dto/results.dto'; -import * as qs from 'qs'; -import { Team } from './teams.schema'; -import { UpdateTeamAvatarDto } from './dto/update-team-avatar.dto'; import { UpdateTeamDto } from './dto/update-team.dto'; +import { UpdateTeamAvatarDto } from './dto/update-team-avatar.dto'; +import { Team } from './teams.schema'; +import { TeamsService } from './teams.service'; @ApiTags('Teams') @Controller('/teams') @@ -55,10 +55,10 @@ export class TeamsController { type: Number, }) @Get() - getTeamsByPage(@Query('page') pageNumber?: number) { + getTeamsByPage(@Query('page') pageNumber?: number): Promise { /* A way to check if the pageNumber is a number or not. If it is not a number, it will return 1. */ const page: number = parseInt(pageNumber as any) || 1; - const limit: number = 9; + const limit = 9; return this.teamsService.getTeamsByPage(page, limit); } @@ -80,17 +80,13 @@ export class TeamsController { getFilteredTeamsByPage( @Query('filtersQuery') filtersQuery: string, @Query('page') pageNumber?: number, - ) { + ): Promise { const page: number = parseInt(pageNumber as any) || 1; - const limit: number = 9; + const limit = 9; /* Parsing the query string into an object. */ const parsedQuery = qs.parse(filtersQuery); - return this.teamsService.getFilteredTeamsByPage( - page, - limit, - parsedQuery, - ); + return this.teamsService.getFilteredTeamsByPage(page, limit, parsedQuery); } @ApiOperation({ @@ -98,7 +94,7 @@ export class TeamsController { }) @ApiResponse({ status: 200, type: Team }) @Get('/:id') - getTeam(@Param('id') id: mongoose.Types.ObjectId) { + getTeam(@Param('id') id: mongoose.Types.ObjectId): Promise { return this.teamsService.getTeamById(id); } @@ -142,9 +138,7 @@ export class TeamsController { }) @ApiResponse({ status: 200, type: InviteToTeamResponseDto }) @Post('/invite') - inviteToTeam( - @Body() dto: InviteToTeamDto, - ): Promise { + inviteToTeam(@Body() dto: InviteToTeamDto): Promise { return this.teamsService.inviteToTeam(dto); } diff --git a/server/src/teams/teams.service.ts b/server/src/teams/teams.service.ts index 4d6ffbd9c..11582f97b 100644 --- a/server/src/teams/teams.service.ts +++ b/server/src/teams/teams.service.ts @@ -1,25 +1,25 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import mongoose, { FilterQuery, Model } from 'mongoose'; -import { CreateTeamDto } from './dto/create-team.dto'; -import { UpdateTeamAvatarDto } from './dto/update-team-avatar.dto'; -import { Team, TeamsDocument } from './teams.schema'; -import { InviteToTeamDto } from './dto/invite-to-team.dto'; -import { TeamType } from './types/teams.type'; -import { TeamMembershipDTO } from './dto/membership.dto'; -import { UpdateTeamDto } from './dto/update-team.dto'; -import { teamUpdateValidate } from '@/validation/team-update.validation'; -import { InviteToTeamResponseDto } from './dto/invite-to-team.response.dto'; +import util from 'util'; + +import { FileService, FileType } from '@/files/file.service'; import { MailsService } from '@/mails/mails.service'; import { NotificationsService } from '@/notifications/notifications.service'; import { UsersService } from '@/users/users.service'; +import { teamUpdateValidate } from '@/validation/team-update.validation'; +import { CreateTeamDto } from './dto/create-team.dto'; +import { InviteToTeamDto } from './dto/invite-to-team.dto'; +import { InviteToTeamResponseDto } from './dto/invite-to-team.response.dto'; +import { TeamMembershipDTO } from './dto/membership.dto'; +import { Results } from './dto/results.dto'; import { StatusResponseDto } from './dto/status-response.dto'; -import { TeamSearchDto } from './dto/team-search.dto'; import { TransferLeaderDto } from './dto/transfer-leader.dto'; -import { Results } from './dto/results.dto'; -import util from 'util'; -import { FileService, FileType } from '@/files/file.service'; +import { UpdateTeamDto } from './dto/update-team.dto'; +import { UpdateTeamAvatarDto } from './dto/update-team-avatar.dto'; +import { Team, TeamsDocument } from './teams.schema'; +import { TeamType } from './types/teams.type'; @Injectable() export class TeamsService { @@ -194,12 +194,9 @@ export class TeamsService { * limit, the number of teams on the current page, and an array of team objects with their members and * leader populated. */ - async getTeamsByPage( - page: number = 1, - limit: number = 9, - ): Promise { + async getTeamsByPage(page = 1, limit = 9): Promise { /* A type assertion. */ - let results = {} as Results; + const results = {} as Results; /* Calculating the total number of users, the last page and the limit. */ results.total = await this.teamModel.count(); @@ -235,12 +232,12 @@ export class TeamsService { * @returns This function returns a Promise that resolves to a Results object. */ async getFilteredTeamsByPage( - page: number = 1, - limit: number = 9, + page = 1, + limit = 9, parsedQuery: FilterQuery = {}, ): Promise { /* A type assertion. */ - let results = {} as Results; + const results = {} as Results; console.log(parsedQuery); @@ -263,16 +260,10 @@ export class TeamsService { parsedQuery.$expr = { $and: [ { - $gte: [ - { $size: '$members' }, - Number(parsedQuery.members[0]), - ], + $gte: [{ $size: '$members' }, Number(parsedQuery.members[0])], }, { - $lte: [ - { $size: '$members' }, - Number(parsedQuery.members[1]), - ], + $lte: [{ $size: '$members' }, Number(parsedQuery.members[1])], }, ], }; @@ -417,9 +408,7 @@ export class TeamsService { userId: mongoose.Types.ObjectId, notificationid: mongoose.Types.ObjectId, ): Promise { - console.log( - `Removing notification ${notificationid} from user ${userId}`, - ); + console.log(`Removing notification ${notificationid} from user ${userId}`); /* Removing the notification from the database. */ await this.notificationsService.removeNotification(notificationid); @@ -438,9 +427,7 @@ export class TeamsService { notificationid: mongoose.Types.ObjectId, ): Promise { const notification = - await this.notificationsService.getTeamNotificationById( - notificationid, - ); + await this.notificationsService.getTeamNotificationById(notificationid); if (!notification) { /* Checking if the user has the notification. If it does, it is removing it. */ @@ -525,9 +512,7 @@ export class TeamsService { notificationid: mongoose.Types.ObjectId, ): Promise { const notification = - await this.notificationsService.getTeamNotificationById( - notificationid, - ); + await this.notificationsService.getTeamNotificationById(notificationid); if (!notification) { /* Checking if the user has the notification. If it does, it is removing it. */ @@ -564,10 +549,7 @@ export class TeamsService { /* Checking if the user exists. If it doesn't, it is throwing an error. */ if (!candidate) { - throw new HttpException( - `User was not found`, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException(`User was not found`, HttpStatus.BAD_REQUEST); } /* Checking if the user already has a team. If it does, it is throwing an error. */ @@ -628,10 +610,7 @@ export class TeamsService { /* Checking if the user exists. If it doesn't, it is throwing an error. */ if (!candidate) { - throw new HttpException( - `User was not found`, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException(`User was not found`, HttpStatus.BAD_REQUEST); } /* Checking if the user already has a team. If it does, it is throwing an error. */ @@ -762,21 +741,13 @@ export class TeamsService { // check if leader is valid user const leader = await this.userService.getUserById(dto.leader_id); if (!leader) { - throw new HttpException( - `User was not found`, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException(`User was not found`, HttpStatus.BAD_REQUEST); } // check if new_leader is valid user - const new_leader = await this.userService.getUserById( - dto.new_leader_id, - ); + const new_leader = await this.userService.getUserById(dto.new_leader_id); if (!new_leader) { - throw new HttpException( - `User was not found`, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException(`User was not found`, HttpStatus.BAD_REQUEST); } // check if both leader and new_leader belogn to the same team diff --git a/server/src/teams/tests/teams.service.spec.ts b/server/src/teams/tests/teams.service.spec.ts index 2b4ea8929..1f69cc63f 100644 --- a/server/src/teams/tests/teams.service.spec.ts +++ b/server/src/teams/tests/teams.service.spec.ts @@ -51,9 +51,7 @@ describe('TeamService', () => { envFilePath: `.dev.env`, }), rootMongooseTestModule(), - MongooseModule.forFeature([ - { name: Team.name, schema: TeamsSchema }, - ]), + MongooseModule.forFeature([{ name: Team.name, schema: TeamsSchema }]), /* Serving the static files. */ ServeStaticModule.forRoot({ rootPath: path.resolve(__dirname, 'static'), @@ -168,7 +166,7 @@ describe('TeamService', () => { }); it('should create 100 users and 100 teams', async () => { - let users = await createMultipleUsers(100); + const users = await createMultipleUsers(100); for (let i = 0; i < users.length; i++) { await teamsService.createTeam(CreateTeamDtoStub(users[i]._id)); @@ -196,9 +194,7 @@ describe('TeamService', () => { it('should create user, give him role, create team, then create another user invite him to team and double check everything was updated', async () => { const user1 = await createUser(); - const team = await teamsService.createTeam( - CreateTeamDtoStub(user1._id), - ); + const team = await teamsService.createTeam(CreateTeamDtoStub(user1._id)); await userService.updateAvatar(UpdateUserAvatarDtoStub(user1.email, 1)); @@ -224,9 +220,7 @@ describe('TeamService', () => { it('should create user, give him role, create team, then create another user invite him to team and double check invite has image field', async () => { const user1 = await createUser(); - const team = await teamsService.createTeam( - CreateTeamDtoStub(user1._id), - ); + const team = await teamsService.createTeam(CreateTeamDtoStub(user1._id)); const user2 = await userService.createUser( RegisterUserDtoStub('mmashc2@uic.edu'), @@ -289,9 +283,7 @@ describe('TeamService', () => { for (let i = 0; i < users.length; i++) { const notification = - await notificationService.getTeamNotificationsForUser( - users[i]._id, - ); + await notificationService.getTeamNotificationsForUser(users[i]._id); /* Checking if the notification is defined. */ expect(notification[0]).toBeDefined(); @@ -350,9 +342,7 @@ describe('TeamService', () => { const updatedTeam = await teamsService.updateTeam(incoming_update_data); expect(updatedTeam.name).toEqual(incoming_update_data.name); - expect(updatedTeam.description).toEqual( - incoming_update_data.description, - ); + expect(updatedTeam.description).toEqual(incoming_update_data.description); expect(updatedTeam.country).toEqual(incoming_update_data.country); expect(updatedTeam.tag).toEqual(incoming_update_data.tag); expect(updatedTeam.type).toEqual(incoming_update_data.type); @@ -382,9 +372,7 @@ describe('TeamService', () => { await teamsService.createTeam(CreateTeamDtoStub(user._id)); // @ts-ignore - await expect(teamsService.updateTeam({})).rejects.toThrow( - HttpException, - ); + await expect(teamsService.updateTeam({})).rejects.toThrow(HttpException); }); it('should create user, then create team and then call updateTeam with only required teamid', async () => { @@ -409,7 +397,7 @@ describe('TeamService', () => { }); it('should create 5 users and 5 teams', async () => { - let users = await createMultipleUsers(5); + const users = await createMultipleUsers(5); await teamsService.createTeam(CreateTeamDtoStub(users[0]._id, 'TEG1')); await teamsService.createTeam(CreateTeamDtoStub(users[1]._id, 'TEG2')); @@ -427,14 +415,12 @@ describe('TeamService', () => { }); it('should create 10 users and 10 teams, make 1 team have 3 players and filter by team with 3 players', async () => { - let users = await createMultipleUsers(10); + const users = await createMultipleUsers(10); let team: Team; for (let i = 0; i < 7; i++) { - team = await teamsService.createTeam( - CreateTeamDtoStub(users[i]._id), - ); + team = await teamsService.createTeam(CreateTeamDtoStub(users[i]._id)); } await teamsService.joinTeam({ @@ -448,20 +434,20 @@ describe('TeamService', () => { }); expect( - (await teamsService.getFilteredTeamsByPage(1, 9, { members: [3] })) - .total, + (await teamsService.getFilteredTeamsByPage(1, 9, { members: [3] })).total, ).toBe(1); }); it('should create 10 users and 10 teams, make 1 team have 3 players and 1 team 4 players and filter by team with 3 players', async () => { - let users = await createMultipleUsers(10); - - let team1: Team; - let team2: Team; + const users = await createMultipleUsers(10); - team1 = await teamsService.createTeam(CreateTeamDtoStub(users[0]._id)); + const team1 = await teamsService.createTeam( + CreateTeamDtoStub(users[0]._id), + ); - team2 = await teamsService.createTeam(CreateTeamDtoStub(users[1]._id)); + const team2 = await teamsService.createTeam( + CreateTeamDtoStub(users[1]._id), + ); // add 3 members to team1 for (let i = 2; i < 4; i++) {