import * as PubSub from 'pubsub-js' ; import { REQUEST } from '@nestjs/core' ; import { Request } from 'express' ; import { JwtService } from '@nestjs/jwt' ; import { Inject , Injectable } from '@nestjs/common' ; import { TestTry } from './course-rewards.service' ; import { EventNameEnum } from '../enum/event-name.enum' ; import { User } from '../../UserModule/entity/user.entity' ; import { StartEventEnum } from '../enum/start-event.enum' ; import { StartEventRules } from '../dto/start-event-rules.dto' ; import { RoleEnum } from '../../SecurityModule/enum/role.enum' ; import { InviteUserRewardData } from './user-rewards.service' ; import { CourseTaken } from '../../CourseModule/entity/course-taken.entity' ; import { CourseNpsRewardDTO } from '../dto/course-nps-reward.dto' ; import { CMSTestDTO } from '../../CourseModule/dto/cms-test.dto' ; @ Injectable ( ) export class PublisherService { constructor ( @ Inject ( REQUEST ) private request , private readonly jwtService ) { } public startEvent ( eventName , rule ) { const authorizationHeader = this . request . headers . authorization ; const userStringToken = this . getUserStringToken ( authorizationHeader ) ; const user = this . getUserFromToken ( userStringToken ) ; if ( user . role . name !== RoleEnum . STUDENT ) return ; const events = { [ StartEventEnum . SHARE_COURSE ] : EventNameEnum . USER_REWARD_SHARE_COURSE , [ StartEventEnum . RATE_APP ] : EventNameEnum . USER_REWARD_RATE_APP , [ StartEventEnum . SHARE_APP ] : EventNameEnum . USER_REWARD_SHARE_APP , } ; const event = events [ eventName ] ; if ( ! event ) return ; PubSub . publish ( event , rule ) ; } public emitInviteUserReward ( inviteKey ) { const data = { inviteKey , } ; PubSub . publish ( EventNameEnum . USER_REWARD_INVITE_USER , data ) ; } public emitCheckTestReward ( test , chosenAlternative ) { const authorizationHeader = this . request . headers . authorization ; const userStringToken = this . getUserStringToken ( authorizationHeader ) ; const user = this . getUserFromToken ( userStringToken ) ; if ( user . role . name !== RoleEnum . STUDENT ) return ; const data = { chosenAlternative : chosenAlternative . toLowerCase ( ) , userId : user . id , test , } ; PubSub . publish ( EventNameEnum . COURSE_REWARD_TEST_ON_FIRST_TAKE , data ) ; } public emitupdateStudent ( id ) { PubSub . publish ( EventNameEnum . USER_REWARD_COMPLETE_REGISTRATION , { id } ) ; } public emitNpsReward ( userId , courseId ) { const data = { userId , courseId , } ; PubSub . publish ( EventNameEnum . COURSE_REWARD_COURSE_NPS , data ) ; } public emitCourseCompleted ( course ) { PubSub . publish ( EventNameEnum . COURSE_REWARD_COMPLETE_COURSE , course ) ; } private getUserStringToken ( authorizationHeader ) { const [ , userStringToken ] = authorizationHeader . split ( ' ' ) ; return userStringToken ; } private getUserFromToken ( userStringToken ) { return this . jwtService . verify < User > ( userStringToken , { ignoreExpiration : true , } ) ; } }