From 151f84780e9b854b24a1ab46aa49ded27f52f09a Mon Sep 17 00:00:00 2001 From: L4168 <L4168@student.jamk.fi> Date: Thu, 27 Jun 2019 12:41:53 +0300 Subject: [PATCH] added fetch tasks service, updated socket --- src/task/task.controller.ts | 11 ++++++++++- src/task/task.module.ts | 9 +++++++-- src/task/task.service.ts | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/task/task.controller.ts b/src/task/task.controller.ts index db6be91..2e04789 100644 --- a/src/task/task.controller.ts +++ b/src/task/task.controller.ts @@ -7,10 +7,13 @@ import { UseGuards, Param, } from '@nestjs/common'; + import { TaskService } from './task.service'; +import { CreateTaskDTO, EditTaskDTO } from './task.dto'; +import { AuthGuard } from '../shared/auth.guard'; import { Roles } from '../shared/roles.decorator'; import { ValidationPipe } from '../shared/validation.pipe'; -import { CreateTaskDTO, EditTaskDTO } from './task.dto'; +import { User } from 'src/user/user.decorator'; @Controller('task') export class TaskController { @@ -33,4 +36,10 @@ export class TaskController { async editTask(@Param('id') id: string, @Body() data: EditTaskDTO) { return this.taskService.editTask(data); } + + @Get('get-tasks/:id') + @UseGuards(new AuthGuard()) + async fetchTasks(@User('id') person, @Param('id') id: string) { + return this.taskService.fetchTasks(person, id); + } } diff --git a/src/task/task.module.ts b/src/task/task.module.ts index b71b4d9..12112f9 100644 --- a/src/task/task.module.ts +++ b/src/task/task.module.ts @@ -4,10 +4,15 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { TaskService } from './task.service'; import { TaskController } from './task.controller'; import { TaskEntity } from './task.entity'; -import { FactionEntity } from 'src/game/faction.entity'; +import { FactionEntity } from '../game/faction.entity'; +import { Game_PersonEntity } from '../game/game.entity'; +import { NotificationModule } from '../notifications/notifications.module'; @Module({ - imports: [TypeOrmModule.forFeature([TaskEntity, FactionEntity])], + imports: [ + TypeOrmModule.forFeature([TaskEntity, FactionEntity, Game_PersonEntity]), + NotificationModule, + ], controllers: [TaskController], providers: [TaskService], }) diff --git a/src/task/task.service.ts b/src/task/task.service.ts index 7df6dee..9d4fe9a 100644 --- a/src/task/task.service.ts +++ b/src/task/task.service.ts @@ -1,9 +1,12 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; + import { TaskEntity } from './task.entity'; import { CreateTaskDTO, EditTaskDTO } from './task.dto'; import { FactionEntity } from '../game/faction.entity'; +import { Game_PersonEntity } from '../game/game.entity'; +import { NotificationGateway } from '../notifications/notifications.gateway'; @Injectable() export class TaskService { @@ -12,6 +15,9 @@ export class TaskService { private taskRepository: Repository<TaskEntity>, @InjectRepository(FactionEntity) private factionRepository: Repository<FactionEntity>, + @InjectRepository(Game_PersonEntity) + private gamePersonRepository: Repository<Game_PersonEntity>, + private notificationGateway: NotificationGateway, ) {} async newTask(task: CreateTaskDTO) { @@ -28,6 +34,12 @@ export class TaskService { const createdTask = await this.taskRepository.create(task); console.log(createdTask); await this.taskRepository.insert(createdTask); + // notify subscribers about a new task + // if faction was set it notifies only faction members, else everyone + this.notificationGateway.server.emit( + task.faction != null ? task.faction.toString() : 'global', + 'new task', + ); return { message: 'Task added', }; @@ -56,4 +68,13 @@ export class TaskService { message: 'Task updated and closed', }; } + + async fetchTasks(user, taskGame) { + // to do: limit fetch for factions + const gamePerson = await this.gamePersonRepository.findOne({ + person: user, + game: taskGame, + }); + return await this.taskRepository.find({ taskGame: taskGame }); + } } -- GitLab