1
1
import algoliasearch from 'algoliasearch'
2
- import payload from 'payload'
3
2
4
- const appID = process . env . NEXT_PUBLIC_ALGOLIA_CH_ID || ''
5
- const apiKey = process . env . NEXT_PRIVATE_ALGOLIA_API_KEY || ''
6
- const indexName = process . env . NEXT_PUBLIC_ALGOLIA_CH_INDEX_NAME || ''
3
+ const {
4
+ NEXT_PRIVATE_ALGOLIA_API_KEY ,
5
+ NEXT_PUBLIC_ALGOLIA_CH_ID ,
6
+ NEXT_PUBLIC_ALGOLIA_CH_INDEX_NAME ,
7
+ NEXT_PUBLIC_CMS_URL ,
8
+ } = process . env
9
+
10
+ const appID = NEXT_PUBLIC_ALGOLIA_CH_ID || ''
11
+ const apiKey = NEXT_PRIVATE_ALGOLIA_API_KEY || ''
12
+ const indexName = NEXT_PUBLIC_ALGOLIA_CH_INDEX_NAME || ''
7
13
8
14
const client = algoliasearch ( appID , apiKey )
9
15
@@ -39,16 +45,17 @@ export const syncToAlgolia = async (): Promise<void> => {
39
45
throw new Error ( 'Algolia environment variables are not set' )
40
46
}
41
47
42
- const { docs } = await payload . find ( {
43
- collection : 'community-help' ,
44
- limit : 30000 ,
45
- } )
48
+ const communityHelpThreads = await fetch (
49
+ `${ NEXT_PUBLIC_CMS_URL } /api/community-help?limit=0` ,
50
+ ) . then ( ( res ) => res . json ( ) )
51
+
52
+ const docs = communityHelpThreads ?. docs
46
53
47
54
const discordDocs : DiscordDoc [ ] = [ ]
48
55
const githubDocs : GithubDoc [ ] = [ ]
49
56
50
57
docs . forEach ( ( doc ) => {
51
- const { communityHelpJSON, discordID, githubID, helpful } = doc as any
58
+ const { communityHelpJSON, discordID, githubID, helpful } = doc
52
59
53
60
if ( discordID ) {
54
61
const { slug, info, intro, messageCount, messages } = communityHelpJSON
@@ -60,9 +67,11 @@ export const syncToAlgolia = async (): Promise<void> => {
60
67
helpful : helpful ?? false ,
61
68
messageCount,
62
69
messages : messages . map ( ( message ) => {
63
- return {
64
- author : message . authorName ,
65
- content : message . content ,
70
+ if ( message ) {
71
+ return {
72
+ author : message . authorName ,
73
+ content : message . content ,
74
+ }
66
75
}
67
76
} ) ,
68
77
objectID : info . id ,
0 commit comments