From 05025de61ee857a348bcfbfa3f3a91f97513b5e4 Mon Sep 17 00:00:00 2001
From: Samuli Virtapohja <l4721@student.jamk.fi>
Date: Wed, 24 Jul 2019 13:02:50 +0300
Subject: [PATCH] faction auditted

---
 src/app.module.ts                 |  4 ++-
 src/faction/faction.controller.ts | 43 +++++++++++++++++++------------
 src/faction/faction.dto.ts        |  9 ++++++-
 src/faction/faction.entity.ts     | 13 +++-------
 src/faction/faction.module.ts     | 17 +++++++++---
 src/faction/faction.service.ts    | 41 +++++++++++++++--------------
 src/score/score.controller.ts     |  1 -
 src/user/user.controller.ts       |  2 +-
 src/user/user.decorator.ts        | 14 +++++++---
 src/user/user.module.ts           |  1 +
 10 files changed, 89 insertions(+), 56 deletions(-)

diff --git a/src/app.module.ts b/src/app.module.ts
index 30a7c06..571d924 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -25,10 +25,12 @@ import { ReplayModule } from './replay/replay.module';
   Core of the server,
   Every module is being imported and combined here.
 
+  AppController needs to be kept in for SSL verification to work (root needs to return something)
+
   TypeOrmModule checks ormconfig.json for database connection.
 
   More information on global decorators can be found from shared folder.
-  
+
   Providers can be found from shared folder
    - HttpErrorFilter
    - LoggingInterceptor
diff --git a/src/faction/faction.controller.ts b/src/faction/faction.controller.ts
index 24e4a9a..cbbd3ec 100644
--- a/src/faction/faction.controller.ts
+++ b/src/faction/faction.controller.ts
@@ -7,8 +7,6 @@ import {
   Body,
   Get,
   Put,
-  UseInterceptors,
-  ClassSerializerInterceptor,
   Delete,
 } from '@nestjs/common';
 
@@ -26,6 +24,23 @@ import { FactionService } from './faction.service';
 import { Roles, GameStates } from '../shared/guard.decorator';
 import { GamePerson } from 'src/game/gameperson.decorator';
 
