feat: Implement in-app notifications with new database model, enhance Firebase initialization to support service account files, and add message read functionalities.

This commit is contained in:
pradeepkumar
2026-02-25 06:45:27 +05:30
parent 36ccc8c303
commit 7b8d6e3db8
7 changed files with 289 additions and 63 deletions

View File

@@ -10,7 +10,6 @@ import {
import { Server, Socket } from 'socket.io';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { MessagesService } from './messages.service';
import { CreateMessageDto } from './dto';
import { Logger } from '@nestjs/common';
@@ -38,7 +37,6 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
private readonly messagesService: MessagesService,
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
private readonly eventEmitter: EventEmitter2,
) {}
/**
@@ -188,19 +186,6 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
? participants.agentUserId
: participants.userId;
this.sendToUser(otherUserId, 'new_message', message);
// Emit push notification event
const senderProfile = message.sender?.agentProfile || message.sender?.userProfile;
const senderName = senderProfile
? `${senderProfile.firstName || ''} ${senderProfile.lastName || ''}`.trim()
: 'Someone';
this.eventEmitter.emit('notification.message', {
recipientUserId: otherUserId,
senderName,
messagePreview: data.message.content?.substring(0, 100) || '',
conversationId: data.conversationId,
});
}
return { success: true, message };

View File

@@ -4,13 +4,17 @@ import {
ForbiddenException,
BadRequestException,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { PrismaService } from '../prisma/prisma.service';
import { CreateMessageDto } from './dto';
import { ConnectionStatus, MessageStatus, MessageType, UserRole } from '@prisma/client';
@Injectable()
export class MessagesService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly eventEmitter: EventEmitter2,
) {}
/**
* Validate if a user can message an agent (must have ACCEPTED connection)
@@ -223,6 +227,23 @@ export class MessagesService {
},
});
// Emit push notification event for the recipient
const recipientUserId = isUser
? conversation.agentProfile.userId
: conversation.userId;
const senderProfile =
message.sender?.agentProfile || message.sender?.userProfile;
const senderName = senderProfile
? `${senderProfile.firstName || ''} ${senderProfile.lastName || ''}`.trim()
: 'Someone';
this.eventEmitter.emit('notification.message', {
recipientUserId,
senderName,
messagePreview: dto.content?.substring(0, 100) || '',
conversationId,
});
return message;
}