import { createRequire } from 'module'; const require = createRequire(import.meta.url);
import{randomUUID as w}from"node:crypto";import{DynamoDBClient as k,PutItemCommand as I}from"@aws-sdk/client-dynamodb";var A=new k({});async function E(m,y){let l=y.awsRequestId,d=w().slice(0,8),a=(s,o)=>{console.log(JSON.stringify({requestId:l,batchId:d,msg:s,...o}))},T=(s,o,e)=>{console.error(JSON.stringify({requestId:l,batchId:d,msg:s,error:String(o),...e}))};a("Processing SES batch",{recordCount:m.Records.length});let u=process.env.TABLE_NAME;if(!u)throw new Error("TABLE_NAME environment variable not set");let g=Number.parseInt(process.env.RETENTION_DAYS||"90",10);for(let s of m.Records)try{let e=JSON.parse(s.body).detail,t=e.eventType||e.notificationType,r=e.mail,p=r.messageId,f=new Date(r.timestamp).getTime(),S=r.source,b=r.destination||[],v=r.commonHeaders?.subject||"";a("Processing email event",{messageId:p,eventType:t,recipientCount:b.length});let n={},i=f;if(t==="Send"&&e.send)n={tags:r.tags||{}};else if(t==="Delivery"&&e.delivery)i=new Date(e.delivery.timestamp).getTime(),n={timestamp:e.delivery.timestamp,processingTimeMillis:e.delivery.processingTimeMillis,recipients:e.delivery.recipients,smtpResponse:e.delivery.smtpResponse,remoteMtaIp:e.delivery.remoteMtaIp};else if(t==="Open"&&e.open)i=new Date(e.open.timestamp).getTime(),n={timestamp:e.open.timestamp,userAgent:e.open.userAgent,ipAddress:e.open.ipAddress};else if(t==="Click"&&e.click)i=new Date(e.click.timestamp).getTime(),n={timestamp:e.click.timestamp,link:e.click.link,linkTags:e.click.linkTags||{},userAgent:e.click.userAgent,ipAddress:e.click.ipAddress};else if(t==="Bounce"&&e.bounce){let c=e.bounce.bounceSubType;c==="Suppressed"||c==="OnAccountSuppressionList"?(t="Suppressed",i=new Date(e.bounce.timestamp).getTime(),n={reason:c,suppressedRecipients:e.bounce.bouncedRecipients,timestamp:e.bounce.timestamp,feedbackId:e.bounce.feedbackId}):(i=new Date(e.bounce.timestamp).getTime(),n={bounceType:e.bounce.bounceType,bounceSubType:e.bounce.bounceSubType,bouncedRecipients:e.bounce.bouncedRecipients,timestamp:e.bounce.timestamp,feedbackId:e.bounce.feedbackId})}else t==="Complaint"&&e.complaint?(i=new Date(e.complaint.timestamp).getTime(),n={complainedRecipients:e.complaint.complainedRecipients,timestamp:e.complaint.timestamp,feedbackId:e.complaint.feedbackId,complaintFeedbackType:e.complaint.complaintFeedbackType,userAgent:e.complaint.userAgent}):t==="Reject"&&e.reject?n={reason:e.reject.reason}:t==="Rendering Failure"&&e.failure?n={errorMessage:e.failure.errorMessage,templateName:e.failure.templateName}:t==="DeliveryDelay"&&e.deliveryDelay?(i=new Date(e.deliveryDelay.timestamp).getTime(),n={timestamp:e.deliveryDelay.timestamp,delayType:e.deliveryDelay.delayType,expirationTime:e.deliveryDelay.expirationTime,delayedRecipients:e.deliveryDelay.delayedRecipients}):t==="Subscription"&&e.subscription&&(i=new Date(e.subscription.timestamp).getTime(),n={contactList:e.subscription.contactList,timestamp:e.subscription.timestamp,source:e.subscription.source,newTopicPreferences:e.subscription.newTopicPreferences,oldTopicPreferences:e.subscription.oldTopicPreferences});let D=g>0?Date.now()+g*24*60*60*1e3:Date.now()+365*24*60*60*1e3;await A.send(new I({TableName:u,Item:{messageId:{S:p},sentAt:{N:i.toString()},accountId:{S:process.env.AWS_ACCOUNT_ID||"unknown"},from:{S},to:{L:b.map(c=>({S:c}))},subject:{S:v},eventType:{S:t},eventData:{S:JSON.stringify(e)},additionalData:{S:JSON.stringify(n)},createdAt:{N:Date.now().toString()},expiresAt:{N:D.toString()}}})),a("Stored event",{eventType:t,messageId:p})}catch(o){T("Error processing record",o,{sqsMessageId:s.messageId})}return{statusCode:200,body:JSON.stringify({message:"Events processed successfully"})}}export{E as handler};
