@@ -43,6 +43,7 @@ import { z } from "zod";
4343import { AvailableSQSFunctions , SQSPayload } from "common/types/sqsMessage.js" ;
4444import { SendMessageBatchCommand , SQSClient } from "@aws-sdk/client-sqs" ;
4545import { v4 as uuidv4 } from "uuid" ;
46+ import { randomUUID } from "crypto" ;
4647
4748const iamRoutes : FastifyPluginAsync = async ( fastify , _options ) => {
4849 const getAuthorizedClients = async ( ) => {
@@ -426,80 +427,94 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
426427 }
427428 }
428429 }
429- const sqsAddedPayloads = addedEmails . map ( ( x ) => {
430- return {
431- function : AvailableSQSFunctions . EmailNotifications ,
432- metadata : {
433- initiator : request . username ! ,
434- reqId : request . id ,
435- } ,
436- payload : {
437- to : [ x ] ,
438- subject : "You have been added to an access group" ,
439- content : `
430+ const sqsAddedPayloads = addedEmails
431+ . filter ( ( x ) => ! ! x )
432+ . map ( ( x ) => {
433+ return {
434+ function : AvailableSQSFunctions . EmailNotifications ,
435+ metadata : {
436+ initiator : request . username ! ,
437+ reqId : request . id ,
438+ } ,
439+ payload : {
440+ to : [ x ] ,
441+ subject : "You have been added to an access group" ,
442+ content : `
440443Hello,
441444
442445We're letting you know that you have been added to the "${ groupMetadata . displayName } " access group by ${ request . username } . Changes may take up to 2 hours to reflect in all systems.
443446
444447No action is required from you at this time.
445448 ` ,
446- } ,
447- } ;
448- } ) ;
449- const sqsRemovedPayloads = removedEmails . map ( ( x ) => {
450- return {
451- function : AvailableSQSFunctions . EmailNotifications ,
452- metadata : {
453- initiator : request . username ! ,
454- reqId : request . id ,
455- } ,
456- payload : {
457- to : [ x ] ,
458- subject : "You have been removed from an access group" ,
459- content : `
449+ } ,
450+ } ;
451+ } ) ;
452+ const sqsRemovedPayloads = removedEmails
453+ . filter ( ( x ) => ! ! x )
454+ . map ( ( x ) => {
455+ return {
456+ function : AvailableSQSFunctions . EmailNotifications ,
457+ metadata : {
458+ initiator : request . username ! ,
459+ reqId : request . id ,
460+ } ,
461+ payload : {
462+ to : [ x ] ,
463+ subject : "You have been removed from an access group" ,
464+ content : `
460465Hello,
461466
462467We're letting you know that you have been removed from the "${ groupMetadata . displayName } " access group by ${ request . username } .
463468
464469No action is required from you at this time.
465470 ` ,
466- } ,
467- } ;
468- } ) ;
471+ } ,
472+ } ;
473+ } ) ;
469474 if ( ! fastify . sqsClient ) {
470475 fastify . sqsClient = new SQSClient ( {
471476 region : genericConfig . AwsRegion ,
472477 } ) ;
473478 }
474479 if ( sqsAddedPayloads . length > 0 ) {
475480 request . log . debug ( "Sending added emails" ) ;
476- const addedQueued = await fastify . sqsClient . send (
477- new SendMessageBatchCommand ( {
478- QueueUrl : fastify . environmentConfig . SqsQueueUrl ,
479- Entries : sqsAddedPayloads . map ( ( x ) => ( {
480- Id : uuidv4 ( ) ,
481- MessageBody : JSON . stringify ( x ) ,
482- } ) ) ,
483- } ) ,
484- ) ;
485- request . log . info (
486- `Sent added emails, queue ID ${ addedQueued . $metadata . requestId } ` ,
487- ) ;
481+ let chunkId = 0 ;
482+ for ( let i = 0 ; i < sqsAddedPayloads . length ; i += 10 ) {
483+ chunkId += 1 ;
484+ const chunk = sqsAddedPayloads . slice ( i , i + 10 ) ;
485+ const removedQueued = await fastify . sqsClient . send (
486+ new SendMessageBatchCommand ( {
487+ QueueUrl : fastify . environmentConfig . SqsQueueUrl ,
488+ Entries : chunk . map ( ( x ) => ( {
489+ Id : randomUUID ( ) ,
490+ MessageBody : JSON . stringify ( x ) ,
491+ } ) ) ,
492+ } ) ,
493+ ) ;
494+ request . log . info (
495+ `Sent added emails chunk ${ chunkId } , queue ID ${ removedQueued . $metadata . requestId } ` ,
496+ ) ;
497+ }
488498 }
489499 if ( sqsRemovedPayloads . length > 0 ) {
490500 request . log . debug ( "Sending removed emails" ) ;
491- const removedQueued = await fastify . sqsClient . send (
492- new SendMessageBatchCommand ( {
493- QueueUrl : fastify . environmentConfig . SqsQueueUrl ,
494- Entries : sqsRemovedPayloads . map ( ( x ) => ( {
495- Id : uuidv4 ( ) ,
496- MessageBody : JSON . stringify ( x ) ,
497- } ) ) ,
498- } ) ,
499- ) ;
500- request . log . info (
501- `Sent removed emails, queue ID ${ removedQueued . $metadata . requestId } ` ,
502- ) ;
501+ let chunkId = 0 ;
502+ for ( let i = 0 ; i < sqsRemovedPayloads . length ; i += 10 ) {
503+ chunkId += 1 ;
504+ const chunk = sqsRemovedPayloads . slice ( i , i + 10 ) ;
505+ const removedQueued = await fastify . sqsClient . send (
506+ new SendMessageBatchCommand ( {
507+ QueueUrl : fastify . environmentConfig . SqsQueueUrl ,
508+ Entries : chunk . map ( ( x ) => ( {
509+ Id : randomUUID ( ) ,
510+ MessageBody : JSON . stringify ( x ) ,
511+ } ) ) ,
512+ } ) ,
513+ ) ;
514+ request . log . info (
515+ `Sent removed emails chunk ${ chunkId } , queue ID ${ removedQueued . $metadata . requestId } ` ,
516+ ) ;
517+ }
503518 }
504519 await Promise . allSettled ( logPromises ) ;
505520 reply . status ( 202 ) . send ( response ) ;
0 commit comments