MCPcopy Create free account
hub / github.com/async-labs/async / delete

Method delete

api/server/models/Discussion.ts:233–282  ·  view source on GitHub ↗
({ userId, discussionId })

Source from the content-addressed store, hash-verified

231
232 // review
233 public static async delete({ userId, discussionId }): Promise<any> {
234 if (!discussionId) {
235 throw new Error('Bad data');
236 }
237
238 const existingDiscussion = await this.findById(discussionId).setOptions({ lean: true });
239
240 if (!existingDiscussion) {
241 throw new Error('Not found');
242 }
243
244 await this.checkPermission({
245 userId,
246 teamId: existingDiscussion.teamId,
247 discussion: existingDiscussion,
248 });
249
250 const commentsToBeRemoved = await Comment.find({
251 discussionId: existingDiscussion._id.toString(),
252 }).setOptions({ lean: true });
253
254 const commentIdsToBeRemoved = commentsToBeRemoved.map((c) => {
255 return c['_id'].toString();
256 });
257
258 // remove all comments and files if any
259 for (const commentId of commentIdsToBeRemoved) {
260 await Comment.delete({ userId, id: commentId, isDiscussionBeingDeleted: true });
261 }
262
263 // remove discussion document
264 await this.deleteOne({ _id: discussionId });
265
266 // remove commentId from each user's unreadCommentIds
267 for (const id of existingDiscussion.discussionMemberIds.filter((id) => id !== userId)) {
268 const user = await User.findOne({ _id: id }).setOptions({ lean: true });
269
270 const unreadCommentIds = user.unreadCommentIds.filter((uci) => {
271 return !commentIdsToBeRemoved.includes(uci);
272 });
273
274 await User.findOneAndUpdate(
275 { _id: id },
276 { $set: { unreadCommentIds } },
277 { runValidators: true },
278 ).setOptions({ lean: true });
279 }
280
281 return { teamId: existingDiscussion.teamId };
282 }
283
284 // inside `app`, make sure that Comment cannot be added to the Archived Discussion
285 public static async archive({ userId, discussionId, action }): Promise<any> {

Callers

nothing calls this directly

Calls 2

checkPermissionMethod · 0.95
deleteMethod · 0.65

Tested by

no test coverage detected