From 273592923e3f2a367e87ee67f90a989060a90935 Mon Sep 17 00:00:00 2001 From: ZHallen122 Date: Mon, 28 Oct 2024 17:02:54 -0400 Subject: [PATCH] do soft delete --- backend/src/chat/chat.service.ts | 55 ++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/backend/src/chat/chat.service.ts b/backend/src/chat/chat.service.ts index 03bc232..a90e336 100644 --- a/backend/src/chat/chat.service.ts +++ b/backend/src/chat/chat.service.ts @@ -168,24 +168,38 @@ export class ChatService { async getChatHistory(chatId: string): Promise { const chat = await this.chatRepository.findOne({ - where: { id: chatId }, - order: { createdAt: 'ASC' }, + where: { id: chatId, isDeleted: false }, relations: ['messages'], }); + + if (chat && chat.messages) { + // Sort messages by createdAt in ascending order + chat.messages = chat.messages + .filter((message) => !message.isDeleted) + .sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()); + } + return chat ? chat.messages : []; } async getMessageById(messageId: string): Promise { return await this.messageRepository.findOne({ - where: { id: messageId }, + where: { id: messageId, isDeleted: false }, }); } async getChatDetails(chatId: string): Promise { - return this.chatRepository.findOne({ - where: { id: chatId }, + const chat = await this.chatRepository.findOne({ + where: { id: chatId, isDeleted: false }, relations: ['messages'], }); + + if (chat) { + // Filter out messages that are soft-deleted + chat.messages = chat.messages.filter((message) => !message.isDeleted); + } + + return chat; } async createChat(userId: string, newChatInput: NewChatInput): Promise { @@ -208,17 +222,38 @@ export class ChatService { } async deleteChat(chatId: string): Promise { - const result = await this.chatRepository.delete(chatId); - return result.affected > 0; + const chat = await this.chatRepository.findOne({ + where: { id: chatId, isDeleted: false }, + relations: ['messages'], + }); + + if (chat) { + // Soft delete the chat + chat.isDeleted = true; + chat.isActive = false; + await this.chatRepository.save(chat); + + // Soft delete all associated messages + await this.messageRepository.update( + { chat: { id: chatId }, isDeleted: false }, + { isDeleted: true, isActive: false }, + ); + + return true; + } + return false; } async clearChatHistory(chatId: string): Promise { const chat = await this.chatRepository.findOne({ - where: { id: chatId }, + where: { id: chatId, isDeleted: false }, relations: ['messages'], }); if (chat) { - chat.messages = []; + await this.messageRepository.update( + { chat: { id: chatId }, isDeleted: false }, + { isDeleted: true, isActive: false }, + ); chat.updatedAt = new Date(); await this.chatRepository.save(chat); return true; @@ -230,7 +265,7 @@ export class ChatService { upateChatTitleInput: UpdateChatTitleInput, ): Promise { const chat = await this.chatRepository.findOne({ - where: { id: upateChatTitleInput.chatId }, + where: { id: upateChatTitleInput.chatId, isDeleted: false }, }); if (chat) { chat.title = upateChatTitleInput.title;