feat: Add endpoint and service logic to delete conversations and their associated messages.

This commit is contained in:
pradeepkumar
2026-03-10 00:10:38 +05:30
parent 5a88b0ffa6
commit c431c751e8
2 changed files with 59 additions and 0 deletions

View File

@@ -598,6 +598,42 @@ export class MessagesService {
};
}
/**
* Delete a conversation and all its messages
*/
async deleteConversation(conversationId: string, userId: string) {
const conversation = await this.prisma.conversation.findUnique({
where: { id: conversationId },
include: {
agentProfile: {
select: { userId: true },
},
},
});
if (!conversation) {
throw new NotFoundException('Conversation not found');
}
const isUser = conversation.userId === userId;
const isAgent = conversation.agentProfile.userId === userId;
if (!isUser && !isAgent) {
throw new ForbiddenException('You are not part of this conversation');
}
// Delete all messages first, then the conversation
await this.prisma.message.deleteMany({
where: { conversationId },
});
await this.prisma.conversation.delete({
where: { id: conversationId },
});
return null;
}
/**
* Get total unread count for a user across all conversations
*/