@@ -9,8 +9,11 @@ import {
99  convoToSpaces , 
1010  convos , 
1111  emailIdentities , 
12+   orgMembers , 
1213  orgs , 
1314  postalServers , 
15+   spaceMembers , 
16+   teams , 
1417  type  ConvoEntryMetadata 
1518}  from  '@u22n/database/schema' ; 
1619import  { 
@@ -797,71 +800,90 @@ export const worker = createWorker<MailProcessorJobData>(
797800            // @ts -expect-error we check and define earlier up 
798801            fromAddressParticipantId  =  contactParticipant . id ; 
799802          }  else  if  ( fromAddressPlatformObject . type  ===  'emailIdentity' )  { 
800-             await  discord . info ( 
801-               `🚪 Adding participants from internal email identity with messages sent from external email services not supported yet, convoId: ${ convoId } ${ fromAddressParticipantId }  
802-             ) ; 
803-             //! TODO: How do we handle adding the participant to the convo if we only track spaces? 
804-             //! leave code below for ref and quick revert if needed 
805-             // we need to get the first person/team in the routing rule and add them to the convo 
806-             // const emailIdentityParticipant = 
807-             //   await db.query.emailIdentities.findFirst({ 
808-             //     where: and( 
809-             //       eq(emailIdentities.orgId, orgId), 
810-             //       eq(emailIdentities.id, fromAddressPlatformObject?.id) 
811-             //     ), 
812-             //     columns: { 
813-             //       id: true 
814-             //     }, 
815-             //     with: { 
816-             //       routingRules: { 
817-             //         columns: { 
818-             //           id: true 
819-             //         }, 
820-             //         with: { 
821-             //           destinations: { 
822-             //             columns: { 
823-             //               spaceId: true, 
824-             //             } 
825-             //           } 
826-             //         } 
827-             //       } 
828-             //     } 
829-             //   }); 
830-             // const firstDestination = 
831-             //   // @ts -expect-error, taken form old code, will rewrite later 
832-             //   emailIdentityParticipant.routingRules.destinations[0]!; 
833-             // let convoParticipantFromAddressIdentity; 
834-             // if (firstDestination.orgMemberId) { 
835-             //   convoParticipantFromAddressIdentity = 
836-             //     await db.query.convoParticipants.findFirst({ 
837-             //       where: and( 
838-             //         eq(convoParticipants.orgId, orgId), 
839-             //         eq(convoParticipants.convoId, convoId), 
840- 
841-             //         eq( 
842-             //           convoParticipants.orgMemberId, 
843-             //           firstDestination.orgMemberId 
844-             //         ) 
845-             //       ), 
846-             //       columns: { 
847-             //         id: true 
848-             //       } 
849-             //     }); 
850-             // } else if (firstDestination.teamId) { 
851-             //   convoParticipantFromAddressIdentity = 
852-             //     await db.query.convoParticipants.findFirst({ 
853-             //       where: and( 
854-             //         eq(convoParticipants.orgId, orgId), 
855-             //         eq(convoParticipants.convoId, convoId || 0), 
856-             //         eq(convoParticipants.teamId, firstDestination.teamId) 
857-             //       ), 
858-             //       columns: { 
859-             //         id: true 
860-             //       } 
861-             //     }); 
862-             // } 
863- 
864-             // fromAddressParticipantId = convoParticipantFromAddressIdentity.id; 
803+             const  emailIdentity  =  await  db . query . emailIdentities . findFirst ( { 
804+               where : and ( 
805+                 eq ( emailIdentities . orgId ,  orgId ) , 
806+                 eq ( emailIdentities . id ,  fromAddressPlatformObject ?. id ) 
807+               ) , 
808+               columns : { 
809+                 id : true 
810+               } , 
811+               with : { 
812+                 authorizedSenders : { 
813+                   with : { 
814+                     orgMember : true , 
815+                     team : true , 
816+                     space : true 
817+                   } 
818+                 } 
819+               } 
820+             } ) ; 
821+ 
822+             if  ( ! emailIdentity )  { 
823+               throw  new  Error ( 'No email identity participant found' ) ; 
824+             } 
825+ 
826+             const  ownerMember  =  await  db . query . orgMembers . findFirst ( { 
827+               where : and ( 
828+                 eq ( orgMembers . orgId ,  orgId ) , 
829+                 eq ( orgMembers . defaultEmailIdentityId ,  emailIdentity . id ) 
830+               ) 
831+             } ) ; 
832+ 
833+             if  ( ownerMember )  { 
834+               fromAddressParticipantId  =  ownerMember . id ; 
835+             }  else  { 
836+               const  ownerTeam  =  await  db . query . teams . findFirst ( { 
837+                 where : and ( 
838+                   eq ( teams . orgId ,  orgId ) , 
839+                   eq ( teams . defaultEmailIdentityId ,  emailIdentity . id ) 
840+                 ) 
841+               } ) ; 
842+ 
843+               if  ( ownerTeam )  { 
844+                 fromAddressParticipantId  =  ownerTeam . id ; 
845+               } 
846+             } 
847+ 
848+             // if we still don't have a participant, then we need to find the first person/team in the authorized senders or first member of the first space 
849+             if  ( ! fromAddressParticipantId )  { 
850+               if  ( emailIdentity . authorizedSenders . length )  { 
851+                 const  firstAuthorizedSender  = 
852+                   emailIdentity . authorizedSenders [ 0 ] ; 
853+                 if  ( firstAuthorizedSender ?. orgMember )  { 
854+                   fromAddressParticipantId  =  firstAuthorizedSender . orgMember . id ; 
855+                 } 
856+                 if  ( firstAuthorizedSender ?. team )  { 
857+                   fromAddressParticipantId  =  firstAuthorizedSender . team . id ; 
858+                 } 
859+                 if  ( firstAuthorizedSender ?. space )  { 
860+                   const  spaceMember  =  await  db . query . spaceMembers . findFirst ( { 
861+                     where : and ( 
862+                       eq ( spaceMembers . spaceId ,  firstAuthorizedSender . space . id ) 
863+                     ) , 
864+                     with : { 
865+                       orgMember : true , 
866+                       team : true 
867+                     } 
868+                   } ) ; 
869+                   if  ( spaceMember ?. team )  { 
870+                     fromAddressParticipantId  =  spaceMember . team . id ; 
871+                   }  else  if  ( spaceMember ?. orgMember )  { 
872+                     fromAddressParticipantId  =  spaceMember . orgMember . id ; 
873+                   } 
874+                 } 
875+               }  else  { 
876+                 throw  new  Error ( 
877+                   'No authorized senders found for email identity' 
878+                 ) ; 
879+               } 
880+             } 
881+ 
882+             if  ( ! fromAddressParticipantId )  { 
883+               throw  new  Error ( 
884+                 `Failed to find a from address participant for email identity ${ fromAddressPlatformObject ?. email }  
885+               ) ; 
886+             } 
865887          } 
866888        } 
867889
0 commit comments