({ userId, chatId, doc = null })
| 491 | } |
| 492 | |
| 493 | private static async checkPermission({ userId, chatId, doc = null }) { |
| 494 | if (!userId || !chatId) { |
| 495 | throw new Error('Bad data. You have no permission.'); |
| 496 | } |
| 497 | |
| 498 | if (doc && doc.createdUserId !== userId) { |
| 499 | throw new Error('You do not have permission.'); |
| 500 | } |
| 501 | |
| 502 | const chat = await Chat.findById(chatId).setOptions({ lean: true }); |
| 503 | |
| 504 | const teamLeader = await User.findOne({ |
| 505 | 'teamsForTeamLeader.teamId': chat.teamId, |
| 506 | }).setOptions({ |
| 507 | lean: true, |
| 508 | }); |
| 509 | |
| 510 | const team = teamLeader.teamsForTeamLeader.find((team) => { |
| 511 | return team.teamId === chat.teamId; |
| 512 | }); |
| 513 | |
| 514 | if (!chat && (teamLeader._id.toString() === userId || team.idsOfTeamMembers.includes(userId))) { |
| 515 | return { team }; |
| 516 | } |
| 517 | |
| 518 | if ( |
| 519 | chat && |
| 520 | (teamLeader._id.toString() === userId || team.idsOfTeamMembers.includes(userId)) && |
| 521 | (chat.chatParticipantIds === userId || chat.chatParticipantIds.includes(userId)) |
| 522 | ) { |
| 523 | return { team, chat }; |
| 524 | } |
| 525 | |
| 526 | throw new Error('Permission denied'); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | mongoSchema.loadClass(MessageClass); |
no outgoing calls
no test coverage detected