From 5770fb2f3e46668b1fa4ac5891e9f8387abfe303 Mon Sep 17 00:00:00 2001 From: Kenny Dang Date: Thu, 9 Feb 2023 12:39:31 -0500 Subject: [PATCH] removed deleted thread from watched thread ID array --- src/community/community.service.ts | 49 ++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/community/community.service.ts b/src/community/community.service.ts index 8b85b646..f65bcd77 100644 --- a/src/community/community.service.ts +++ b/src/community/community.service.ts @@ -219,11 +219,56 @@ export class CommunityService { } async deleteThread(id: string) { - return await this.prisma.thread.delete({ + const getUsersWatching = await this.prisma.thread.findFirst({ where: { id + }, + include: { + usersWatching: true } - }); + }) + + if(!getUsersWatching) return new Error("Thread not found!") + else { + + const usersWatchingID = getUsersWatching.usersWatching.map((v) => v.id) + + usersWatchingID.map(async (u) => { + const user = await this.prisma.user.findFirst({ + where: { + id: u + }, + include: { + watchedThreads: true + } + }); + if(!user) return new Error("User's threads not found.") + else { + const filteredThreads = user.watchedThreads.filter((watcherID) => watcherID.id !== id) + + const filteredThreadIDs = filteredThreads.map((v) => v.id) + + console.log(filteredThreadIDs); + + this.prisma.user.update({ + where: { + id: user.id + }, + data: { + watchedThreadIDs: { + set: filteredThreadIDs + } + } + }) + } + }) + + return await this.prisma.thread.delete({ + where: { + id + } + }); + } } async addCommentToThread(parentThreadID: string, data: ICommentCreateInput) {