@@ -11,7 +11,7 @@ export default {
1111 name : "New Contact Added to List" ,
1212 description :
1313 "Emit new event when a contact is added to a HubSpot list. [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/lists#get-%2Fcrm%2Fv3%2Flists%2F%7Blistid%7D%2Fmemberships%2Fjoin-order)" ,
14- version : "0.0.10 " ,
14+ version : "0.0.1 " ,
1515 type : "source" ,
1616 dedupe : "unique" ,
1717 props : {
@@ -44,13 +44,14 @@ export default {
4444 } ,
4545 methods : {
4646 ...common . methods ,
47+ _getKey ( listId ) {
48+ return `list_${ listId } _last_timestamp` ;
49+ } ,
4750 _getLastMembershipTimestamp ( listId ) {
48- const key = `list_${ listId } _last_timestamp` ;
49- return this . db . get ( key ) ;
51+ return this . db . get ( this . _getKey ( listId ) ) ;
5052 } ,
5153 _setLastMembershipTimestamp ( listId , timestamp ) {
52- const key = `list_${ listId } _last_timestamp` ;
53- this . db . set ( key , timestamp ) ;
54+ this . db . set ( this . _getKey ( listId ) , timestamp ) ;
5455 } ,
5556 getTs ( ) {
5657 return Date . now ( ) ;
@@ -78,24 +79,41 @@ export default {
7879 ...properties ,
7980 ] ;
8081
82+ const chunks = [ ] ;
83+ const chunkSize = 100 ;
84+ for ( let i = 0 ; i < contactIds . length ; i += chunkSize ) {
85+ chunks . push ( contactIds . slice ( i , i + chunkSize ) ) ;
86+ }
87+
88+ const contactMap = { } ;
89+
8190 try {
82- const { results } = await this . hubspot . batchGetObjects ( {
83- objectType : "contacts" ,
84- data : {
85- inputs : contactIds . map ( ( id ) => ( {
86- id,
87- } ) ) ,
88- properties : allProperties ,
89- } ,
90- } ) ;
91-
92- const contactMap = { } ;
93- results . forEach ( ( contact ) => {
94- contactMap [ contact . id ] = contact ;
95- } ) ;
91+ for ( const chunk of chunks ) {
92+ try {
93+ const { results } = await this . hubspot . batchGetObjects ( {
94+ objectType : "contacts" ,
95+ data : {
96+ inputs : chunk . map ( ( id ) => ( {
97+ id,
98+ } ) ) ,
99+ properties : allProperties ,
100+ } ,
101+ } ) ;
102+
103+ results . forEach ( ( contact ) => {
104+ contactMap [ contact . id ] = contact ;
105+ } ) ;
106+ } catch ( error ) {
107+ console . warn (
108+ `Error fetching contact details for chunk of ${ chunk . length } contacts:` ,
109+ error ,
110+ ) ;
111+ }
112+ }
113+
96114 return contactMap ;
97115 } catch ( error ) {
98- console . warn ( "Error fetching contact details:" , error ) ;
116+ console . warn ( "Error processing contact details:" , error ) ;
99117 return { } ;
100118 }
101119 } ,
@@ -126,25 +144,29 @@ export default {
126144 params,
127145 } ) ;
128146
129- if ( ! results || results . length === 0 ) {
147+ if ( ! results ) {
148+ console . warn (
149+ `No results returned from API for list ${ listId } - possible API issue` ,
150+ ) ;
151+ break ;
152+ }
153+
154+ if ( results . length === 0 ) {
130155 break ;
131156 }
132157
133158 for ( const membership of results ) {
134159 const { membershipTimestamp } = membership ;
135160
136- if (
137- new Date ( membershipTimestamp ) > new Date ( lastMembershipTimestamp )
138- ) {
161+ if ( membershipTimestamp > lastMembershipTimestamp ) {
139162 newMemberships . push ( {
140163 membership,
141164 listInfo,
142165 } ) ;
143166
144167 if (
145168 ! latestMembershipTimestamp ||
146- new Date ( membershipTimestamp ) >
147- new Date ( latestMembershipTimestamp )
169+ membershipTimestamp > latestMembershipTimestamp
148170 ) {
149171 latestMembershipTimestamp = membershipTimestamp ;
150172 }
@@ -168,7 +190,10 @@ export default {
168190 return newMemberships ;
169191 } ,
170192 async processResults ( ) {
171- const { listId } = this ;
193+ const {
194+ listId,
195+ listInfo : { name } ,
196+ } = this ;
172197
173198 if ( ! listId ) {
174199 console . warn ( "No list selected to monitor" ) ;
@@ -177,7 +202,7 @@ export default {
177202
178203 const listInfo = {
179204 listId,
180- name : `List ${ listId } ` ,
205+ name : `List ${ name } ` ,
181206 } ;
182207
183208 try {
@@ -203,7 +228,7 @@ export default {
203228 contactId : membership . recordId ,
204229 contact : contactDetail ,
205230 membership,
206- addedAt : new Date ( ) . toISOString ( ) ,
231+ addedAt : membership . membershipTimestamp ,
207232 } ;
208233
209234 const meta = this . generateMeta ( membership , listInfo ) ;
0 commit comments