| 121 | } |
| 122 | |
| 123 | class CommentClass extends mongoose.Model { |
| 124 | public static async getList({ userId, discussionId }) { |
| 125 | await this.checkPermission({ userId, discussionId }); |
| 126 | |
| 127 | const filter = { discussionId }; |
| 128 | |
| 129 | return this.find(filter).sort({ createdAt: 1 }).setOptions({ lean: true }); |
| 130 | } |
| 131 | |
| 132 | // add arguments: teamId, socketId |
| 133 | public static async addOrEdit({ content, discussionId, teamId, userId, id, files }) { |
| 134 | if (!content || !teamId) { |
| 135 | throw new Error('Bad data'); |
| 136 | } |
| 137 | |
| 138 | const { isSubscriptionActiveForAccount, isTrialPeriodOverForAccount } = |
| 139 | await User.getSubscriptionStatus(userId); |
| 140 | |
| 141 | if (isTrialPeriodOverForAccount && !isSubscriptionActiveForAccount) { |
| 142 | throw new Error('This team is not subscribed to a paid plan and free trial period is over.'); |
| 143 | } |
| 144 | |
| 145 | if (id) { |
| 146 | const editedComment = await this.edit({ |
| 147 | content, |
| 148 | discussionId, |
| 149 | userId, |
| 150 | id, |
| 151 | }); |
| 152 | |
| 153 | // no realtime update for editing comment |
| 154 | return { comment: editedComment, userIdsToNotify: [] }; |
| 155 | } else { |
| 156 | const { discussion } = await this.checkPermission({ userId, discussionId }); |
| 157 | |
| 158 | const htmlContent = markdownToHtml(content); |
| 159 | |
| 160 | const newComment = await this.create({ |
| 161 | createdUserId: userId, |
| 162 | discussionId, |
| 163 | content, |
| 164 | htmlContent, |
| 165 | isEdited: false, |
| 166 | createdAt: new Date(), |
| 167 | lastEditedAt: new Date(), |
| 168 | }); |
| 169 | |
| 170 | const isFirstComment = |
| 171 | (await this.countDocuments({ |
| 172 | discussionId: newComment.discussionId, |
| 173 | })) === 1; |
| 174 | |
| 175 | if (isFirstComment) { |
| 176 | Discussion.updateOne( |
| 177 | { _id: discussionId }, |
| 178 | { lastUpdatedAt: new Date(), $set: { firstCommentId: newComment._id.toString() } }, |
| 179 | ).exec(); |
| 180 | } else { |
nothing calls this directly
no outgoing calls
no test coverage detected