({ userId, messageId, fileName, fileUrl })
| 457 | } |
| 458 | |
| 459 | public static async addFile({ userId, messageId, fileName, fileUrl }) { |
| 460 | if (!userId || !messageId || !fileName || !fileUrl) { |
| 461 | throw new Error('Bad data'); |
| 462 | } |
| 463 | |
| 464 | const doc = await this.findOne({ _id: messageId }).setOptions({ lean: true }); |
| 465 | |
| 466 | if (!doc) { |
| 467 | throw new Error('Not found'); |
| 468 | } |
| 469 | |
| 470 | await this.checkPermission({ userId, chatId: doc.chatId }); |
| 471 | |
| 472 | const messageWithFile = await this.findOneAndUpdate( |
| 473 | { _id: messageId }, |
| 474 | { |
| 475 | $push: { |
| 476 | files: { |
| 477 | fileName, |
| 478 | fileUrl, |
| 479 | addedAt: new Date(), |
| 480 | }, |
| 481 | }, |
| 482 | }, |
| 483 | { runValidators: true, new: true }, |
| 484 | ).setOptions({ |
| 485 | lean: true, |
| 486 | }); |
| 487 | |
| 488 | return messageWithFile.files.find((f) => { |
| 489 | return f.fileUrl === fileUrl; |
| 490 | }); |
| 491 | } |
| 492 | |
| 493 | private static async checkPermission({ userId, chatId, doc = null }) { |
| 494 | if (!userId || !chatId) { |
nothing calls this directly
no test coverage detected