11import axios from 'axios' ;
2- import { setTimeout } from 'timers/promises' ;
32import { type ServerlessFunctionConfig } from 'twenty-sdk/application' ;
43
54const TWENTY_API_KEY = process . env . TWENTY_API_KEY ?? '' ;
65const TWENTY_URL =
76 process . env . TWENTY_API_URL !== '' && process . env . TWENTY_API_URL !== undefined
87 ? `${ process . env . TWENTY_API_URL } /rest`
98 : 'https://api.twenty.com/rest' ;
10- const DELAY = 500 ;
119
1210const create_last_interaction = ( id : string ) => {
1311 return {
@@ -100,6 +98,53 @@ const interactionData = (date: string, status: string) => {
10098 } ;
10199} ;
102100
101+ const updateInteractionStatus = async ( objectName : string , id : string , messageDate : string , status : string ) => {
102+ const options = {
103+ method : 'PATCH' ,
104+ url : `${ TWENTY_URL } /${ objectName } /${ id } ` ,
105+ headers : {
106+ 'Content-Type' : 'application/json' ,
107+ Authorization : `Bearer ${ TWENTY_API_KEY } ` ,
108+ } ,
109+ data : {
110+ lastInteraction : messageDate ,
111+ interactionStatus : status
112+ }
113+ } ;
114+ try {
115+ const response = await axios . request ( options ) ;
116+ if ( response . status === 200 ) {
117+ console . log ( 'Successfully updated company last interaction field' ) ;
118+ }
119+ } catch ( error ) {
120+ if ( axios . isAxiosError ( error ) ) {
121+ throw error ;
122+ }
123+ throw error ;
124+ }
125+ }
126+
127+ const fetchRelatedCompanyId = async ( id : string ) => {
128+ const options = {
129+ method : 'GET' ,
130+ url : `${ TWENTY_URL } /people/${ id } ` ,
131+ headers : {
132+ Authorization : `Bearer ${ TWENTY_API_KEY } ` ,
133+ } ,
134+ } ;
135+ try {
136+ const req = await axios . request ( options ) ;
137+ if ( req . status === 200 && req . data . person . companyId !== null && req . data . person . companyId !== undefined ) {
138+ return req . data . person . companyId ;
139+ }
140+ } catch ( error ) {
141+ if ( axios . isAxiosError ( error ) ) {
142+ throw error ;
143+ }
144+ throw error ;
145+ }
146+ }
147+
103148export const main = async ( params : {
104149 properties : Record < string , any > ;
105150 recordId : string ;
@@ -147,7 +192,6 @@ export const main = async (params: {
147192 if ( response2 . status === 201 ) {
148193 console . log ( 'Successfully created company last interaction field' ) ;
149194 }
150- await setTimeout ( DELAY ) ;
151195 }
152196 if ( company_interaction_status === undefined ) {
153197 const response2 = await axios . request (
@@ -156,7 +200,6 @@ export const main = async (params: {
156200 if ( response2 . status === 201 ) {
157201 console . log ( 'Successfully created company interaction status field' ) ;
158202 }
159- await setTimeout ( DELAY ) ;
160203 }
161204 if ( person_last_interaction === undefined ) {
162205 const response2 = await axios . request (
@@ -165,7 +208,6 @@ export const main = async (params: {
165208 if ( response2 . status === 201 ) {
166209 console . log ( 'Successfully created person last interaction field' ) ;
167210 }
168- await setTimeout ( DELAY ) ;
169211 }
170212 if ( person_interaction_status === undefined ) {
171213 const response2 = await axios . request (
@@ -174,11 +216,10 @@ export const main = async (params: {
174216 if ( response2 . status === 201 ) {
175217 console . log ( 'Successfully created person interaction status field' ) ;
176218 }
177- await setTimeout ( DELAY ) ;
178219 }
179220
180221 // Extract the timestamp of message
181- const messageDate = properties . receivedAt ;
222+ const messageDate = properties . after . receivedAt ;
182223 const interactionStatus = calculateStatus ( messageDate ) ;
183224
184225 // Get the details of person and related company
@@ -190,87 +231,30 @@ export const main = async (params: {
190231 } ,
191232 } ;
192233 const messageDetails = await axios . request ( messageOptions ) ;
193- await setTimeout ( DELAY ) ;
194- const peopleIds = [ ] ;
234+ const peopleIds : string [ ] = [ ] ;
195235 for ( const participant of messageDetails . data . messages
196236 . messageParticipants ) {
197237 peopleIds . push ( participant . personId ) ;
198238 }
199239
200240 const companiesIds = [ ] ;
201241 for ( const id of peopleIds ) {
202- const options = {
203- method : 'GET' ,
204- url : `${ TWENTY_URL } /people/${ id } ` ,
205- headers : {
206- Authorization : `Bearer ${ TWENTY_API_KEY } ` ,
207- } ,
208- } ;
209- try {
210- const req = await axios . request ( options ) ;
211- companiesIds . push ( req . data . person . companyId ) ;
212- await setTimeout ( DELAY ) ;
213- } catch ( error ) {
214- if ( axios . isAxiosError ( error ) ) {
215- throw error ;
216- }
217- throw error ;
218- }
242+ companiesIds . push ( await fetchRelatedCompanyId ( id ) ) ;
219243 }
220244 // Update the field value depending on the timestamp
221245 for ( const id of peopleIds ) {
222- const peopleOptions = {
223- method : 'PATCH' ,
224- url : `${ TWENTY_URL } /people/${ id } ` ,
225- headers : {
226- 'Content-Type' : 'application/json' ,
227- Authorization : `Bearer ${ TWENTY_API_KEY } ` ,
228- } ,
229- data : interactionData ( messageDate , interactionStatus ) ,
230- } ;
231- try {
232- const response = await axios . request ( options ) ;
233- if ( response . status === 200 ) {
234- console . log ( 'Successfully updated company last interaction field' ) ;
235- await setTimeout ( DELAY ) ;
236- }
237- } catch ( error ) {
238- if ( axios . isAxiosError ( error ) ) {
239- throw error ;
240- }
241- throw error ;
242- }
246+ await updateInteractionStatus ( "people" , id , messageDate , interactionStatus ) ;
243247 }
244248
245249 for ( const id of companiesIds ) {
246- const companiesOptions = {
247- method : 'PATCH' ,
248- url : `${ TWENTY_URL } /companies/${ id } ` ,
249- headers : {
250- 'Content-Type' : 'application/json' ,
251- Authorization : `Bearer ${ TWENTY_API_KEY } ` ,
252- } ,
253- data : interactionData ( messageDate , interactionStatus ) ,
254- } ;
255- try {
256- const req = await axios . request ( companiesOptions ) ;
257- if ( req . status === 200 ) {
258- console . log ( `Successfully updated company with ID ${ id } ` ) ;
259- await setTimeout ( DELAY ) ;
260- }
261- } catch ( error ) {
262- if ( axios . isAxiosError ( error ) ) {
263- throw error ;
264- }
265- throw error ;
266- }
250+ await updateInteractionStatus ( "companies" , id , messageDate , interactionStatus ) ;
267251 }
268252 } catch ( error ) {
269253 if ( axios . isAxiosError ( error ) ) {
270- console . error ( error ) ;
254+ console . error ( error . message ) ;
271255 return { } ;
272256 }
273- console . log ( error ) ;
257+ console . error ( error ) ;
274258 return { } ;
275259 }
276260} ;
0 commit comments