+/*
+FactionController is being used for routing:
+
+Group
+- create group when game status is CREATED
+- getting groups with faction id(this is used mainly for listing players)
+- joining group when game status is CREATED
+
+Faction
+- checking users faction
+- joining faction
+- leaving faction
+- changing faction multiplier (not implemented)
+
+See shared folder files for more information on decorators.
+*/
+
 @Controller('faction')
 export class FactionController {
   constructor(private factionservice: FactionService) {}
@@ -40,9 +55,7 @@ export class FactionController {
     @Param('id') id: string,
     @Body() data: GameGroupDTO,
   ) {
-    try {
-      return this.factionservice.createGroup(person, id, data);
-    } catch (error) {}
+    return this.factionservice.createGroup(person, id, data);
   }
 
   // id is faction ID
@@ -63,12 +76,6 @@ export class FactionController {
     return this.factionservice.joinGroup(gameperson, data);
   }
 
-  @UseInterceptors(ClassSerializerInterceptor)
-  @Get('get-faction-members/:id')
-  async getFactionMembers(@Param('id') factionId) {
-    return this.factionservice.listFactionMembers(factionId);
-  }
-
   // param game ID is passed to @Roles
   @Put('promote/:id')
   @Roles('admin')
@@ -100,13 +107,15 @@ export class FactionController {
   leaveFaction(@GamePerson('gamepersonId') gamepersonId) {
     return this.factionservice.leaveFaction(gamepersonId);
   }
+
   // used to change factions multiplier
-  @Put('faction-multiplier/:id')
-  @Roles('admin')
-  @GameStates('STARTED')
-  factionMultiplier(@Param('id') game, @Body() body: FactionDTO) {
-    return this.factionservice.changeFactionMultiplier(body);
-  }
+  // not implemented in frontend uncomment this and services equivalent when needed
+  // @Put('faction-multiplier/:id')
+  // @Roles('admin')
+  // @GameStates('STARTED')
+  // factionMultiplier(@Param('id') game, @Body() body: FactionDTO) {
+  //   return this.factionservice.changeFactionMultiplier(body);
+  // }
 
   // check if person belongs to a faction in a game
   @Get('check-faction/:id')
diff --git a/src/faction/faction.dto.ts b/src/faction/faction.dto.ts
index fbc46d3..df69715 100644
--- a/src/faction/faction.dto.ts
+++ b/src/faction/faction.dto.ts
@@ -13,10 +13,17 @@ import {
 } from 'class-validator';
 
 import { GameEntity } from '../game/game.entity';
-import { RoleValidation, Uuid } from '../shared/custom-validation';
+import { RoleValidation } from '../shared/custom-validation';
 import { GameDTO } from '../game/game.dto';
 import { FactionEntity, GameGroupEntity } from './faction.entity';
 
+/*
+Contains Validation for FactionDTO, JoinFactionDTO, PromotePlayerDTO, GameGroupDTO, JoinGameGroupDTO
+
+uses class-validator built in validations
+see https://github.com/typestack/class-validator
+*/
+
 export class FactionDTO {
   @IsOptional()
   @IsUUID('4')
diff --git a/src/faction/faction.entity.ts b/src/faction/faction.entity.ts
index a851e19..0240260 100644
--- a/src/faction/faction.entity.ts
+++ b/src/faction/faction.entity.ts
@@ -12,11 +12,11 @@ import { GameEntity } from '../game/game.entity';
 import { Game_PersonEntity } from '../game/game.entity';
 import { MapDrawingEntity } from '../draw/coordinate.entity';
 import { Exclude } from 'class-transformer';
-import { ScoreEntity } from 'src/score/score.entity';
+import { ScoreEntity } from '../score/score.entity';
 
-//////////////////////////////////////////////////////////////////////
-/// Entities for Factions and Groups in Factions                   ///
-//////////////////////////////////////////////////////////////////////
+/*
+FactionEntity & GameGroupEntity reflect database tables.
+*/
 
 @Entity('Faction')
 export class FactionEntity {
@@ -44,11 +44,6 @@ export class FactionEntity {
   })
   scores: ScoreEntity[];
 
-  factionObject() {
-    const { factionId, factionName, game } = this;
-    return { factionId, factionName, game };
-  }
-
   passwordCheck(pass: string) {
     return pass == this.factionPassword ? true : false;
   }
diff --git a/src/faction/faction.module.ts b/src/faction/faction.module.ts
index f53f076..49ba6e9 100644
--- a/src/faction/faction.module.ts
+++ b/src/faction/faction.module.ts
@@ -6,10 +6,19 @@ import { FactionService } from './faction.service';
 import { GameGroupEntity, FactionEntity } from './faction.entity';
 import { Game_PersonEntity } from '../game/game.entity';
 
-/////////////////////////////////////////////////////////////////////
-/// Faction                                                       ///
-/// - contains everything to do with Faction data.                ///
-/////////////////////////////////////////////////////////////////////
+/*
+Entities
+- FactionEntity
+- Game_PersonEntity
+- GameGroupEntity
+
+Controllers
+- FactionController
+
+Provider
+- FactionService
+*/
+
 @Module({
   imports: [
     TypeOrmModule.forFeature([
diff --git a/src/faction/faction.service.ts b/src/faction/faction.service.ts
index 3e2efca..a3ea83e 100644
--- a/src/faction/faction.service.ts
+++ b/src/faction/faction.service.ts
@@ -11,6 +11,21 @@ import {
 } from './faction.dto';
 import { Game_PersonEntity } from '../game/game.entity';
 
+/*
+FactionService contains functions for
+- Joining faction
+- Leaving faction
+- Change faction multiplier (not implemented)
+
+Group
+- Creating group
+- List faction players in groups
+
+Player
+- Promote player
+- verifying user
+*/
+
 @Injectable()
 export class FactionService {
   constructor(
@@ -67,13 +82,13 @@ export class FactionService {
   }
 
   // changes factions multiplier to the value given to FactionDTO
-  async changeFactionMultiplier(body: FactionDTO) {
-    const faction = await this.factionRepository.findOne({
-      where: { factionId: body.factionId },
-    });
-    faction.multiplier = body.multiplier;
-    return await this.factionRepository.save(faction);
-  }
+  // async changeFactionMultiplier(body: FactionDTO) {
+  //   const faction = await this.factionRepository.findOne({
+  //     where: { factionId: body.factionId },
+  //   });
+  //   faction.multiplier = body.multiplier;
+  //   return await this.factionRepository.save(faction);
+  // }
 
   async promotePlayer(body) {
     const gamepersonId = body.player;
@@ -197,18 +212,6 @@ export class FactionService {
     };
   }
 
-  // lists all members from given faction
-  async listFactionMembers(faction) {
-    const members = await this.game_PersonRepository.find({
-      where: { faction },
-      relations: ['person'],
-    });
-    members.sort(function(a, b) {
-      return a['person']['name'].localeCompare(b['person']['name']);
-    });
-    return members;
-  }
-
   // checks if player is in a faction and what role the player is in
   async verifyUser(person, game) {
     const gameperson = await this.game_PersonRepository.findOne({
diff --git a/src/score/score.controller.ts b/src/score/score.controller.ts
index a9989a0..344c33c 100644
--- a/src/score/score.controller.ts
+++ b/src/score/score.controller.ts
@@ -32,7 +32,6 @@ export class ScoreController {
   // shows scores, :id is gameId
   @Get('get-score/:id')
   @UseInterceptors(ClassSerializerInterceptor)
-  @GameStates('STARTED')
   async getScores(@Param('id') gameId: GameEntity) {
     return this.scoreService.getScores(gameId);
   }
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 6e39509..09e308d 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -19,7 +19,7 @@ UserController is being used for routing:
 
 - Verify is checking for logged in user
 
-See shared files for more information on decorators.
+See shared folder files for more information on decorators.
 */
 
 @Controller('user')
diff --git a/src/user/user.decorator.ts b/src/user/user.decorator.ts
index 2c499d9..2f9a6fc 100644
--- a/src/user/user.decorator.ts
+++ b/src/user/user.decorator.ts
@@ -1,6 +1,14 @@
-import { createParamDecorator } from "@nestjs/common";
+import { createParamDecorator } from '@nestjs/common';
+
+/*
+UserDecorator
+
+See auth.guard.ts in shared folder for more information
+
+Returns user id and user name, this is mainly used to return user id
+*/
 
 // used to pass user information to controllers
 export const User = createParamDecorator((data, req) => {
-    return data ? req.user[data] : req.user;
-})
\ No newline at end of file
+  return data ? req.user[data] : req.user;
+});
diff --git a/src/user/user.module.ts b/src/user/user.module.ts
index 8b01d81..7216dde 100644
--- a/src/user/user.module.ts
+++ b/src/user/user.module.ts
@@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
 import { UserController } from './user.controller';
 import { UserService } from './user.service';
 import { PersonEntity } from './user.entity';
+
 /*
 Entities
 - PersonEntity
-- 
GitLab