import { InsightsService } from './insights.service' ; import { Controller , UseGuards , UseInterceptors , ClassSerializerInterceptor , Get , Param , Query , BadRequestException , Body , Delete , Patch , } from '@nestjs/common' ; import { JwtAuthGuard } from 'guards/jwt-auth.guard' ; import { Connection } from 'typeorm' ; import { GetInsightOutputResponse , ERROR_MESSAGES , ListInsightsResponse , Role , } from '@koh/common' ; import { User } from '../decorators/user.decorator' ; import { INSIGHTS_MAP } from './insight-objects' ; import { UserModel } from 'profile/user.entity' ; import { Roles } from 'decorators/roles.decorator' ; import { CourseRole } from '../decorators/course-role.decorator' ; import { Filter } from './insight-objects' ; @ Controller ( 'insights' ) @ UseGuards ( JwtAuthGuard ) @ UseInterceptors ( ClassSerializerInterceptor ) export class InsightsController { constructor ( private connection , private insightsService ) { } @ Get ( ':courseId/:insightName' ) async get ( @ CourseRole ( ) role , @ Param ( 'courseId' ) courseId , @ Param ( 'insightName' ) insightName , @ Query ( 'start' ) start , @ Query ( 'end' ) end ) { const insightNames = Object . keys ( INSIGHTS_MAP ) ; if ( ! insightNames . includes ( insightName ) ) { throw new BadRequestException ( ERROR_MESSAGES . insightsController . insightNameNotFound ) ; } if ( ! INSIGHTS_MAP [ insightName ] . roles . includes ( role ) ) { throw new BadRequestException ( ERROR_MESSAGES . insightsController . insightUnathorized ) ; } const filters = [ { type : 'courseId' , courseId , } , ] ; if ( start && end ) { filters . push ( { type : 'timeframe' , start : new Date ( start ) , end : new Date ( end ) , } ) ; } const insight = await this . insightsService . computeOutput ( { insight : INSIGHTS_MAP [ insightName ] , filters , } ) ; return insight ; } @ Get ( 'list' ) @ Roles ( Role . PROFESSOR ) async getAllInsights ( ) { return this . insightsService . convertToInsightsListResponse ( Object . keys ( INSIGHTS_MAP ) ) ; } @ Patch ( '' ) @ Roles ( Role . PROFESSOR ) async toggleInsightOn ( @ Body ( ) body , @ User ( ) user ) { await this . insightsService . toggleInsightOn ( user , body . insightName ) ; return ; } @ Delete ( '' ) @ Roles ( Role . PROFESSOR ) async toggleInsightOff ( @ Body ( ) body , @ User ( ) user ) { await this . insightsService . toggleInsightOff ( user , body . insightName ) ; return ; } }