@@ -7,7 +7,11 @@ import planer from 'planer';
77import EmailSignatureCache from '../../services/cache/EmailSignatureCache' ;
88import { Contact } from '../../db/types' ;
99import logger from '../../utils/logger' ;
10- import { isUsefulSignatureContent , pushNotificationDB } from './utils' ;
10+ import {
11+ isUsefulSignatureContent ,
12+ pushNotificationDB ,
13+ upsertSignaturesDB
14+ } from './utils' ;
1115import { ExtractSignature } from '../../services/signature/types' ;
1216import { DomainStatusVerificationFunction } from '../../services/extractors/engines/EmailMessage' ;
1317import { CleanQuotedForwardedReplies } from '../../utils/helpers/emailParsers' ;
@@ -79,11 +83,13 @@ export class EmailSignatureProcessor {
7983 contacts : Partial < Contact > [ ] | null ;
8084 } > {
8185 const { userId, miningId, data : payload } = data ;
82- const { from, messageDate } = payload . header ?? { } ;
86+ const { from, messageDate, rawHeader } = payload . header ?? { } ;
8387
8488 const shouldProcess = await this . isWorthProcessing ( data ) ;
8589
8690 if ( shouldProcess ) {
91+ const [ messageId ] = rawHeader [ 'message-id' ] ;
92+
8793 this . logging . debug ( 'Processing new signature' , {
8894 userId,
8995 miningId,
@@ -96,6 +102,7 @@ export class EmailSignatureProcessor {
96102 miningId ,
97103 from ?. address ,
98104 payload . body ,
105+ messageId ,
99106 messageDate
100107 ) ;
101108 }
@@ -110,24 +117,36 @@ export class EmailSignatureProcessor {
110117
111118 if ( extracted . length ) {
112119 try {
120+ const signatures = extracted . map (
121+ ( [ personEmail , messageId , rawSignature , extractedSignature ] ) => ( {
122+ userId,
123+ personEmail,
124+ messageId,
125+ rawSignature,
126+ extractedSignature,
127+ details : { miningId }
128+ } )
129+ ) ;
130+
131+ await upsertSignaturesDB ( this . supabase , signatures ) ;
132+
113133 await pushNotificationDB ( this . supabase , {
114134 userId,
115135 type : 'signature' ,
116136 details : {
117- extracted,
118137 signatures : extracted . length
119138 }
120139 } ) ;
121140 } catch ( err ) {
122141 this . logging . error (
123- `Error when pushing notifications: ${ ( err as Error ) . message } ` ,
142+ `Error when inserting signatures/ notifications: ${ ( err as Error ) . message } ` ,
124143 err
125144 ) ;
126145 }
127146 }
128147 return {
129148 finished : true ,
130- contacts : extracted
149+ contacts : extracted . map ( ( [ , , , contact ] ) => contact )
131150 } ;
132151 }
133152
@@ -136,6 +155,7 @@ export class EmailSignatureProcessor {
136155 miningId : string ,
137156 email : string ,
138157 body : string ,
158+ messageId : string ,
139159 messageDate : string
140160 ) : Promise < void > {
141161 const signature = this . extractSignature ( body ) ;
@@ -158,7 +178,14 @@ export class EmailSignatureProcessor {
158178 return ;
159179 }
160180
161- await this . cache . set ( userId , email , signature , messageDate , miningId ) ;
181+ await this . cache . set (
182+ userId ,
183+ email ,
184+ signature ,
185+ messageId ,
186+ messageDate ,
187+ miningId
188+ ) ;
162189 this . logging . info ( 'Cached new signature' , {
163190 email,
164191 miningId,
@@ -170,7 +197,7 @@ export class EmailSignatureProcessor {
170197 private async handleBatchUpdate (
171198 userId : string ,
172199 miningId : string
173- ) : Promise < Partial < Contact > [ ] > {
200+ ) : Promise < [ string , string , string , Partial < Contact > ] [ ] > {
174201 this . logging . debug ( 'handleBatchUpdate()' , { userId, miningId } ) ;
175202
176203 const all = await this . cache . getAllFromMining ( miningId ) ;
@@ -180,25 +207,31 @@ export class EmailSignatureProcessor {
180207 return [ ] ;
181208 }
182209
183- const contacts : ( Partial < Contact > | undefined ) [ ] = await Promise . all (
184- all . map ( async ( { email, signature } ) => {
185- try {
186- const contact = await this . extractContact ( userId , email , signature ) ;
187- if ( contact ) {
188- await this . upsertContact ( contact ) ;
189- return contact ;
210+ const contacts : ( [ string , string , string , Partial < Contact > ] | undefined ) [ ] =
211+ await Promise . all (
212+ all . map ( async ( { email, signature, messageId } ) => {
213+ try {
214+ const contact = await this . extractContact ( userId , email , signature ) ;
215+ if ( contact ) {
216+ await this . upsertContact ( contact ) ;
217+ return [ email , messageId , signature , contact ] ;
218+ }
219+ return undefined ;
220+ } catch ( err ) {
221+ this . logging . error ( 'Error on extract/insert contact' , err ) ;
222+ return undefined ;
190223 }
191- return undefined ;
192- } catch ( err ) {
193- this . logging . error ( 'Error on extract/insert contact' , err ) ;
194- return undefined ;
195- }
196- } )
197- ) ;
224+ } )
225+ ) ;
198226
199227 await this . cache . clearCachedSignature ( miningId ) ;
200228
201- const successfulContacts = contacts . filter ( Boolean ) as Contact [ ] ;
229+ const successfulContacts = contacts . filter ( ( c ) => c && contacts . length ) as [
230+ string ,
231+ string ,
232+ string ,
233+ Partial < Contact >
234+ ] [ ] ;
202235
203236 this . logging . info ( 'Batch complete - cache cleared' , {
204237 miningId,
0 commit comments