Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AUTHSCH_CLIENT_ID=<authSchClientId>
AUTHSCH_CLIENT_SECRET=<authSchClientSecret>
MAIL_SERVER_URL=<mailServer>
MAIL_API_KEY=<apikey>
MAIL_FROM_EMAIL=<email>
MAIL_QUEUE=<queueName>

# --- development example values
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/konzisite?schema=public
Expand Down
44 changes: 26 additions & 18 deletions src/mailing/mailing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ type Templates = Record<string, string> & { default: string }

interface SendMail {
to: string
from: string
from: {
name: string
email: string
}
subject: string
html: string
queue?: string
}

export interface Setup {
Expand Down Expand Up @@ -99,29 +103,33 @@ export class MailingService {
* Sends a mail through the mailing delivery service provided in the setup process.
* @param {SendMail[]} data - Array of objects containing to, from, subject and html string fields.
*/
sendMail(data: SendMail[]) {
async sendMail(data: SendMail[]) {
MailingService.checkSetup()
if (process.env.NODE_ENV !== 'production') {
this.logger.debug(
`The app would be sending ${data.length} email(s) right now, but email sending is only enabled in production.`,
)
return Promise.resolve(false)
}
return axios
.post(MailingService.mailServerUrl, data, {
headers: { 'X-Api-Key': MailingService.apiKey },
})
.then(() => {
this.logger.log(
`${data.length} email data sent to Kir-Dev email service`,
)
return true
})
.catch((e) => {
this.logger.log('Error during email sending')
this.logger.error(e)
return false
})
try {
await Promise.all(
data.map((email) =>
axios.post(
MailingService.mailServerUrl,
{ ...email, queue: process.env.MAIL_QUEUE },
{
headers: { Authorization: `Api-Key ${MailingService.apiKey}` },
},
),
),
)
this.logger.log(`${data.length} email data sent to Kir-Dev email service`)
return true
} catch (e) {
this.logger.log('Error during email sending')
this.logger.error(e)
return false
}
}

private static checkSetup() {
Expand Down Expand Up @@ -172,7 +180,7 @@ export class MailingService {
consultationUrl: `${process.env.FRONTEND_HOST}/consultations/${consultation.id}`,
})
return {
from: 'Konzisite',
from: { name: 'Konzisite', email: process.env.MAIL_FROM_EMAIL },
to: u.email,
subject: 'Megvalósul egy konzi kérésed!',
html,
Expand Down