From 2914e7494764ae7aa2a3322d0e1e1cd5a790f103 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Fri, 20 Mar 2026 02:45:27 +0530 Subject: [PATCH] feat: Implement connection request response notifications and emit them upon status update. --- .../connection-requests.service.ts | 21 +++++++++++++- src/notifications/notifications.service.ts | 29 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/connection-requests/connection-requests.service.ts b/src/connection-requests/connection-requests.service.ts index d2c72ce..1add53d 100644 --- a/src/connection-requests/connection-requests.service.ts +++ b/src/connection-requests/connection-requests.service.ts @@ -241,7 +241,7 @@ export class ConnectionRequestsService { throw new BadRequestException('This request has already been responded to'); } - return this.prisma.connectionRequest.update({ + const updated = await this.prisma.connectionRequest.update({ where: { id: requestId }, data: { status: dto.status as ConnectionStatus, @@ -257,6 +257,25 @@ export class ConnectionRequestsService { }, }, }); + + // Get agent name for notification + const agentProfile = await this.prisma.agentProfile.findUnique({ + where: { id: agentProfileId }, + select: { firstName: true, lastName: true }, + }); + const agentName = agentProfile + ? `${agentProfile.firstName || ''} ${agentProfile.lastName || ''}`.trim() + : 'An agent'; + + // Notify the user about the accept/reject + this.eventEmitter.emit('notification.connection_response', { + recipientUserId: request.userId, + agentName, + status: dto.status, + connectionRequestId: requestId, + }); + + return updated; } /** diff --git a/src/notifications/notifications.service.ts b/src/notifications/notifications.service.ts index c058de4..599d9e9 100644 --- a/src/notifications/notifications.service.ts +++ b/src/notifications/notifications.service.ts @@ -444,4 +444,33 @@ export class NotificationsService implements OnModuleInit { }, ); } + + @OnEvent('notification.connection_response') + async handleConnectionResponseNotification(event: { + recipientUserId: string; + agentName: string; + status: string; + connectionRequestId: string; + }) { + const isAccepted = event.status === 'ACCEPTED'; + const prefix = await this.getRoutePrefix(event.recipientUserId); + const actionUrl = isAccepted ? `${prefix}/message` : `${prefix}/profiles`; + + await this.createAndSendNotification( + event.recipientUserId, + 'connection', + isAccepted ? 'Connection Accepted' : 'Connection Declined', + isAccepted + ? `${event.agentName} accepted your connection request. You can now send messages.` + : `${event.agentName} declined your connection request.`, + 'new_lead_alerts', + actionUrl, + { + type: 'connection_response', + status: event.status, + connectionRequestId: event.connectionRequestId, + link: actionUrl, + }, + ); + } }