@@ -7,22 +7,48 @@ import {
77 CreateTopicCommandInput , SubscribeCommandInput , PublishCommandInput ,
88} from '@aws-sdk/client-sns' ;
99import { ConfigsInterface } from '@core/configs/envs.config' ;
10- import CryptographyService from '@core/security/Cryptography.service' ;
1110import Exceptions from '@core/errors/Exceptions' ;
1211import LoggerService from '@core/logging/Logger.service' ;
1312import DataParserHelper from '@common/utils/helpers/DataParser.helper' ;
1413
1514
16- export type protocolType = 'email' | 'sms' | 'http' | 'https' | 'sqs' | 'lambda' | 'application'
15+ interface EmailPublishTargets {
16+ protocol : 'email' ;
17+ subject : string ;
18+ }
19+ interface SmsPublishTargets {
20+ protocol : 'sms' ;
21+ phoneNumber : string ;
22+ }
23+ interface SqsPublishTargets {
24+ protocol : 'sqs' ;
25+ targetArn ?: string ;
26+ }
27+ interface LambdaPublishTargets {
28+ protocol : 'lambda' ;
29+ targetArn ?: string ;
30+ }
31+
32+ export type IPublishTargetsOptions =
33+ | EmailPublishTargets
34+ | SmsPublishTargets
35+ | SqsPublishTargets
36+ | LambdaPublishTargets ;
37+ export type protocolType = IPublishTargetsOptions [ 'protocol' ] ;
38+
39+ type IPublishParams = IPublishTargetsOptions & {
40+ topicArn : string ;
41+ message : unknown ;
42+ messageGroupId : string ;
43+ messageDeduplicationId : string ;
44+ } ;
1745
1846@Injectable ( )
1947export default class SnsClient {
20- private readonly messageGroupId : string ;
2148 private readonly snsClient : SNSClient ;
2249
2350 constructor (
2451 private readonly configService : ConfigService ,
25- private readonly cryptographyService : CryptographyService ,
2652 private readonly exceptions : Exceptions ,
2753 private readonly logger : LoggerService ,
2854 private readonly dataParserHelper : DataParserHelper ,
@@ -32,8 +58,6 @@ export default class SnsClient {
3258 } } = this . configService . get < ConfigsInterface [ 'integration' ] [ 'aws' ] > ( 'integration.aws' ) ! ;
3359 const showExternalLogs = this . configService . get < ConfigsInterface [ 'application' ] [ 'showExternalLogs' ] > ( 'application.showExternalLogs' ) ! ;
3460
35- this . messageGroupId = 'DefaultGroup' ;
36-
3761 this . snsClient = new SNSClient ( {
3862 endpoint, region, apiVersion, maxAttempts,
3963 credentials : { accessKeyId, secretAccessKey, sessionToken } ,
@@ -47,7 +71,7 @@ export default class SnsClient {
4771 }
4872
4973 private createParams ( topicName : string ) : CreateTopicCommandInput {
50- const isFifoTopic : boolean = topicName ?. includes ( '.fifo' ) ;
74+ const isFifoTopic = topicName ?. endsWith ( '.fifo' ) ;
5175
5276 const params : CreateTopicCommandInput = {
5377 Name : topicName ,
@@ -68,28 +92,36 @@ export default class SnsClient {
6892 } ;
6993 }
7094
71- private publishParams ( topicArn : string , topicName : string ,
72- protocol : protocolType , message : string , { subject , phoneNumber } : Record < string , string | undefined > ) : PublishCommandInput {
73- const isFifoTopic : boolean = topicName ?. includes ( '.fifo' ) ;
95+ private publishParams ( params : IPublishParams ) : PublishCommandInput {
96+ const { message, protocol , topicArn , messageGroupId , messageDeduplicationId } = params ;
97+ const isFifoTopic = topicArn ?. endsWith ( '.fifo' ) ;
7498 const messageBody = this . formatMessageBeforeSend ( message ) ;
7599
76100 const publishData : PublishCommandInput = {
77- TopicArn : topicArn ,
78101 Message : messageBody ,
79- MessageDeduplicationId : isFifoTopic ? this . cryptographyService . generateUuid ( ) : undefined ,
80- MessageGroupId : isFifoTopic ? this . messageGroupId : undefined , // Required for FIFO topics
102+ // NOTE - required for FIFO topics
103+ MessageDeduplicationId : isFifoTopic ? messageDeduplicationId : undefined ,
104+ MessageGroupId : isFifoTopic ? messageGroupId : undefined ,
81105 } ;
82106
83107 switch ( protocol ) {
108+ case 'sms' :
109+ publishData . PhoneNumber = params . phoneNumber ;
110+ break ;
84111 case 'email' :
85- publishData . Subject = subject ;
112+ publishData . TopicArn = topicArn ;
113+ publishData . Subject = params . subject ;
86114 break ;
87- case 'sms' :
88- publishData . PhoneNumber = phoneNumber ;
115+ case 'lambda' :
116+ case 'sqs' :
117+ if ( params . targetArn ) {
118+ publishData . TargetArn = params . targetArn ;
119+ } else {
120+ publishData . TopicArn = topicArn ;
121+ }
89122 break ;
90- default : // application | sqs | lambda | http(s)
123+ default :
91124 publishData . TopicArn = topicArn ;
92- publishData . TargetArn = topicArn ;
93125 break ;
94126 }
95127
@@ -168,12 +200,9 @@ export default class SnsClient {
168200 }
169201 }
170202
171- public async publishMessage ( topicArn : string , topicName : string ,
172- protocol : protocolType , message : string , destination : Record < string , string | undefined > ) : Promise < string > {
203+ public async publishMessage ( params : IPublishParams ) : Promise < string > {
173204 try {
174- const result = await this . snsClient . send ( new PublishCommand (
175- this . publishParams ( topicArn , topicName , protocol , message , destination )
176- ) ) ;
205+ const result = await this . snsClient . send ( new PublishCommand ( this . publishParams ( params ) ) ) ;
177206
178207 if ( ! result ?. MessageId )
179208 throw this . exceptions . internal ( { message : 'Message not published' } ) ;
0 commit comments