| 205 | where: { id: chatId, isDeleted: false }, |
| 206 | }); |
| 207 | |
| 208 | if (chat) { |
| 209 | chat.isDeleted = true; |
| 210 | chat.isActive = false; |
| 211 | await this.chatRepository.save(chat); |
| 212 | return true; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | async clearChatHistory(chatId: string): Promise<boolean> { |
| 218 | // Queued with the appends: clearing does not read the array first, but an |
| 219 | // append that started before the clear would write its stale copy back |
| 220 | // afterwards and undo it. |
| 221 | return ChatService.serialise(chatId, async () => { |
| 222 | const chat = await this.chatRepository.findOne({ |
| 223 | where: { id: chatId, isDeleted: false }, |
| 224 | }); |
| 225 | if (chat) { |
| 226 | chat.messages = []; |
| 227 | chat.updatedAt = new Date(); |
| 228 | await this.chatRepository.save(chat); |
| 229 | return true; |
| 230 | } |
| 231 | return false; |
| 232 | }); |
| 233 | } |
| 234 | |