@@ -19,9 +19,12 @@ import { Worker } from "bullmq";
1919import redis from "../redis" ;
2020import mongoose from "mongoose" ;
2121import sequenceQueue from "./sequence-queue" ;
22+ import EmailDelivery from "./model/email-delivery" ;
2223import { AdminSequence , InternalUser } from "@courselit/common-logic" ;
23- import { renderEmailToHtml } from "@courselit/email-editor" ;
24+ import { Email as EmailType , renderEmailToHtml } from "@courselit/email-editor" ;
2425import { getUnsubLink } from "../utils/get-unsub-link" ;
26+ import { getSiteUrl } from "../utils/get-site-url" ;
27+ import { jwtUtils } from "@courselit/utils" ;
2528const liquidEngine = new Liquid ( ) ;
2629
2730new Worker (
@@ -38,6 +41,11 @@ new Worker(
3841) ;
3942
4043export async function processOngoingSequences ( ) : Promise < void > {
44+ if ( ! process . env . PIXEL_SIGNING_SECRET ) {
45+ throw new Error (
46+ "PIXEL_SIGNING_SECRET environment variable is not defined" ,
47+ ) ;
48+ }
4149 // eslint-disable-next-line no-constant-condition
4250 while ( true ) {
4351 // eslint-disable-next-line no-console
@@ -194,9 +202,37 @@ async function attemptMailSending({
194202 return ;
195203 }
196204 // const content = email.content;
205+ const pixelPayload = {
206+ userId : user . userId ,
207+ sequenceId : ongoingSequence . sequenceId ,
208+ emailId : email . emailId ,
209+ } ;
210+ const pixelToken = jwtUtils . generateToken (
211+ pixelPayload ,
212+ process . env . PIXEL_SIGNING_SECRET ,
213+ "365d" ,
214+ ) ;
215+ const pixelUrl = `${ getSiteUrl ( domain ) } /api/pixel?d=${ pixelToken } ` ;
216+ const emailContentWithPixel : EmailType = {
217+ content : [
218+ ...email . content . content ,
219+ {
220+ blockType : "image" ,
221+ settings : {
222+ src : pixelUrl ,
223+ width : "1px" ,
224+ height : "1px" ,
225+ alt : "CourseLit Pixel" ,
226+ } ,
227+ } ,
228+ ] ,
229+ style : email . content . style ,
230+ meta : email . content . meta ,
231+ } ;
232+
197233 const content = await liquidEngine . parseAndRender (
198234 await renderEmailToHtml ( {
199- email : email . content ,
235+ email : emailContentWithPixel ,
200236 } ) ,
201237 templatePayload ,
202238 ) ;
@@ -207,6 +243,13 @@ async function attemptMailSending({
207243 subject,
208244 html : content ,
209245 } ) ;
246+ // @ts -ignore - Mongoose type compatibility issue
247+ await EmailDelivery . create ( {
248+ domain : ( domain as any ) . id ,
249+ sequenceId : sequence . sequenceId ,
250+ userId : user . userId ,
251+ emailId : email . emailId ,
252+ } ) ;
210253 } catch ( err : any ) {
211254 ongoingSequence . retryCount ++ ;
212255 if ( ongoingSequence . retryCount >= sequenceBounceLimit ) {
0 commit comments