@@ -3,13 +3,7 @@ import { authProcedure } from "../trpc"
33import { prisma } from "../utils/prisma"
44import { TRPCError } from "@trpc/server"
55import pMap from "p-map"
6- import { LinkTracker } from "../lib/LinkTracker"
7- import { v4 as uuidV4 } from "uuid"
86import { Mailer } from "../lib/Mailer"
9- import {
10- replacePlaceholders ,
11- PlaceholderDataKey ,
12- } from "../utils/placeholder-parser"
137
148const createCampaignSchema = z . object ( {
159 title : z . string ( ) . min ( 1 , "Campaign title is required" ) ,
@@ -378,105 +372,16 @@ export const startCampaign = authProcedure
378372 } )
379373 }
380374
381- const updatedCampaign = await prisma . $transaction ( async ( tx ) => {
382- const status =
383- campaign . scheduledAt && campaign . scheduledAt > new Date ( )
384- ? "SCHEDULED"
385- : "SENDING"
386-
387- const linkTracker = new LinkTracker ( tx )
388-
389- const messagesData = await Promise . all (
390- Array . from ( subscribers . values ( ) ) . map ( async ( subscriber ) => {
391- if ( ! campaign . content ) {
392- throw new TRPCError ( {
393- code : "BAD_REQUEST" ,
394- message : "Campaign must have content" ,
395- } )
396- }
397-
398- if ( ! generalSettings . baseURL ) {
399- throw new TRPCError ( {
400- code : "BAD_REQUEST" ,
401- message :
402- "Base URL must be configured in settings before running a campaign" ,
403- } )
404- }
405-
406- const messageId = uuidV4 ( )
407- let content = campaign . Template
408- ? campaign . Template . content . replace (
409- / { { content} } / g,
410- campaign . content
411- )
412- : campaign . content
413-
414- const placeholderData : Partial < Record < PlaceholderDataKey , string > > = {
415- "subscriber.email" : subscriber . email ,
416- "campaign.name" : campaign . title ,
417- "campaign.subject" : campaign . subject ?? "" ,
418- "organization.name" : organization . name ,
419- unsubscribe_link : `${ generalSettings . baseURL } /unsubscribe?sid=${ subscriber . id } &cid=${ campaign . id } &mid=${ messageId } ` ,
420- current_date : new Date ( ) . toLocaleDateString ( "en-CA" ) ,
421- }
422-
423- if ( campaign . openTracking ) {
424- content += `<img src="${ generalSettings . baseURL } /img/${ messageId } /img.png" alt="" width="1" height="1" style="display:none" />`
425- }
426-
427- if ( subscriber . name ) {
428- placeholderData [ "subscriber.name" ] = subscriber . name
429- }
430-
431- if ( subscriber . Metadata ) {
432- for ( const meta of subscriber . Metadata ) {
433- placeholderData [ `subscriber.metadata.${ meta . key } ` ] = meta . value
434- }
435- }
436-
437- content = replacePlaceholders ( content , placeholderData )
438-
439- const { content : finalContent , trackedIds } =
440- await linkTracker . replaceMessageContentWithTrackedLinks (
441- content ,
442- campaign . id ,
443- generalSettings . baseURL
444- )
445-
446- return {
447- message : {
448- id : messageId ,
449- subscriberId : subscriber . id ,
450- content : finalContent ,
451- } ,
452- subscriberTrackedLinks : trackedIds . map ( ( trackedLinkId ) => ( {
453- messageId,
454- subscriberId : subscriber . id ,
455- trackedLinkId,
456- } ) ) ,
457- }
458- } )
459- )
460-
461- const createdCampaign = await tx . campaign . update ( {
462- where : { id : campaign . id } ,
463- data : {
464- status,
465- Messages : {
466- create : messagesData . map ( ( { message } ) => message ) ,
467- } ,
468- } ,
469- include : {
470- Messages : true ,
471- _count : {
472- select : {
473- Messages : true ,
474- } ,
475- } ,
476- } ,
477- } )
375+ const status =
376+ campaign . scheduledAt && campaign . scheduledAt > new Date ( )
377+ ? "SCHEDULED"
378+ : "SENDING"
478379
479- return createdCampaign
380+ const updatedCampaign = await prisma . campaign . update ( {
381+ where : { id : campaign . id } ,
382+ data : {
383+ status,
384+ } ,
480385 } )
481386
482387 return { campaign : updatedCampaign }
0 commit comments