feat: Automatically mark messages as delivered when recipients come online or receive new messages.

This commit is contained in:
pradeepkumar
2026-03-28 02:02:49 +05:30
parent 0abfa51ed9
commit 1697d638b6
2 changed files with 78 additions and 0 deletions

View File

@@ -528,6 +528,52 @@ export class MessagesService {
});
}
/**
* Mark all SENT messages addressed to a user as DELIVERED (when they come online)
* Returns the updated messages grouped by conversation for notification purposes
*/
async markPendingMessagesAsDelivered(userId: string): Promise<{ conversationId: string; senderId: string; messageIds: string[]; deliveredAt: Date }[]> {
const now = new Date();
// Find all conversations where this user is a participant
const conversations = await this.prisma.conversation.findMany({
where: {
OR: [
{ userId },
{ agentProfile: { userId } },
],
},
select: { id: true, userId: true, agentProfile: { select: { userId: true } } },
});
const results: { conversationId: string; senderId: string; messageIds: string[]; deliveredAt: Date }[] = [];
for (const conv of conversations) {
const otherUserId = conv.userId === userId ? conv.agentProfile.userId : conv.userId;
// Find SENT messages from the other user in this conversation
const sentMessages = await this.prisma.message.findMany({
where: {
conversationId: conv.id,
senderId: otherUserId,
status: MessageStatus.SENT,
},
select: { id: true },
});
if (sentMessages.length > 0) {
const messageIds = sentMessages.map((m) => m.id);
await this.prisma.message.updateMany({
where: { id: { in: messageIds } },
data: { status: MessageStatus.DELIVERED, deliveredAt: now },
});
results.push({ conversationId: conv.id, senderId: otherUserId, messageIds, deliveredAt: now });
}
}
return results;
}
/**
* Get conversation by ID with full details
*/