UNPKG

@wepublish/api

Version:
81 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailContext = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const mail_controller_1 = require("./mail.controller"); let MailContext = exports.MailContext = class MailContext { constructor(props) { this.mailProvider = props.mailProvider; this.prisma = props.prisma; this.defaultFromAddress = props.defaultFromAddress; this.defaultReplyToAddress = props.defaultReplyToAddress; } sendMail(opts) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (opts.externalMailTemplateId) { yield new mail_controller_1.MailController(this.prisma, this, opts).sendMail(); } }); } sendRemoteTemplateDirect({ remoteTemplate, recipient, data, mailLogID }) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.mailProvider) { throw new Error('MailProvider is not set!'); } yield this.mailProvider.sendMail({ mailLogID, recipient, replyToAddress: (_a = this.defaultReplyToAddress) !== null && _a !== void 0 ? _a : this.defaultFromAddress, subject: '', template: remoteTemplate, templateData: data }); }); } getUserTemplateName(event, throwOnMissing = true) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const userFlowMail = yield this.prisma.userFlowMail.findUnique({ where: { event }, include: { mailTemplate: true } }); // Return null if no mailtemplete is defined and function is not called "throwOnMissing" if (!throwOnMissing) { return ((_a = userFlowMail === null || userFlowMail === void 0 ? void 0 : userFlowMail.mailTemplate) === null || _a === void 0 ? void 0 : _a.externalMailTemplateId) || null; } if (!userFlowMail) { throw new Error(`No UserFlowMail defined for event ${event}`); } if (!userFlowMail.mailTemplate) { throw new Error(`No email template defined for event ${event}`); } return userFlowMail.mailTemplate.externalMailTemplateId; }); } getTemplates() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.mailProvider.getTemplates(); }); } getUsedTemplateIdentifiers() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const intervals = yield this.prisma.subscriptionInterval.findMany({ include: { mailTemplate: true } }); return intervals.flatMap(interval => { var _a, _b; return (_b = (_a = interval.mailTemplate) === null || _a === void 0 ? void 0 : _a.externalMailTemplateId) !== null && _b !== void 0 ? _b : []; }); }); } }; exports.MailContext = MailContext = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [Object]) ], MailContext); //# sourceMappingURL=mail-context.js.map