@mbc-cqrs-serverless/core
Version:
CQRS and event base core
199 lines • 9.34 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var EmailService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailService = void 0;
const client_sesv2_1 = require("@aws-sdk/client-sesv2");
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const nodemailer_1 = __importDefault(require("nodemailer"));
const CLIENT_INSTANCE = Symbol();
let EmailService = EmailService_1 = class EmailService {
constructor(config) {
this.config = config;
this.logger = new common_1.Logger(EmailService_1.name);
this[CLIENT_INSTANCE] = new client_sesv2_1.SESv2Client({
endpoint: config.get('SES_ENDPOINT'),
region: config.get('SES_REGION'),
});
}
async sendEmail(msg) {
try {
const fromAddress = msg.fromAddr || this.config.get('SES_FROM_EMAIL');
const destination = {
ToAddresses: msg.toAddrs,
CcAddresses: msg.ccAddrs,
BccAddresses: msg.bccAddrs,
};
const emailTags = msg.emailTags?.map((tag) => ({
Name: tag.name,
Value: tag.value,
}));
if (!msg.attachments || msg.attachments.length === 0) {
this.logger.log(`Sending simple email to ${msg.toAddrs.join(', ')}`);
const command = new client_sesv2_1.SendEmailCommand({
FromEmailAddress: fromAddress,
Destination: destination,
Content: {
Simple: {
Subject: { Data: msg.subject },
Body: { Html: { Data: msg.body } },
},
},
ReplyToAddresses: msg.replyToAddrs,
EmailTags: emailTags,
});
const result = await this[CLIENT_INSTANCE].send(command);
return result;
}
this.logger.log(`Sending raw email with attachments to ${msg.toAddrs.join(', ')}`);
const transporter = nodemailer_1.default.createTransport({
streamTransport: true,
newline: 'unix',
buffer: true,
});
const mailOptions = {
from: fromAddress,
to: msg.toAddrs,
cc: msg.ccAddrs,
bcc: msg.bccAddrs,
replyTo: msg.replyToAddrs,
subject: msg.subject,
html: msg.body,
attachments: msg.attachments,
};
const emailBuffer = await new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (err, info) => {
if (err)
return reject(err);
resolve(info.message);
});
});
const command = new client_sesv2_1.SendEmailCommand({
Destination: destination,
Content: {
Raw: { Data: emailBuffer },
},
EmailTags: emailTags,
});
const result = await this[CLIENT_INSTANCE].send(command);
return result;
}
catch (error) {
this.logger.error(`Failed to send email to ${msg.toAddrs.join(', ')}`, error.stack);
throw error;
}
}
/**
* Sends an email using SES v2 Inline Templates.
* @remarks
* Supports a hybrid mode for local development:
* - PRODUCTION: Uses AWS SES Native Inline Templates.
* - OFFLINE/LOCAL: Falls back to 'Simple' email with manual variable substitution
* to bypass limitations of local emulators (like serverless-offline-ses-v2).
*/
async sendInlineTemplateEmail(msg) {
// 1. Validation: Fail early if no recipients
if (!msg.toAddrs.length && !msg.ccAddrs?.length && !msg.bccAddrs?.length) {
this.logger.warn('Email skipped: No recipients provided.');
return;
}
try {
const fromAddress = msg.fromAddr || this.config.get('SES_FROM_EMAIL');
const isOffline = process.env.IS_OFFLINE === 'true' || process.env.IS_OFFLINE === '1';
// 2. Privacy-safe logging
this.logger.log(`Sending inline template email to ${msg.toAddrs.length} recipient(s). Mode: ${isOffline ? 'LOCAL_EMULATION' : 'AWS_NATIVE'}`);
let contentPayload;
if (isOffline) {
// --- LOCAL FALLBACK LOGIC ---
this.logger.warn('⚠️ IS_OFFLINE detected: Switching to manual template compilation for local testing.');
// Helper to access deep properties (e.g., "user.profile.name" or "認証コード")
const getDeepValue = (obj, path) => {
return path.split('.').reduce((acc, part) => acc && acc[part], obj);
};
const replaceVariables = (text, data) => {
if (!text)
return '';
// Regex Explanation: /\{\{([^}]{1,255})\}\}/g
// 1. [^}]: Matches any character that is NOT a closing curly brace.
// 2. {1,255}: Limits length to prevent ReDoS.
return text.replace(/\{\{([^}]{1,255})\}\}/g, (_, expression) => {
const key = expression.trim(); // Remove spaces (e.g. {{ code }} -> code)
const value = getDeepValue(data || {}, key);
// AWS SES behavior: If the value is missing, the tag remains in the output
// or is handled by the template engine. We'll keep the original tag here.
return value !== undefined ? String(value) : `{{${expression}}}`;
});
};
contentPayload = {
Simple: {
Subject: { Data: replaceVariables(msg.template.subject, msg.data) },
Body: {
Html: { Data: replaceVariables(msg.template.html, msg.data) },
Text: msg.template.text
? { Data: replaceVariables(msg.template.text, msg.data) }
: undefined,
},
},
};
}
else {
// --- PRODUCTION AWS LOGIC ---
// Use native SES v2 Inline Template features
contentPayload = {
Template: {
TemplateContent: {
Subject: msg.template.subject,
Html: msg.template.html,
...(msg.template.text && { Text: msg.template.text }),
},
// Ensure data is valid JSON string
TemplateData: JSON.stringify(msg.data || {}),
},
};
}
const emailTags = msg.emailTags?.map((tag) => ({
Name: tag.name,
Value: tag.value,
}));
const command = new client_sesv2_1.SendEmailCommand({
FromEmailAddress: fromAddress,
Destination: {
ToAddresses: msg.toAddrs,
CcAddresses: msg.ccAddrs,
BccAddresses: msg.bccAddrs,
},
ReplyToAddresses: msg.replyToAddrs,
ConfigurationSetName: msg.configurationSetName,
Content: contentPayload,
EmailTags: emailTags,
});
const result = await this[CLIENT_INSTANCE].send(command);
this.logger.log(`Template email sent successfully: ${result.MessageId}`);
return result;
}
catch (error) {
this.logger.error(`Failed to send inline template email.
ConfigSet: ${msg.configurationSetName || 'None'}
Recipients (Count): ${msg.toAddrs.length}`, error.stack);
throw error;
}
}
};
exports.EmailService = EmailService;
exports.EmailService = EmailService = EmailService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], EmailService);
//# sourceMappingURL=email.service.js.map