@mbc-cqrs-serverless/core
Version:
CQRS and event base core
98 lines • 4.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,
};
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,
});
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 },
},
});
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;
}
}
};
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