Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Applied eslint and fixed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nmashchenko committed May 3, 2023
1 parent 6c9366b commit a710e35
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 108 deletions.
1 change: 1 addition & 0 deletions server/src/teams/dto/results.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';

import { Team } from '../teams.schema';

export class Results {
Expand Down
32 changes: 13 additions & 19 deletions server/src/teams/teams.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand All @@ -55,10 +55,10 @@ export class TeamsController {
type: Number,
})
@Get()
getTeamsByPage(@Query('page') pageNumber?: number) {
getTeamsByPage(@Query('page') pageNumber?: number): Promise<Results> {
/* 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);
}

Expand All @@ -80,25 +80,21 @@ export class TeamsController {
getFilteredTeamsByPage(
@Query('filtersQuery') filtersQuery: string,
@Query('page') pageNumber?: number,
) {
): Promise<Results> {
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({
summary: 'Get team by id',
})
@ApiResponse({ status: 200, type: Team })
@Get('/:id')
getTeam(@Param('id') id: mongoose.Types.ObjectId) {
getTeam(@Param('id') id: mongoose.Types.ObjectId): Promise<Team> {
return this.teamsService.getTeamById(id);
}

Expand Down Expand Up @@ -142,9 +138,7 @@ export class TeamsController {
})
@ApiResponse({ status: 200, type: InviteToTeamResponseDto })
@Post('/invite')
inviteToTeam(
@Body() dto: InviteToTeamDto,
): Promise<InviteToTeamResponseDto> {
inviteToTeam(@Body() dto: InviteToTeamDto): Promise<InviteToTeamResponseDto> {
return this.teamsService.inviteToTeam(dto);
}

Expand Down
85 changes: 28 additions & 57 deletions server/src/teams/teams.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Results> {
async getTeamsByPage(page = 1, limit = 9): Promise<Results> {
/* 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();
Expand Down Expand Up @@ -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<any> = {},
): Promise<Results> {
/* A type assertion. */
let results = {} as Results;
const results = {} as Results;

console.log(parsedQuery);

Expand All @@ -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])],
},
],
};
Expand Down Expand Up @@ -417,9 +408,7 @@ export class TeamsService {
userId: mongoose.Types.ObjectId,
notificationid: mongoose.Types.ObjectId,
): Promise<void> {
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);

Expand All @@ -438,9 +427,7 @@ export class TeamsService {
notificationid: mongoose.Types.ObjectId,
): Promise<StatusResponseDto> {
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. */
Expand Down Expand Up @@ -525,9 +512,7 @@ export class TeamsService {
notificationid: mongoose.Types.ObjectId,
): Promise<StatusResponseDto> {
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. */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand Down
50 changes: 18 additions & 32 deletions server/src/teams/tests/teams.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));

Expand All @@ -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'),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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'));
Expand All @@ -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({
Expand All @@ -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++) {
Expand Down

0 comments on commit a710e35

Please sign in to comment.