import { Connection , EntitySubscriberInterface , EventSubscriber , InsertEvent , UpdateEvent } from 'typeorm' ; import { Achievement } from '../entity/achievement.entity' ; import { NotificationService } from '../../NotificationModule/service/notification.service' ; import { NotificationTypeEnum } from '../../NotificationModule/enum/notification-type.enum' ; import { InjectConnection } from '@nestjs/typeorm' ; @ EventSubscriber ( ) export class AchievementSubscriber implements EntitySubscriberInterface < Achievement > { constructor ( @ InjectConnection ( ) readonly connection , private readonly notificationService ) { connection . subscribers . push ( this ) ; } listenTo ( ) { return Achievement ; } async afterInsert ( event ) { if ( ! event . entity . completed ) return ; await this . notificationService . create < Achievement > ( event . entity . user , NotificationTypeEnum . GAMEFICATION , event . entity ) ; } async afterUpdate ( event ) { if ( ! event . entity . completed ) return ; await this . notificationService . create < Achievement > ( event . entity . user , NotificationTypeEnum . GAMEFICATION , event . entity ) ; } }