-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check is made when : - Sending a feedback request - Giving a spontaneous feedback
- Loading branch information
Showing
12 changed files
with
141 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { MailgunMessageData } from 'mailgun.js'; | ||
|
||
export type SendParams = Required<Pick<MailgunMessageData, 'from' | 'subject' | 'html'> & { to: string }>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,55 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import * as FormData from 'form-data'; | ||
import Mailgun, { MailgunMessageData } from 'mailgun.js'; | ||
import Mailgun from 'mailgun.js'; | ||
import { AppConfig } from '../config'; | ||
import { SendParams } from './email.params'; | ||
|
||
@Injectable() | ||
export class EmailService { | ||
private logger = new Logger('EmailService'); | ||
|
||
private clientOptions = this.configService.get('mailgunClientOptions', { infer: true })!; | ||
|
||
private client = new Mailgun(FormData).client(this.clientOptions); | ||
|
||
private domain = this.configService.get('mailgunDomain', { infer: true })!; | ||
|
||
private appEnv = this.configService.get('appEnv', { infer: true })!; | ||
|
||
constructor(private configService: ConfigService<AppConfig>) {} | ||
|
||
async send({ from, to, subject, html }: Required<Pick<MailgunMessageData, 'from' | 'to' | 'subject' | 'html'>>) { | ||
await this.client.messages.create(this.domain, { from, to, subject, html }); | ||
async validate(email: string, allowRoleAdress = false) { | ||
// Email validation is only available in the Mailgun production environment. | ||
if (this.appEnv === 'developement') { | ||
return true; | ||
} | ||
try { | ||
// API reference: https://documentation.mailgun.com/en/latest/api-email-validation.html#single-validation | ||
const validation = await this.client.validate.get(email); | ||
this.logger.log(validation); | ||
|
||
return ( | ||
validation.result === 'deliverable' && | ||
validation.is_disposable_address === false && | ||
(allowRoleAdress || validation.is_role_address === false) | ||
); | ||
} catch (err) { | ||
this.logger.error(err); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
async send(params: SendParams) { | ||
try { | ||
await this.client.messages.create(this.domain, params); | ||
|
||
return true; | ||
} catch (err) { | ||
this.logger.error(err); | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthModule } from '../core/auth'; | ||
import { EmailModule } from '../core/email'; | ||
import { EmployeeModule } from '../employee'; | ||
import { FeedbackDbModule } from './feedback-db'; | ||
import { FeedbackEmailModule } from './feedback-email'; | ||
import { FeedbackController } from './feedback.controller'; | ||
|
||
@Module({ | ||
imports: [AuthModule, FeedbackDbModule, FeedbackEmailModule, EmployeeModule], | ||
imports: [AuthModule, EmailModule, FeedbackDbModule, FeedbackEmailModule, EmployeeModule], | ||
controllers: [FeedbackController], | ||
}) | ||
export class FeedbackModule {} |