@@ -1265,6 +1265,64 @@ export const accessReviewRouter = createTRPCRouter({
12651265 } ) ) ,
12661266 } ;
12671267 } ) ,
1268+
1269+ sendCampaignReport : protectedProcedure
1270+ . input (
1271+ z . object ( {
1272+ campaignId : z . string ( ) ,
1273+ recipientEmails : z . array ( z . string ( ) . email ( ) ) ,
1274+ includeDetails : z . boolean ( ) . default ( true ) ,
1275+ } )
1276+ )
1277+ . mutation ( async ( { ctx, input } ) => {
1278+ const campaign = await db . accessReviewCampaign . findUnique ( {
1279+ where : { id : input . campaignId } ,
1280+ include : {
1281+ createdBy : {
1282+ select : { name : true , email : true } ,
1283+ } ,
1284+ } ,
1285+ } ) ;
1286+
1287+ if ( ! campaign ) {
1288+ throw new TRPCError ( { code : 'NOT_FOUND' , message : 'Campaign not found' } ) ;
1289+ }
1290+
1291+ // Get campaign stats
1292+ const [ totalItems , retainCount , removeCount ] = await Promise . all ( [
1293+ db . accessReviewItem . count ( { where : { campaignId : input . campaignId } } ) ,
1294+ db . accessReviewDecision . count ( {
1295+ where : { item : { campaignId : input . campaignId } , decision : 'retain' } ,
1296+ } ) ,
1297+ db . accessReviewDecision . count ( {
1298+ where : { item : { campaignId : input . campaignId } , decision : 'remove' } ,
1299+ } ) ,
1300+ ] ) ;
1301+
1302+ // TODO: Integrate with email service (SendGrid, SES, etc.)
1303+ // For now, log the email that would be sent
1304+ console . log ( `[SendCampaignReport] Would send report for campaign "${ campaign . name } " to:` , input . recipientEmails ) ;
1305+ console . log ( `[SendCampaignReport] Stats: ${ totalItems } items, ${ retainCount } retained, ${ removeCount } removed` ) ;
1306+
1307+ // Create notifications for the recipients
1308+ for ( const email of input . recipientEmails ) {
1309+ await db . accessReviewNotification . create ( {
1310+ data : {
1311+ userId : ctx . user . id ,
1312+ type : 'execution_complete' ,
1313+ title : `Campaign Report: ${ campaign . name } ` ,
1314+ message : `Report sent to ${ email } . Total items: ${ totalItems } , Retained: ${ retainCount } , Removed: ${ removeCount } ` ,
1315+ campaignId : input . campaignId ,
1316+ } ,
1317+ } ) ;
1318+ }
1319+
1320+ return {
1321+ success : true ,
1322+ message : `Report queued for ${ input . recipientEmails . length } recipient(s)` ,
1323+ recipientCount : input . recipientEmails . length ,
1324+ } ;
1325+ } ) ,
12681326} ) ;
12691327
12701328// Helper function to calculate next run date
0 commit comments