@@ -176,50 +176,46 @@ export async function POST(req: NextRequest) {
176
176
webhook_payload . entry [ 0 ] . changes [ 0 ] . value
177
177
) ;
178
178
179
+ // Handle both comment reply and DM in parallel
180
+ const actions = [ ] ;
181
+
179
182
if ( automation . listener . commentReply ) {
180
183
console . log ( "Replying to comment with template" ) ;
181
-
182
- const comment_reply = await replyToComment (
183
- webhook_payload . entry [ 0 ] . changes [ 0 ] . value . id ,
184
- automation . listener . commentReply ,
185
- automation . User ?. integrations [ 0 ] . token !
184
+ actions . push (
185
+ replyToComment (
186
+ webhook_payload . entry [ 0 ] . changes [ 0 ] . value . id ,
187
+ automation . listener . commentReply ,
188
+ automation . User ?. integrations [ 0 ] . token !
189
+ )
186
190
) ;
191
+ }
187
192
188
- if ( comment_reply . status === 200 ) {
189
- const tracked = await trackResponses ( automation . id , "COMMENT" ) ;
190
- if ( tracked ) {
191
- await trackAnalytics ( automation . userId ! , "comment" ) . catch ( console . error ) ;
192
- return NextResponse . json (
193
- {
194
- message : "Comment reply sent" ,
195
- } ,
196
- { status : 200 }
197
- ) ;
198
- }
199
- }
200
- } else {
201
- console . log ( "Sending private message" ) ;
202
- const direct_message = await sendPrivateMessage (
193
+ console . log ( "Sending private message" ) ;
194
+ actions . push (
195
+ sendPrivateMessage (
203
196
webhook_payload . entry [ 0 ] . id ,
204
197
webhook_payload . entry [ 0 ] . changes [ 0 ] . value . from . id ,
205
198
automation . listener ?. prompt ,
206
199
automation . User ?. integrations [ 0 ] . token !
207
- ) ;
200
+ )
201
+ ) ;
208
202
209
- if ( direct_message . status === 200 ) {
210
- const tracked = await trackResponses ( automation . id , "COMMENT" ) ;
203
+ const results = await Promise . all ( actions ) ;
204
+ const direct_message = results [ actions . length - 1 ] ;
211
205
212
- if ( tracked ) {
213
- await trackAnalytics ( automation . userId ! , "comment" ) . catch (
214
- console . error
215
- ) ;
216
- return NextResponse . json (
217
- {
218
- message : "Message sent" ,
219
- } ,
220
- { status : 200 }
221
- ) ;
222
- }
206
+ if ( direct_message . status === 200 ) {
207
+ const tracked = await trackResponses ( automation . id , "COMMENT" ) ;
208
+
209
+ if ( tracked ) {
210
+ await trackAnalytics ( automation . userId ! , "comment" ) . catch (
211
+ console . error
212
+ ) ;
213
+ return NextResponse . json (
214
+ {
215
+ message : "Messages sent successfully" ,
216
+ } ,
217
+ { status : 200 }
218
+ ) ;
223
219
}
224
220
}
225
221
}
0 commit comments