Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed deleted thread from watched thread ID array #445

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down