diff --git a/src/draw/coordinate.entity.ts b/src/draw/coordinate.entity.ts index ef5447f61cfd1dd07d1f81a8ee1fce8e8256da2a..049f41b8e63af083890411ad16e895c2183384dd 100644 --- a/src/draw/coordinate.entity.ts +++ b/src/draw/coordinate.entity.ts @@ -1,12 +1,6 @@ -import { - Entity, - Column, - PrimaryGeneratedColumn, - ManyToOne, - Timestamp, -} from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm'; -import { Game_PersonEntity, GameEntity } from '../game/game.entity'; +import { GameEntity } from '../game/game.entity'; import { FactionEntity } from '../faction/faction.entity'; @Entity('MapDrawing') @@ -25,27 +19,14 @@ export class MapDrawingEntity { onDelete: 'CASCADE', }) gameId: GameEntity; -} - -@Entity('Game_Person_MapDrawing') -export class Game_Person_MapDrawingEntity { - @PrimaryGeneratedColumn('uuid') GPmapDrawingId: string; - @Column({ type: 'timestamp' }) GPCTimeStamp: Timestamp; - @ManyToOne( - type => Game_PersonEntity, - game_person => game_person.gamepersonId, - { - onDelete: 'CASCADE', - }, - ) - game_person: Game_PersonEntity; - @ManyToOne( - type => MapDrawingEntity, - map_drawing => map_drawing.mapDrawingId, - { - onDelete: 'CASCADE', - }, - ) - map_drawing: MapDrawingEntity; + async ownershipCheck(factionEntity, role) { + if (role === 'admin') { + return factionEntity == this.faction; + } else { + return this.faction && factionEntity.factionId === this.faction.factionId + ? true + : false; + } + } } diff --git a/src/draw/draw.controller.ts b/src/draw/draw.controller.ts index 26f6cf8c56142ef190ad3bcbfd7ce9d32caa371e..39db26afdbaab2f8c4c44ca166fc545742daf5f9 100644 --- a/src/draw/draw.controller.ts +++ b/src/draw/draw.controller.ts @@ -7,12 +7,14 @@ import { UsePipes, ValidationPipe, Body, + UseInterceptors, + ClassSerializerInterceptor, } from '@nestjs/common'; -import { AuthGuard } from '../shared/auth.guard'; import { DrawService } from './draw.service'; import { Roles, GameStates } from '../shared/guard.decorator'; -import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; +import { MapDrawingDTO } from './mapdrawing.dto'; +import { GamePerson } from 'src/game/gameperson.decorator'; /* DrawController @@ -29,14 +31,18 @@ export class DrawController { @Roles('admin', 'factionleader') @GameStates('CREATED', 'STARTED') @UsePipes(new ValidationPipe()) - async draw(@Param('id') gameId, @Body() data: MapDrawingDTO) { - return this.drawService.draw(gameId, data); + async draw( + @GamePerson() gameperson, + @Param('id') gameId, + @Body() data: MapDrawingDTO, + ) { + return this.drawService.draw(gameperson, gameId, data); } @Get('map/:id') - @UseGuards(new AuthGuard()) - @UsePipes(new ValidationPipe()) - async drawMap(@Param('id') id, @Body() data: ReturnDrawingsDTO) { - return this.drawService.drawMap(id, data); + @Roles('admin', 'factionleader', 'soldier', 'groupleader') + @UseInterceptors(ClassSerializerInterceptor) + async drawMap(@GamePerson() gameperson, @Param('id') gameId) { + return this.drawService.drawMap(gameperson, gameId); } } diff --git a/src/draw/draw.service.ts b/src/draw/draw.service.ts index d699af7a990a2178cda1617a6a2a06c3ba988f0e..a046ce9a3327b1c71faecd72143d54a0badd83d3 100644 --- a/src/draw/draw.service.ts +++ b/src/draw/draw.service.ts @@ -1,9 +1,9 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { MapDrawingEntity } from '../draw/coordinate.entity'; -import { MapDrawingDTO, ReturnDrawingsDTO } from './mapdrawing.dto'; +import { MapDrawingDTO } from './mapdrawing.dto'; import { NotificationGateway } from 'src/notifications/notifications.gateway'; @Injectable() @@ -14,38 +14,59 @@ export class DrawService { private notificationGateway: NotificationGateway, ) {} - async draw(gameId, data: MapDrawingDTO) { + async draw(gameperson, gameId, data: MapDrawingDTO) { data['gameId'] = gameId; const drawing = await this.mapDrawingRepository.create(data); this.notificationGateway.server.emit(gameId, { type: 'drawing-update', }); + // create new instance if id is null if (data.mapDrawingId == null || data.mapDrawingId == '') { - // luo uuden instanssin. + drawing.faction = gameperson.faction; + console.log(drawing); const mapDrawing = await this.mapDrawingRepository.insert(drawing); return mapDrawing.identifiers; - } else { - //päivittää mapDrawingin + } + // get ref from db + const draw = await this.mapDrawingRepository.findOne({ + where: { mapDrawingId: data.mapDrawingId }, + relations: ['faction'], + }); + if (await draw.ownershipCheck(gameperson.faction, gameperson.role)) { + // else update the existing instance return await this.mapDrawingRepository.save(drawing); } + + throw new HttpException( + 'Drawing is not from your faction!', + HttpStatus.BAD_REQUEST, + ); } - // draw map based on game and - async drawMap(id, data: ReturnDrawingsDTO) { + // draw map based on game and gameperson faction + async drawMap(gameperson, gameId) { + // return all active drawings if admin + if (gameperson.role === 'admin') { + return await this.mapDrawingRepository.find({ + where: { gameId: gameId, drawingIsActive: true }, + relations: ['faction'], + }); + } // return mapdrawings with given faction and gameid return await this.mapDrawingRepository.find({ where: [ { - gameId: id, - faction: data.factionId, + gameId: gameId, + faction: gameperson.faction, drawingIsActive: true, }, { - gameId: id, + gameId: gameId, faction: null, drawingIsActive: true, }, ], + relations: ['faction'], }); } } diff --git a/src/draw/mapdrawing.dto.ts b/src/draw/mapdrawing.dto.ts index f7e7c781a0cbd308edf5f8c14f068b449f43a05e..f377b5c601ea423ad7824d9fa36758cbe479cc96 100644 --- a/src/draw/mapdrawing.dto.ts +++ b/src/draw/mapdrawing.dto.ts @@ -12,16 +12,7 @@ export class MapDrawingDTO { @IsOptional() @IsUUID('4') gameId: GameEntity; - @IsOptional() - @IsUUID('4') - faction?: FactionEntity; @IsBoolean() drawingIsActive?: boolean; drawingValidTill?: string; } - -export class ReturnDrawingsDTO { - @IsOptional() - @IsUUID('4') - factionId: FactionEntity; -} diff --git a/src/faction/faction.service.ts b/src/faction/faction.service.ts index 06940338dabf99494ed657a0cd56cefb9576ccf5..ab1dd014165b8c8c37b812b70757f3792fa59711 100644 --- a/src/faction/faction.service.ts +++ b/src/faction/faction.service.ts @@ -134,12 +134,16 @@ export class FactionService { where: { person, game }, relations: ['faction'], }); - if (gameperson) { + if (gameperson.faction) { return { factionId: gameperson.faction.factionId, + factionName: gameperson.faction.factionName, }; } else { - throw new HttpException('No faction was found', HttpStatus.BAD_REQUEST); + throw new HttpException( + gameperson ? 'You are admin for this game!' : 'No faction was found', + HttpStatus.BAD_REQUEST, + ); } } }