feat: Add endpoint and service logic to delete conversations and their associated messages.
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
Patch,
|
Patch,
|
||||||
|
Delete,
|
||||||
Body,
|
Body,
|
||||||
Param,
|
Param,
|
||||||
Query,
|
Query,
|
||||||
@@ -196,6 +197,28 @@ export class MessagesController {
|
|||||||
return this.messagesService.markMessagesAsRead(conversationId, userId);
|
return this.messagesService.markMessagesAsRead(conversationId, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// DELETE CONVERSATION
|
||||||
|
// ==========================================
|
||||||
|
@Delete('conversations/:id')
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
|
@ApiOperation({ summary: 'Delete a conversation and all its messages' })
|
||||||
|
@ApiParam({ name: 'id', description: 'Conversation ID' })
|
||||||
|
@ApiResponse({
|
||||||
|
status: 200,
|
||||||
|
description: 'Conversation deleted',
|
||||||
|
})
|
||||||
|
@ApiResponse({
|
||||||
|
status: 404,
|
||||||
|
description: 'Conversation not found',
|
||||||
|
})
|
||||||
|
async deleteConversation(
|
||||||
|
@Param('id') conversationId: string,
|
||||||
|
@CurrentUser('id') userId: string,
|
||||||
|
) {
|
||||||
|
return this.messagesService.deleteConversation(conversationId, userId);
|
||||||
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// GET UNREAD COUNT
|
// GET UNREAD COUNT
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|||||||
@@ -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
|
* Get total unread count for a user across all conversations
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user