feat: Add endpoint and service logic to clear all messages within a conversation.
This commit is contained in:
@@ -615,6 +615,49 @@ export class MessagesService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all messages in a conversation (keep the conversation itself)
|
||||
*/
|
||||
async clearChat(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
|
||||
await this.prisma.message.deleteMany({
|
||||
where: { conversationId },
|
||||
});
|
||||
|
||||
// Reset conversation preview
|
||||
await this.prisma.conversation.update({
|
||||
where: { id: conversationId },
|
||||
data: {
|
||||
lastMessageAt: null,
|
||||
lastMessageText: null,
|
||||
userUnreadCount: 0,
|
||||
agentUnreadCount: 0,
|
||||
},
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a conversation and all its messages
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user