import { Injectable , NotFoundException } from '@nestjs/common' ; import { NotificationRepository } from '../repository/notification.repository' ; import { NotificationTypeEnum } from '../enum/notification-type.enum' ; import { Notification } from '../entity/notification.entity' ; import { User } from '../../UserModule/entity/user.entity' ; import { OrderEnum } from '../../CommonsModule/enum/order.enum' ; import { CreateNotificationDTO } from '../dto/create-notification.dto' ; @ Injectable ( ) export class NotificationService { constructor ( private readonly repository ) { } public async create < T = { new ( ) ; } | Record < string | number | symbol , unknown > > ( user , type , content , options = { important : false } ) { return this . repository . save ( { user , type , content , ... options } ) ; } public async getNotificationsByUser ( user ) { return this . repository . find ( { where : user } ) ; } public async getNotificationsByUserId ( userId , order , enabled , seen ) { return this . repository . getNotificationsByUserId ( userId , order , enabled , seen ) ; } public async getSemearNotSeenAndEanbledNotificationByUserId ( userId ) { const otherNotSeenNotifications = await this . repository . getOtherNotificationsByUserId ( userId ) ; return otherNotSeenNotifications . find ( ( notification ) => { const content = JSON . parse ( notification . content as string ) ; return content . semearSiteUrl != null ; } ) ; } public async setSeeNotification ( id ) { const notification = await this . findById ( id ) ; await this . repository . save ( { ... notification , enabled : false , seen : true } ) ; } public async findById ( id ) { const response = await this . repository . find ( { id } ) ; if ( ! response . length ) { throw new NotFoundException ( 'Notification not found' ) ; } return response [ 0 ] ; } public async addNotificationToUser ( userId , notification ) { return this . repository . save ( { ... notification , user : { id : userId } , seen : false , enabled : true , } ) ; } }