import { Injectable } from '@nestjs/common'; import { EmailInterface } from '@gitroom/nestjs-libraries/emails/email.interface'; import { ResendProvider } from '@gitroom/nestjs-libraries/emails/resend.provider'; import { EmptyProvider } from '@gitroom/nestjs-libraries/emails/empty.provider'; import { NodeMailerProvider } from '@gitroom/nestjs-libraries/emails/node.mailer.provider'; import { TemporalService } from 'nestjs-temporal-core'; @Injectable() export class EmailService { emailService: EmailInterface; constructor(private _temporalService: TemporalService) { this.emailService = this.selectProvider(process.env.EMAIL_PROVIDER!); console.log('Email service provider:', this.emailService.name); for (const key of this.emailService.validateEnvKeys) { if (!process.env[key]) { console.error(`Missing environment variable: ${key}`); } } } hasProvider() { return !(this.emailService instanceof EmptyProvider); } selectProvider(provider: string) { switch (provider) { case 'resend': return new ResendProvider(); case 'nodemailer': return new NodeMailerProvider(); default: return new EmptyProvider(); } } async sendEmail( to: string, subject: string, html: string, sendTo: 'top' | 'bottom', replyTo?: string ) { return this._temporalService.client .getRawClient() ?.workflow.signalWithStart('sendEmailWorkflow', { taskQueue: 'main', workflowId: 'send_email', signal: 'sendEmail', args: [{ queue: [] }], signalArgs: [{ to, subject, html, replyTo, sendTo }], workflowIdConflictPolicy: 'USE_EXISTING', }); } async sendEmailSync( to: string, subject: string, html: string, replyTo?: string ) { if (to.indexOf('@') === -1) { return; } if (!process.env.EMAIL_FROM_ADDRESS || !process.env.EMAIL_FROM_NAME) { console.log( 'Email sender information not found in environment variables' ); return; } const modifiedHtml = `

${subject}

${html}

${process.env.EMAIL_FROM_NAME}

You can change your notification preferences in your account settings.
`; try { const sends = await this.emailService.sendEmail( to, subject, modifiedHtml, process.env.EMAIL_FROM_NAME, process.env.EMAIL_FROM_ADDRESS, replyTo ); console.log(sends); } catch (err) { console.log(err); } } }