Files
canina/backend/src/notifications/transactional-emails.service.ts
T
devops 625c3f5ce2
Deploy Canina / deploy (push) Failing after 1m57s
feat(notifications): send branded transactional emails for shop events
Add Canina-branded welcome, contact confirmation/admin notify, and order created/paid/shipped emails via support@canina.ir without blocking the SMS flows.
2026-11-10 13:27:37 +00:00

123 lines
7.6 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { EmailService } from './email.service';
import {
escapeHtml,
renderCaninaEmailLayout,
textToHtmlParagraphs,
} from './email-layout';
const ADMIN_NOTIFY_TO = [
'support@canina.ir',
'parsa.aghaee@gmail.com',
] as const;
@Injectable()
export class TransactionalEmailsService {
private readonly logger = new Logger(TransactionalEmailsService.name);
constructor(private readonly emailService: EmailService) {}
private async sendSafe(to: string, subject: string, text: string, html: string) {
try {
const res = await this.emailService.sendMail({ to, subject, text, html });
if (!res.success) {
this.logger.warn(`Email to ${to} failed: ${res.error || 'unknown'}`);
}
return res;
} catch (err) {
this.logger.warn(`Email to ${to} threw: ${(err as Error).message}`);
return { success: false, error: (err as Error).message };
}
}
async sendWelcome(to: string | null | undefined, firstName?: string | null) {
if (!to) return;
const name = (firstName || '').trim() || 'کاربر گرامی';
const subject = 'به کنینا خوش آمدید';
const text = `سلام ${name}،\n\nثبت‌نام شما در فروشگاه کنینا با موفقیت انجام شد.\n\ncanina.ir`;
const html = renderCaninaEmailLayout({
title: subject,
bodyHtml: `<p style="margin:0 0 12px;">سلام ${escapeHtml(name)}،</p><p style="margin:0;">ثبت‌نام شما در فروشگاه کنینا با موفقیت انجام شد. از این به بعد می‌توانید سفارش‌ها و وضعیت حساب خود را از سایت پیگیری کنید.</p>`,
});
await this.sendSafe(to, subject, text, html);
}
async sendOrderCreated(to: string | null | undefined, order: { id?: string; trackingNumber?: string | null; totalAmount?: number | string | null }) {
if (!to) return;
const orderRef = escapeHtml(String(order.trackingNumber || order.id || ''));
const amount = Number(order.totalAmount || 0);
const subject = 'سفارش شما در کنینا ثبت شد';
const text = `سفارش شما ثبت شد.\nشماره/کد: ${order.trackingNumber || order.id || ''}\nمبلغ: ${amount.toLocaleString('fa-IR')} تومان`;
const html = renderCaninaEmailLayout({
title: subject,
bodyHtml: `<p style="margin:0 0 12px;">سفارش شما ثبت شد.</p><p style="margin:0 0 8px;">شناسه/کد پیگیری: <b dir="ltr">${orderRef}</b></p><p style="margin:0;">مبلغ سفارش: <b>${escapeHtml(amount.toLocaleString('fa-IR'))}</b> تومان</p><p style="margin:16px 0 0;font-size:13px;color:#7A9B9F;">این پیام به‌تنهایی به‌معنای تأیید پرداخت نیست؛ وضعیت پرداخت را در حساب کاربری بررسی کنید.</p>`,
});
await this.sendSafe(to, subject, text, html);
}
async sendOrderPaid(to: string | null | undefined, order: { id?: string; trackingNumber?: string | null; totalAmount?: number | string | null }) {
if (!to) return;
const orderRef = escapeHtml(String(order.trackingNumber || order.id || ''));
const amount = Number(order.totalAmount || 0);
const subject = 'پرداخت سفارش کنینا تأیید شد';
const text = `پرداخت سفارش تأیید شد.\nشماره/کد: ${order.trackingNumber || order.id || ''}\nمبلغ: ${amount.toLocaleString('fa-IR')} تومان`;
const html = renderCaninaEmailLayout({
title: subject,
bodyHtml: `<p style="margin:0 0 12px;">پرداخت سفارش شما با موفقیت تأیید شد.</p><p style="margin:0 0 8px;">شناسه/کد پیگیری: <b dir="ltr">${orderRef}</b></p><p style="margin:0;">مبلغ پرداخت‌شده: <b>${escapeHtml(amount.toLocaleString('fa-IR'))}</b> تومان</p>`,
});
await this.sendSafe(to, subject, text, html);
}
async sendOrderShipped(
to: string | null | undefined,
order: { id?: string; trackingNumber?: string | null },
trackingCode?: string,
) {
if (!to) return;
const track = escapeHtml(String(trackingCode || order.trackingNumber || 'ارسال با پیک شهری'));
const orderRef = escapeHtml(String(order.trackingNumber || order.id || ''));
const subject = 'سفارش کنینا ارسال شد';
const text = `سفارش شما ارسال شد.\nسفارش: ${order.trackingNumber || order.id || ''}\nرهگیری: ${trackingCode || order.trackingNumber || 'ارسال با پیک شهری'}`;
const html = renderCaninaEmailLayout({
title: subject,
bodyHtml: `<p style="margin:0 0 12px;">وضعیت سفارش به ارسال‌شده تغییر کرد.</p><p style="margin:0 0 8px;">سفارش: <b dir="ltr">${orderRef}</b></p><p style="margin:0;">کد/روش ارسال: <b dir="ltr">${track}</b></p>`,
});
await this.sendSafe(to, subject, text, html);
}
async sendContactConfirmation(to: string | null | undefined, name: string, message: string, submissionId?: string) {
if (!to) return;
const subject = 'پیام شما در کنینا ثبت شد';
const text = `سلام ${name}،\n\nپیام شما ثبت شد و به‌زودی بررسی می‌شود.\nشناسه: ${submissionId || ''}\n\n${message}`;
const html = renderCaninaEmailLayout({
title: subject,
bodyHtml: `<p style="margin:0 0 12px;">سلام ${escapeHtml(name)}،</p><p style="margin:0 0 16px;">پیام شما از طریق فرم تماس کنینا ثبت شد و به‌زودی بررسی می‌شود.</p><div style="padding:14px;background:#F4F7F6;border:1px solid #E5ECE9;border-right:3px solid #F1B646;"><p style="margin:0;white-space:pre-wrap;">${textToHtmlParagraphs(message || '')}</p></div>${submissionId ? `<p style="margin:16px 0 0;font-size:13px;color:#7A9B9F;">شناسه پیگیری: <span dir="ltr">#${escapeHtml(submissionId)}</span></p>` : ''}`,
});
await this.sendSafe(to, subject, text, html);
}
async notifyAdminsAboutContact(dto: {
name: string;
phone: string;
email?: string;
subject?: string;
message: string;
submissionId?: string;
}) {
const subject = `پیام جدید فرم تماس کنینا${dto.subject ? ` — ${dto.subject}` : ''}`;
const text = `نام: ${dto.name}\nتلفن: ${dto.phone}\nایمیل: ${dto.email || '-'}\nموضوع: ${dto.subject || '-'}\nشناسه: ${dto.submissionId || '-'}\n\n${dto.message}`;
const html = renderCaninaEmailLayout({
title: 'پیام جدید از فرم تماس',
bodyHtml: `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">
<tr><td style="padding:10px 12px;background:#F4F7F6;border:1px solid #E5ECE9;width:110px;">نام</td><td style="padding:10px 12px;border:1px solid #E5ECE9;">${escapeHtml(dto.name)}</td></tr>
<tr><td style="padding:10px 12px;background:#F4F7F6;border:1px solid #E5ECE9;">تلفن</td><td style="padding:10px 12px;border:1px solid #E5ECE9;" dir="ltr">${escapeHtml(dto.phone)}</td></tr>
<tr><td style="padding:10px 12px;background:#F4F7F6;border:1px solid #E5ECE9;">ایمیل</td><td style="padding:10px 12px;border:1px solid #E5ECE9;" dir="ltr">${escapeHtml(dto.email || '-')}</td></tr>
<tr><td style="padding:10px 12px;background:#F4F7F6;border:1px solid #E5ECE9;vertical-align:top;">پیام</td><td style="padding:10px 12px;border:1px solid #E5ECE9;white-space:pre-wrap;">${textToHtmlParagraphs(dto.message || '')}</td></tr>
</table>`,
});
await Promise.all(
ADMIN_NOTIFY_TO.map((to) => this.sendSafe(to, subject, text, html)),
);
}
}