Skip to content

Commit c4b4575

Browse files
committed
chore: remove duplicate threads from CH
1 parent 14a719a commit c4b4575

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

src/app/(frontend)/api/sync-ch/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import clearDuplicateThreads from '@root/scripts/clearDuplicateThreads'
12
import { NextResponse } from 'next/server'
23

34
import fetchDiscord from '../../../../scripts/fetchDiscord'
@@ -8,6 +9,7 @@ export const maxDuration = 300 // 5 mins (max on vercel pro plan)
89
export const dynamic = 'force-dynamic'
910

1011
export async function GET(): Promise<NextResponse> {
12+
await clearDuplicateThreads()
1113
await fetchDiscord()
1214
await fetchGitHub()
1315
await syncToAlgolia()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { qs } from '@root/utilities/qs'
2+
import { cookies } from 'next/headers'
3+
4+
const { NEXT_PUBLIC_CMS_URL } = process.env
5+
6+
async function clearDuplicateThreads() {
7+
const existingThreads = await fetch(`${NEXT_PUBLIC_CMS_URL}/api/community-help?depth=0&limit=0`)
8+
.then((res) => res.json())
9+
.then((data) =>
10+
data.docs.map((thread) => ({
11+
id: thread.communityHelpType === 'discord' ? thread.discordID : thread.githubID, // Use respective IDs
12+
communityHelpType: thread.communityHelpType,
13+
uniqueID: thread.id,
14+
})),
15+
)
16+
17+
const threadGroups = existingThreads.reduce((acc, thread) => {
18+
const groupId = thread.id
19+
acc[groupId] = acc[groupId] || []
20+
acc[groupId].push(thread)
21+
return acc
22+
}, {})
23+
24+
const threadsToDelete: string[] = []
25+
const cleanedThreads = Object.values(threadGroups).map((group: any[]) => {
26+
if (group.length > 1) {
27+
threadsToDelete.push(...group.slice(1).map((thread) => thread.uniqueID)) // Flatten by spreading
28+
}
29+
return group[0]
30+
})
31+
32+
const cookieStore = await cookies()
33+
const token = cookieStore.get('payload-token')
34+
35+
if (!token) {
36+
throw new Error('You are unauthorized, please log in.')
37+
}
38+
39+
const batchNumber = 10
40+
for (let i = 0; i < threadsToDelete.length; i += batchNumber) {
41+
const batch = threadsToDelete.slice(i, i + batchNumber)
42+
await Promise.all(
43+
batch.map(async (id) => {
44+
try {
45+
const res = await fetch(
46+
`${NEXT_PUBLIC_CMS_URL}/api/community-help?${qs.stringify({
47+
depth: 0,
48+
where: { id: { equals: id } },
49+
})}`,
50+
{
51+
credentials: 'include',
52+
headers: {
53+
Authorization: `JWT ${token.value}`,
54+
'Content-Type': 'application/json',
55+
},
56+
method: 'DELETE',
57+
},
58+
)
59+
if (!res.ok) {
60+
throw new Error(`Failed to delete thread with ID: ${id}`)
61+
}
62+
} catch (error) {
63+
console.error('Error deleting thread:', error)
64+
}
65+
}),
66+
)
67+
}
68+
}
69+
70+
export default clearDuplicateThreads

src/scripts/fetchDiscord.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ async function fetchDiscord() {
216216
)
217217

218218
const filteredThreads = allThreads.filter((thread) => {
219-
const existingThread = existingThreadIDs.find((existing) => existing.id === thread.id)
220-
return !existingThread || existingThread.messageCount !== thread.message_count
219+
const existingThread = existingThreadIDs.some((existing) => existing.id === thread.id)
220+
return (
221+
!existingThread || (existingThread && existingThread.messageCount !== thread.message_count)
222+
)
221223
})
222224

223225
bar.start(filteredThreads.length, 0)

0 commit comments

Comments
 (0)