({ userId, chatId })
| 234 | } |
| 235 | |
| 236 | public static async clearHistory({ userId, chatId }): Promise<{ teamId: string }> { |
| 237 | if (!chatId) { |
| 238 | throw new Error('Bad data'); |
| 239 | } |
| 240 | |
| 241 | const existingChat = await this.findById(chatId).setOptions({ lean: true }); |
| 242 | |
| 243 | if (!existingChat) { |
| 244 | throw new Error('Not found'); |
| 245 | } |
| 246 | |
| 247 | await this.checkPermission({ |
| 248 | userId, |
| 249 | teamId: existingChat.teamId, |
| 250 | chat: existingChat, |
| 251 | }); |
| 252 | |
| 253 | const messagesToBeRemoved = await Message.find({ chatId }).setOptions({ lean: true }); |
| 254 | |
| 255 | const messageIdsToBeRemoved = messagesToBeRemoved.map((m) => { |
| 256 | return m['_id'].toString(); |
| 257 | }); |
| 258 | |
| 259 | // remove all messages and files if any |
| 260 | for (const messageId of messageIdsToBeRemoved) { |
| 261 | await Message.deleteForClearHistory({ userId, id: messageId }); |
| 262 | } |
| 263 | |
| 264 | // remove messageId from each user's unreadByUserMessageIds |
| 265 | for (const id of existingChat.chatParticipantIds.filter((id) => id !== userId)) { |
| 266 | const user = await User.findOne({ _id: id }).setOptions({ lean: true }); |
| 267 | |
| 268 | const unreadByUserMessageIds = user.unreadByUserMessageIds.filter((uci) => { |
| 269 | return !messageIdsToBeRemoved.includes(uci); |
| 270 | }); |
| 271 | |
| 272 | await User.findOneAndUpdate( |
| 273 | { _id: id }, |
| 274 | { $set: { unreadByUserMessageIds } }, |
| 275 | { runValidators: true }, |
| 276 | ).setOptions({ lean: true }); |
| 277 | } |
| 278 | |
| 279 | return { teamId: existingChat.teamId }; |
| 280 | } |
| 281 | |
| 282 | // search within one Chat |
| 283 | // add chatId as argument |
nothing calls this directly
no test coverage detected