import { ChannelTypeEnum , IMessage } from '@notifire/shared' ; import { FilterQuery , PopulateOptions } from 'mongoose' ; import { BaseRepository } from '../base-repository' ; import { MessageEntity } from './message.entity' ; import { Message } from './message.schema' ; import { Notification } from '../notification' ; import { NotificationTemplateEntity } from '../notification-template' ; export class MessageRepository extends BaseRepository < MessageEntity > { constructor ( ) { super ( Message , MessageEntity ) ; } async findBySubscriberChannel ( applicationId , subscriberId , channel , options = { limit : 10 } ) { return await this . find ( { _applicationId : applicationId , _subscriberId : subscriberId , channel , } , '' , { limit : options . limit , skip : options . skip , sort : '-createdAt' , } ) ; } async getUnseenCount ( applicationId , subscriberId , channel ) { return await this . count ( { _applicationId : applicationId , _subscriberId : subscriberId , seen : false , channel , } ) ; } async changeSeenStatus ( subscriberId , messageId , isSeen ) { return this . update ( { _subscriberId : subscriberId , _id : messageId , } , { $set : { seen : isSeen , lastSeenDate : new Date ( ) , } , } ) ; } async getBulkMessagesByNotificationIds ( applicationId , notificationIds ) { return this . find ( { _applicationId : applicationId , _notificationId : { $in : notificationIds , } , } ) ; } async updateMessageStatus ( id , status , providerPayload = { } , errorId , errorText ) { return await this . update ( { _id : id , } , { $set : { status , errorId , errorText , providerPayload , } , } ) ; } async getFeed ( applicationId , query = { } , skip = 0 , limit = 10 ) { const requestQuery = { _applicationId : applicationId , } ; if ( query ?. channels ) { requestQuery . channel = { $in : query . channels , } ; } if ( query ?. templates ) { requestQuery . _templateId = { $in : query . templates , } ; } if ( query ?. subscriberId ) { requestQuery . _subscriberId = query ?. subscriberId ; } const totalCount = await this . count ( requestQuery ) ; const response = await Message . find ( requestQuery ) . populate ( 'subscriber' , 'firstName _id lastName email' ) . populate ( 'template' , 'name _id' ) . skip ( skip ) . limit ( limit ) . sort ( '-createdAt' ) ; return { totalCount , data : this . mapEntities ( response ) , } ; } }