@@ -62,21 +62,21 @@ export async function POST(req: Request) {
6262 const normalizeId = ( id ?: string ) => id ?. replace ( / [ < > ] / g, "" ) . trim ( ) ;
6363 const inReplyTo = normalizeId ( inReplyToHeader ) ;
6464
65- let matchedOutreachItem = null ;
65+ let matchedOutreachItem : any = null ;
6666
6767 // Match via In-Reply-To — try both normalized and raw formats
6868 if ( inReplyTo ) {
6969 // First try without angle brackets (normalized)
7070 matchedOutreachItem = await prismadb . crm_Outreach_Items . findFirst ( {
7171 where : { message_id : inReplyTo } ,
72- select : { id : true , campaign : true , lead : true , account_id : true , subject : true } ,
72+ select : { id : true , campaign : true , lead : true , account_id : true , subject : true , sender_email : true } ,
7373 } ) ;
7474
7575 // Also try with angle brackets (some providers store them that way)
7676 if ( ! matchedOutreachItem && inReplyToHeader ) {
7777 matchedOutreachItem = await prismadb . crm_Outreach_Items . findFirst ( {
7878 where : { message_id : inReplyToHeader . trim ( ) } ,
79- select : { id : true , campaign : true , lead : true , account_id : true , subject : true } ,
79+ select : { id : true , campaign : true , lead : true , account_id : true , subject : true , sender_email : true } ,
8080 } ) ;
8181 }
8282 }
@@ -89,7 +89,7 @@ export async function POST(req: Request) {
8989 if ( allRefs . length > 0 ) {
9090 matchedOutreachItem = await prismadb . crm_Outreach_Items . findFirst ( {
9191 where : { message_id : { in : allRefs } } ,
92- select : { id : true , campaign : true , lead : true , account_id : true , subject : true } ,
92+ select : { id : true , campaign : true , lead : true , account_id : true , subject : true , sender_email : true } ,
9393 } ) ;
9494 }
9595 }
@@ -106,6 +106,18 @@ export async function POST(req: Request) {
106106 } ) ;
107107 }
108108
109+ // Resolve the correct user for routing: prefer sender_email lookup over campaign owner
110+ let resolvedUserId = campaignRecord ?. user || undefined ;
111+ if ( matchedOutreachItem ?. sender_email ) {
112+ try {
113+ const senderUser = await prismadb . users . findFirst ( {
114+ where : { email : matchedOutreachItem . sender_email } ,
115+ select : { id : true } ,
116+ } ) ;
117+ if ( senderUser ) resolvedUserId = senderUser . id ;
118+ } catch { /* fall back to campaign owner */ }
119+ }
120+
109121 let finalLeadId = matchedOutreachItem ?. lead || undefined ;
110122 let sentimentResult : any = null ;
111123
@@ -119,7 +131,7 @@ export async function POST(req: Request) {
119131 originalSubject : matchedOutreachItem . subject || subject ,
120132 leadName : isAccountOnly ? "Account Contact" : undefined ,
121133 } ,
122- campaignRecord ?. user || "sysadm"
134+ resolvedUserId || "sysadm"
123135 ) ;
124136
125137 if ( isAccountOnly && sentimentResult ?. extractedContact ) {
@@ -155,7 +167,7 @@ export async function POST(req: Request) {
155167 const newThread = await prismadb . crm_Email_Thread . create ( {
156168 data : {
157169 team_id : campaignRecord ?. team_id || "600000000000000000000000" ,
158- user : campaignRecord ?. user || undefined ,
170+ user : resolvedUserId ,
159171 lead : finalLeadId ,
160172 campaign : matchedOutreachItem ?. campaign || undefined ,
161173 outreach_item : matchedOutreachItem ?. id || undefined ,
0 commit comments