UNPKG

@wepublish/api

Version:
109 lines 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailController = exports.mailLogType = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); const api_1 = require("../../../utils-api/src"); const crypto_1 = require("crypto"); const ONE_WEEK_IN_MINUTES = 7 * 24 * 60 * 60; var mailLogType; (function (mailLogType) { mailLogType[mailLogType["SubscriptionFlow"] = 0] = "SubscriptionFlow"; mailLogType[mailLogType["UserFlow"] = 1] = "UserFlow"; mailLogType[mailLogType["SystemMail"] = 2] = "SystemMail"; })(mailLogType || (exports.mailLogType = mailLogType = {})); class MailController { constructor(prismaService, mailContext, config) { this.prismaService = prismaService; this.mailContext = mailContext; this.config = config; this.logger = new common_1.Logger('MailController'); } /** * Build a string uniquely identifying the email delivery * @returns the identification string */ generateMailIdentifier() { return `${this.config.mailType}-${this.config.periodicJobRunDate ? this.config.periodicJobRunDate.toISOString() : 'null'}-${this.config.daysAwayFromEnding}-${this.config.externalMailTemplateId}-${this.config.recipient.id}`; } /** * Get the number of mails with the specified MailIdentifier. Any number > 0 * means the mail was already sent. * @returns number of mails with this identifier */ checkIfMailIsSent() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.prismaService.mailLog.count({ where: { mailIdentifier: this.generateMailIdentifier() } }); }); } /** * Build the data for passing it to the mail templates * @returns a HashMap of configuration data */ buildData() { // avoid unwanted data mutation by reference const recipient = JSON.parse(JSON.stringify(this.config.recipient)); recipient.password = 'hidden'; recipient.roleIDs = ['hidden']; if (!process.env['JWT_SECRET_KEY']) throw new Error('No JWT_SECRET_KEY defined in environment.'); return { user: recipient, optional: this.config.optionalData, jwt: (0, api_1.generateJWT)({ issuer: 'mailer', audience: 'audience', id: recipient.id, expiresInMinutes: ONE_WEEK_IN_MINUTES, secret: process.env['JWT_SECRET_KEY'] }) }; } /** * Send an email using a specific template with the configured mail provider. * This method stores an entry in the MailLog table for referencing and * re-trying the delivery. * @returns void */ sendMail() { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (this.config.isRetry && (yield this.checkIfMailIsSent())) { this.logger.warn(`Mail with id <${this.generateMailIdentifier()}> is already sent. Skipping...`); return; } const mailLogId = (0, crypto_1.randomUUID)(); yield this.mailContext.sendRemoteTemplateDirect({ mailLogID: mailLogId, remoteTemplate: this.config.externalMailTemplateId, recipient: this.config.recipient.email, data: this.buildData() }); yield this.prismaService.mailLog.create({ data: { id: mailLogId, recipient: { connect: { id: this.config.recipient.id } }, state: client_1.MailLogState.submitted, sentDate: new Date(), mailProviderID: this.mailContext.mailProvider.id || '', mailIdentifier: this.generateMailIdentifier(), mailTemplate: { connect: { externalMailTemplateId: this.config.externalMailTemplateId } } } }); }); } } exports.MailController = MailController; //# sourceMappingURL=mail.controller.js.map