({ userId, commentId, fileName, fileUrl })
| 362 | } |
| 363 | |
| 364 | public static async addFile({ userId, commentId, fileName, fileUrl }) { |
| 365 | if (!userId || !commentId || !fileName || !fileUrl) { |
| 366 | throw new Error('Bad data'); |
| 367 | } |
| 368 | |
| 369 | const doc = await this.findOne({ _id: commentId }).setOptions({ lean: true }); |
| 370 | |
| 371 | if (!doc) { |
| 372 | throw new Error('Not found'); |
| 373 | } |
| 374 | |
| 375 | await this.checkPermission({ userId, discussionId: doc.discussionId }); |
| 376 | |
| 377 | const commentWithFile = await this.findOneAndUpdate( |
| 378 | { _id: commentId }, |
| 379 | { |
| 380 | $push: { |
| 381 | files: { |
| 382 | fileName, |
| 383 | fileUrl, |
| 384 | addedAt: new Date(), |
| 385 | }, |
| 386 | }, |
| 387 | }, |
| 388 | { runValidators: true, new: true }, |
| 389 | ).setOptions({ |
| 390 | lean: true, |
| 391 | }); |
| 392 | |
| 393 | return commentWithFile.files.find((f) => { |
| 394 | return f.fileUrl === fileUrl; |
| 395 | }); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | mongoSchema.loadClass(CommentClass); |
nothing calls this directly
no test coverage detected