@@ -312,6 +312,114 @@ export class EmailService implements OnModuleInit {
312312 } ) ;
313313 }
314314 }
315+ async getSenders (
316+ req : EmailProto . GetSendersRequest ,
317+ ) : Promise < EmailProto . GetSendersResponse > {
318+ const config = await lastValueFrom (
319+ this . emailService . getEmailServiceConfig ( {
320+ id : Number ( req . configId ) ,
321+ environment : req . configEnvironment ,
322+ } ) ,
323+ ) ;
324+
325+ const sendgridApiKey = config . sendgridKey ;
326+
327+ if ( ! sendgridApiKey ) {
328+ throw new RpcException ( {
329+ code : status . FAILED_PRECONDITION ,
330+ message : 'Cannot get senders (SendGrid API key is missing)' ,
331+ } ) ;
332+ }
333+
334+ if ( process . env [ 'NODE_ENV' ] == 'test' ) {
335+ return { senders : [ ] } ;
336+ }
337+
338+ try {
339+ const response = await axios . get (
340+ 'https://api.sendgrid.com/v3/verified_senders' ,
341+ {
342+ headers : {
343+ Authorization : `Bearer ${ sendgridApiKey } ` ,
344+ 'Content-Type' : 'application/json' ,
345+ } ,
346+ } ,
347+ ) ;
348+
349+ const senders = ( response . data . results ?? [ ] ) . map ( ( sender : any ) => ( {
350+ id : sender . id ,
351+ nickname : sender . nickname ?? '' ,
352+ fromEmail : sender . from_email ?? '' ,
353+ fromName : sender . from_name ?? '' ,
354+ replyTo : sender . reply_to ?? '' ,
355+ address : sender . address ?? '' ,
356+ city : sender . city ?? '' ,
357+ state : sender . state ?? '' ,
358+ country : sender . country ?? '' ,
359+ zip : sender . zip ?? '' ,
360+ verified : sender . verified ?? false ,
361+ locked : sender . locked ?? false ,
362+ } ) ) ;
363+
364+ return { senders } ;
365+ } catch ( error ) {
366+ throw new RpcException ( {
367+ code : status . INTERNAL ,
368+ message : `Failed to fetch senders from SendGrid: ${ JSON . stringify ( error ) } ` ,
369+ } ) ;
370+ }
371+ }
372+
373+ async getDomains (
374+ req : EmailProto . GetDomainsRequest ,
375+ ) : Promise < EmailProto . GetDomainsResponse > {
376+ const config = await lastValueFrom (
377+ this . emailService . getEmailServiceConfig ( {
378+ id : Number ( req . configId ) ,
379+ environment : req . configEnvironment ,
380+ } ) ,
381+ ) ;
382+
383+ const sendgridApiKey = config . sendgridKey ;
384+
385+ if ( ! sendgridApiKey ) {
386+ throw new RpcException ( {
387+ code : status . FAILED_PRECONDITION ,
388+ message : 'Cannot get domains (SendGrid API key is missing)' ,
389+ } ) ;
390+ }
391+
392+ if ( process . env [ 'NODE_ENV' ] == 'test' ) {
393+ return { domains : [ ] } ;
394+ }
395+
396+ try {
397+ const response = await axios . get (
398+ 'https://api.sendgrid.com/v3/whitelabel/domains' ,
399+ {
400+ headers : {
401+ Authorization : `Bearer ${ sendgridApiKey } ` ,
402+ 'Content-Type' : 'application/json' ,
403+ } ,
404+ } ,
405+ ) ;
406+
407+ const domains = ( response . data ?? [ ] ) . map ( ( domain : any ) => ( {
408+ id : domain . id ,
409+ domain : domain . domain ?? '' ,
410+ subdomain : domain . subdomain ?? undefined ,
411+ valid : domain . valid ?? false ,
412+ } ) ) ;
413+
414+ return { domains } ;
415+ } catch ( error ) {
416+ throw new RpcException ( {
417+ code : status . INTERNAL ,
418+ message : `Failed to fetch domains from SendGrid: ${ JSON . stringify ( error ) } ` ,
419+ } ) ;
420+ }
421+ }
422+
315423 async getStatistics (
316424 req : EmailProto . GetStatisticsRequest ,
317425 ) : Promise < EmailProto . StatisticResponses > {
0 commit comments