({ userId, commentId, fileUrl })
| 295 | } |
| 296 | |
| 297 | public static async deleteFile({ userId, commentId, fileUrl }) { |
| 298 | if (!commentId || !fileUrl) { |
| 299 | throw new Error('Bad data'); |
| 300 | } |
| 301 | |
| 302 | const doc = await this.findById(commentId).setOptions({ lean: true }); |
| 303 | |
| 304 | if (!doc) { |
| 305 | throw new Error('Not found'); |
| 306 | } |
| 307 | |
| 308 | await this.checkPermission({ userId, discussionId: doc.discussionId }); |
| 309 | |
| 310 | await this.findOneAndUpdate( |
| 311 | { _id: commentId }, |
| 312 | { |
| 313 | $pull: { files: { fileUrl } }, |
| 314 | }, |
| 315 | { runValidators: true }, |
| 316 | ); |
| 317 | |
| 318 | const filesToDeleteFromS3 = [fileUrl]; |
| 319 | |
| 320 | deleteFiles(filesToDeleteFromS3).catch((err) => console.log(err)); |
| 321 | } |
| 322 | |
| 323 | private static async checkPermission({ userId, discussionId, doc = null }) { |
| 324 | if (!userId || !discussionId) { |
nothing calls this directly
no test coverage detected