({ userId, id, isDiscussionBeingDeleted })
| 255 | } |
| 256 | |
| 257 | public static async delete({ userId, id, isDiscussionBeingDeleted }) { |
| 258 | if (!id) { |
| 259 | throw new Error('Bad data'); |
| 260 | } |
| 261 | |
| 262 | const doc = await this.findById(id).setOptions({ lean: true }); |
| 263 | |
| 264 | if (!doc) { |
| 265 | throw new Error('Not found'); |
| 266 | } |
| 267 | |
| 268 | const { discussion } = await this.checkPermission({ userId, discussionId: doc.discussionId }); |
| 269 | |
| 270 | const isFirstDoc = |
| 271 | (await this.countDocuments({ _id: { $lt: doc._id }, discussionId: doc.discussionId })) === 0; |
| 272 | |
| 273 | if (isFirstDoc && !isDiscussionBeingDeleted) { |
| 274 | throw new Error( |
| 275 | 'You can not delete first comment of a discussion but you can delete discussion itself.', |
| 276 | ); |
| 277 | } |
| 278 | |
| 279 | await this.deleteOne({ _id: id }); |
| 280 | |
| 281 | const arrayOfFileUrls = doc.files.map((f) => { |
| 282 | return f['fileUrl']; |
| 283 | }); |
| 284 | |
| 285 | if (arrayOfFileUrls.length > 0) { |
| 286 | deleteFiles(arrayOfFileUrls).catch((err) => console.log(err)); |
| 287 | } |
| 288 | |
| 289 | await User.markCommentAsReadForParticipantsOfDiscussionAfterCommentIsDeleted({ |
| 290 | userIdsToNotify: discussion.discussionMemberIds.filter((id) => id !== userId), |
| 291 | commentId: id, |
| 292 | }); |
| 293 | |
| 294 | return { userIdsToNotify: discussion.discussionMemberIds.filter((id) => id !== userId) }; |
| 295 | } |
| 296 | |
| 297 | public static async deleteFile({ userId, commentId, fileUrl }) { |
| 298 | if (!commentId || !fileUrl) { |
nothing calls this directly
no test coverage detected