import { Body , Controller , Get , HttpCode , Param , Post , Query , UseGuards , } from '@nestjs/common' ; import { Constants } from '../../CommonsModule/constants' ; import { StartEventDTO } from '../dto/start-event.dto' ; import { GameficationService } from '../service/gamefication.service' ; import { NeedPolicies , NeedRoles , } from '../../CommonsModule/decorator/role-guard-metadata.decorator' ; import { RoleEnum } from '../../SecurityModule/enum/role.enum' ; import { RoleGuard } from '../../CommonsModule/guard/role.guard' ; import { OrderEnum } from '../../CommonsModule/enum/order.enum' ; import { TimeRangeEnum } from '../enum/time-range.enum' ; import { ApiBearerAuth , ApiOkResponse , ApiTags } from '@nestjs/swagger' ; import { RankingDTO } from '../dto/ranking.dto' ; import { UserIdParam } from '../../CommonsModule/guard/student-metadata.guard' ; import { StudentGuard } from '../../CommonsModule/guard/student.guard' ; import { Pageable } from '../../CommonsModule/dto/pageable.dto' ; import { RankingQueryDTO } from '../dto/ranking-query.dto' ; import { PageableRankingSwagger } from '../swagger/pageable-ranking.swagger' ; @ ApiTags ( 'Gamefication' ) @ Controller ( `${ Constants . API_PREFIX }/${ Constants . API_VERSION_1 }/${ Constants . GAMEFICATION_ENDPOINT }` ) export class GameficationController { constructor ( private readonly service ) { } @ Post ( 'start-event' ) @ HttpCode ( 200 ) @ UseGuards ( RoleGuard ) @ NeedRoles ( RoleEnum . STUDENT ) @ NeedPolicies ( `${ Constants . POLICIES_PREFIX }/START_EVENT` ) public startEvent ( @ Body ( ) body ) { this . service . startEvent ( body . event , body . rule ) ; } @ Get ( 'ranking' ) @ ApiOkResponse ( { type : PageableRankingSwagger } ) @ UseGuards ( RoleGuard ) @ NeedRoles ( RoleEnum . STUDENT , RoleEnum . ADMIN ) @ NeedPolicies ( `${ Constants . POLICIES_PREFIX }/GET_RANKING` ) public async getRanking ( @ Query ( 'order' ) order = OrderEnum . ASC , @ Query ( 'timeRange' ) timeRange = TimeRangeEnum . MONTH , @ Query ( 'limit' ) limit = 10 , @ Query ( 'page' ) page = 1 , @ Query ( 'institutionName' ) institutionName ? , @ Query ( 'city' ) city ? , @ Query ( 'state' ) state ? ) { return await this . service . getRanking ( order , timeRange , Number ( limit ) , Number ( page ) , institutionName , city , state ) ; } @ Get ( 'ranking/user/:userId' ) @ UserIdParam ( 'userId' ) @ UseGuards ( RoleGuard ) public async getUserRanking ( @ Param ( 'userId' ) userId , @ Query ( 'timeRange' ) timeRange = TimeRangeEnum . MONTH , @ Query ( 'institutionName' ) institutionName ? , @ Query ( 'city' ) city ? , @ Query ( 'state' ) state ? ) { return await this . service . getUserRanking ( userId , { timeRange , institutionName , city , state , } ) ; } @ Get ( 'ranking/user/:userId/total-points' ) @ UserIdParam ( 'userId' ) public async getUserTotalPoints ( @ Param ( 'userId' ) userId ) { const userTotalPoints = await this . service . getUserTotalPoints ( userId ) ; return userTotalPoints || { userId , points : '0' } ; } }