2025-12-22 11:57:16 +05:30
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
getVerificationEmailTemplate,
|
|
|
|
|
getPasswordResetTemplate,
|
|
|
|
|
getWelcomeEmailTemplate,
|
|
|
|
|
} from './templates';
|
|
|
|
|
|
|
|
|
|
export interface EmailOptions {
|
|
|
|
|
to: string;
|
|
|
|
|
subject: string;
|
|
|
|
|
html: string;
|
|
|
|
|
text?: string;
|
2026-04-08 09:36:08 +05:30
|
|
|
cc?: string[];
|
|
|
|
|
bcc?: string[];
|
|
|
|
|
replyTo?: string;
|
2025-12-22 11:57:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class EmailService {
|
|
|
|
|
private readonly logger = new Logger(EmailService.name);
|
2026-04-08 09:36:08 +05:30
|
|
|
private readonly endpoint: string;
|
2025-12-22 11:57:16 +05:30
|
|
|
|
|
|
|
|
constructor(private readonly configService: ConfigService) {
|
2026-04-08 09:36:08 +05:30
|
|
|
this.endpoint =
|
|
|
|
|
this.configService.get<string>('EMAIL_API_URL') ||
|
|
|
|
|
'https://email.routes.hiffi.com/superlabs/v1/send';
|
|
|
|
|
this.logger.log(`Email service initialized → ${this.endpoint}`);
|
2025-12-22 11:57:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendEmail(options: EmailOptions): Promise<boolean> {
|
2026-04-08 09:36:08 +05:30
|
|
|
const payload: Record<string, any> = {
|
|
|
|
|
to: [options.to],
|
|
|
|
|
subject: options.subject,
|
|
|
|
|
html: options.html,
|
|
|
|
|
};
|
2025-12-22 11:57:16 +05:30
|
|
|
|
2026-04-08 09:36:08 +05:30
|
|
|
if (options.text) payload.text = options.text;
|
|
|
|
|
if (options.cc && options.cc.length > 0) payload.cc = options.cc;
|
|
|
|
|
if (options.bcc && options.bcc.length > 0) payload.bcc = options.bcc;
|
|
|
|
|
if (options.replyTo) payload.reply_to = options.replyTo;
|
2025-12-22 11:57:16 +05:30
|
|
|
|
|
|
|
|
try {
|
2026-04-08 09:36:08 +05:30
|
|
|
const res = await fetch(this.endpoint, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(payload),
|
2025-12-22 11:57:16 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-08 09:36:08 +05:30
|
|
|
const data = await res.json().catch(() => null);
|
|
|
|
|
|
|
|
|
|
if (!res.ok || !data?.ok) {
|
|
|
|
|
const errMsg = data?.error || `HTTP ${res.status}`;
|
|
|
|
|
this.logger.error(`Failed to send email to ${options.to}: ${errMsg}`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.logger.log(`Email sent successfully to ${options.to}`);
|
2025-12-22 11:57:16 +05:30
|
|
|
return true;
|
2026-04-08 09:36:08 +05:30
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error(`Email API request failed for ${options.to}: ${error.message}`);
|
2025-12-22 11:57:16 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
// EMAIL VERIFICATION
|
|
|
|
|
// ==========================================
|
|
|
|
|
async sendVerificationEmail(email: string, token: string, name?: string): Promise<boolean> {
|
|
|
|
|
const frontendUrl = this.configService.get<string>('FRONTEND_URL') || 'http://localhost:3000';
|
|
|
|
|
const verificationUrl = `${frontendUrl}/verify-email?token=${token}`;
|
|
|
|
|
|
2026-04-09 21:34:04 +05:30
|
|
|
const html = getVerificationEmailTemplate({ verificationUrl, name, frontendUrl });
|
|
|
|
|
const text = `Welcome to RE-Quest! Please verify your email by clicking this link: ${verificationUrl}`;
|
2025-12-22 11:57:16 +05:30
|
|
|
|
|
|
|
|
return this.sendEmail({
|
|
|
|
|
to: email,
|
2026-04-09 21:34:04 +05:30
|
|
|
subject: 'Verify Your Email - RE-Quest',
|
2025-12-22 11:57:16 +05:30
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
// PASSWORD RESET
|
|
|
|
|
// ==========================================
|
|
|
|
|
async sendPasswordResetEmail(email: string, resetUrl: string): Promise<boolean> {
|
2026-04-09 21:34:04 +05:30
|
|
|
const frontendUrl = this.configService.get<string>('FRONTEND_URL') || 'http://localhost:3000';
|
|
|
|
|
const html = getPasswordResetTemplate({ resetUrl, frontendUrl });
|
2025-12-22 11:57:16 +05:30
|
|
|
const text = `Reset your password by clicking this link: ${resetUrl}. This link will expire in 1 hour.`;
|
|
|
|
|
|
|
|
|
|
return this.sendEmail({
|
|
|
|
|
to: email,
|
2026-04-09 21:34:04 +05:30
|
|
|
subject: 'Reset Your Password - RE-Quest',
|
2025-12-22 11:57:16 +05:30
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
// WELCOME EMAIL
|
|
|
|
|
// ==========================================
|
|
|
|
|
async sendWelcomeEmail(email: string, name?: string): Promise<boolean> {
|
|
|
|
|
const frontendUrl = this.configService.get<string>('FRONTEND_URL') || 'http://localhost:3000';
|
|
|
|
|
const html = getWelcomeEmailTemplate({ name, frontendUrl });
|
2026-04-09 21:34:04 +05:30
|
|
|
const text = `Welcome to RE-Quest, ${name || 'User'}! Your real estate journey starts now.`;
|
2025-12-22 11:57:16 +05:30
|
|
|
|
|
|
|
|
return this.sendEmail({
|
|
|
|
|
to: email,
|
2026-04-09 21:34:04 +05:30
|
|
|
subject: 'Welcome to RE-Quest!',
|
2025-12-22 11:57:16 +05:30
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|