@@ -43,12 +43,20 @@ export default {
4343 } ,
4444 methods : {
4545 ...common . methods ,
46- _getLastProcessedRecord ( listId ) {
47- const key = `list_${ listId } _last_record ` ;
46+ _getAfterToken ( listId ) {
47+ const key = `list_${ listId } _after_token ` ;
4848 return this . db . get ( key ) ;
4949 } ,
50- _setLastProcessedRecord ( listId , recordId ) {
51- const key = `list_${ listId } _last_record` ;
50+ _setAfterToken ( listId , afterToken ) {
51+ const key = `list_${ listId } _after_token` ;
52+ this . db . set ( key , afterToken ) ;
53+ } ,
54+ _getLastRecordId ( listId ) {
55+ const key = `list_${ listId } _last_record_id` ;
56+ return this . db . get ( key ) ;
57+ } ,
58+ _setLastRecordId ( listId , recordId ) {
59+ const key = `list_${ listId } _last_record_id` ;
5260 this . db . set ( key , recordId ) ;
5361 } ,
5462 getTs ( ) {
@@ -67,48 +75,76 @@ export default {
6775 async getContactDetails ( contactIds ) {
6876 if ( ! contactIds . length ) return { } ;
6977
78+ const uniqueContactIds = [
79+ ...new Set ( contactIds ) ,
80+ ] ;
81+
7082 const { properties = [ ] } = this ;
7183 const allProperties = [
72- ...DEFAULT_CONTACT_PROPERTIES ,
73- ...properties ,
84+ ...new Set ( [
85+ ...DEFAULT_CONTACT_PROPERTIES ,
86+ ...properties ,
87+ ] ) ,
7488 ] ;
7589
90+ const chunks = [ ] ;
91+ const chunkSize = 100 ;
92+ for ( let i = 0 ; i < uniqueContactIds . length ; i += chunkSize ) {
93+ chunks . push ( uniqueContactIds . slice ( i , i + chunkSize ) ) ;
94+ }
95+
96+ const contactMap = { } ;
97+
98+ const chunkPromises = chunks . map ( async ( chunk ) => {
99+ try {
100+ const { results } = await this . hubspot . batchGetObjects ( {
101+ objectType : "contacts" ,
102+ data : {
103+ inputs : chunk . map ( ( id ) => ( {
104+ id,
105+ } ) ) ,
106+ properties : allProperties ,
107+ } ,
108+ } ) ;
109+ return results ;
110+ } catch ( error ) {
111+ console . warn ( "Error fetching contact details for chunk:" , error ) ;
112+ return [ ] ;
113+ }
114+ } ) ;
115+
76116 try {
77- const { results } = await this . hubspot . batchGetObjects ( {
78- objectType : "contacts" ,
79- data : {
80- inputs : contactIds . map ( ( id ) => ( {
81- id,
82- } ) ) ,
83- properties : allProperties ,
84- } ,
85- } ) ;
117+ const chunkResults = await Promise . all ( chunkPromises ) ;
86118
87- const contactMap = { } ;
88- results . forEach ( ( contact ) => {
89- contactMap [ contact . id ] = contact ;
119+ chunkResults . forEach ( ( results ) => {
120+ results . forEach ( ( contact ) => {
121+ contactMap [ contact . id ] = contact ;
122+ } ) ;
90123 } ) ;
124+
91125 return contactMap ;
92126 } catch ( error ) {
93- console . warn ( "Error fetching contact details:" , error ) ;
127+ console . warn ( "Error processing contact details:" , error ) ;
94128 return { } ;
95129 }
96130 } ,
97131 async processListMemberships ( listId , listInfo ) {
98- const lastProcessedRecord = this . _getLastProcessedRecord ( listId ) ;
132+ const afterToken = this . _getAfterToken ( listId ) ;
133+ const lastRecordId = this . _getLastRecordId ( listId ) ;
99134 const newMemberships = [ ] ;
100135
101136 let params = {
102137 limit : DEFAULT_LIMIT ,
103138 } ;
104139
105- if ( lastProcessedRecord ) {
106- params . after = lastProcessedRecord ;
140+ if ( afterToken ) {
141+ params . after = afterToken ;
107142 }
108143
109144 try {
110145 let hasMore = true ;
111- let latestRecordId = lastProcessedRecord ;
146+ let latestAfterToken = afterToken ;
147+ let latestRecordId = lastRecordId ;
112148
113149 while ( hasMore ) {
114150 const {
@@ -124,6 +160,10 @@ export default {
124160 }
125161
126162 for ( const membership of results ) {
163+ if ( lastRecordId && membership . recordId === lastRecordId ) {
164+ continue ;
165+ }
166+
127167 newMemberships . push ( {
128168 membership,
129169 listInfo,
@@ -132,14 +172,18 @@ export default {
132172 }
133173
134174 if ( paging ?. next ?. after ) {
175+ latestAfterToken = paging . next . after ;
135176 params . after = paging . next . after ;
136177 } else {
137178 hasMore = false ;
138179 }
139180 }
140181
182+ if ( latestAfterToken !== afterToken ) {
183+ this . _setAfterToken ( listId , latestAfterToken ) ;
184+ }
141185 if ( latestRecordId ) {
142- this . _setLastProcessedRecord ( listId , latestRecordId ) ;
186+ this . _setLastRecordId ( listId , latestRecordId ) ;
143187 }
144188 } catch ( error ) {
145189 console . error ( `Error processing list ${ listId } :` , error ) ;
0 commit comments