n8n
Version:
n8n Workflow Automation Tool
165 lines • 7.93 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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserManagementMailer = void 0;
const typedi_1 = require("typedi");
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const handlebars_1 = __importDefault(require("handlebars"));
const path_1 = require("path");
const n8n_workflow_1 = require("n8n-workflow");
const config_1 = __importDefault(require("../../config"));
const user_repository_1 = require("../../databases/repositories/user.repository");
const InternalHooks_1 = require("../../InternalHooks");
const Logger_1 = require("../../Logger");
const url_service_1 = require("../../services/url.service");
const internal_server_error_1 = require("../../errors/response-errors/internal-server.error");
const utils_1 = require("../../utils");
const NodeMailer_1 = require("./NodeMailer");
const templates = {};
async function getTemplate(templateName, defaultFilename = `${templateName}.html`) {
let template = templates[templateName];
if (!template) {
const templateOverride = config_1.default.getEnv(`userManagement.emails.templates.${templateName}`);
let markup;
if (templateOverride && (0, fs_1.existsSync)(templateOverride)) {
markup = await (0, promises_1.readFile)(templateOverride, 'utf-8');
}
else {
markup = await (0, promises_1.readFile)((0, path_1.join)(__dirname, `templates/${defaultFilename}`), 'utf-8');
}
template = handlebars_1.default.compile(markup);
templates[templateName] = template;
}
return template;
}
let UserManagementMailer = class UserManagementMailer {
constructor(userRepository, logger, urlService) {
this.userRepository = userRepository;
this.logger = logger;
this.urlService = urlService;
this.isEmailSetUp =
config_1.default.getEnv('userManagement.emails.mode') === 'smtp' &&
config_1.default.getEnv('userManagement.emails.smtp.host') !== '';
if (this.isEmailSetUp) {
this.mailer = typedi_1.Container.get(NodeMailer_1.NodeMailer);
}
}
async verifyConnection() {
if (!this.mailer)
throw new n8n_workflow_1.ApplicationError('No mailer configured.');
return await this.mailer.verifyConnection();
}
async invite(inviteEmailData) {
var _a;
const template = await getTemplate('invite');
const result = await ((_a = this.mailer) === null || _a === void 0 ? void 0 : _a.sendMail({
emailRecipients: inviteEmailData.email,
subject: 'You have been invited to n8n',
body: template(inviteEmailData),
}));
return result !== null && result !== void 0 ? result : { emailSent: false };
}
async passwordReset(passwordResetData) {
var _a;
const template = await getTemplate('passwordReset', 'passwordReset.html');
const result = await ((_a = this.mailer) === null || _a === void 0 ? void 0 : _a.sendMail({
emailRecipients: passwordResetData.email,
subject: 'n8n password reset',
body: template(passwordResetData),
}));
return result !== null && result !== void 0 ? result : { emailSent: false };
}
async notifyWorkflowShared({ sharer, newShareeIds, workflow, }) {
if (!this.mailer)
return;
const recipients = await this.userRepository.getEmailsByIds(newShareeIds);
if (recipients.length === 0)
return;
const emailRecipients = recipients.map(({ email }) => email);
const populateTemplate = await getTemplate('workflowShared', 'workflowShared.html');
const baseUrl = this.urlService.getInstanceBaseUrl();
try {
const result = await this.mailer.sendMail({
emailRecipients,
subject: `${sharer.firstName} has shared an n8n workflow with you`,
body: populateTemplate({
workflowName: workflow.name,
workflowUrl: `${baseUrl}/workflow/${workflow.id}`,
}),
});
if (!result)
return { emailSent: false };
this.logger.info('Sent workflow shared email successfully', { sharerId: sharer.id });
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onUserTransactionalEmail({
user_id: sharer.id,
message_type: 'Workflow shared',
public_api: false,
});
return result;
}
catch (e) {
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onEmailFailed({
user: sharer,
message_type: 'Workflow shared',
public_api: false,
});
const error = (0, utils_1.toError)(e);
throw new internal_server_error_1.InternalServerError(`Please contact your administrator: ${error.message}`);
}
}
async notifyCredentialsShared({ sharer, newShareeIds, credentialsName, }) {
if (!this.mailer)
return;
const recipients = await this.userRepository.getEmailsByIds(newShareeIds);
if (recipients.length === 0)
return;
const emailRecipients = recipients.map(({ email }) => email);
const populateTemplate = await getTemplate('credentialsShared', 'credentialsShared.html');
const baseUrl = this.urlService.getInstanceBaseUrl();
try {
const result = await this.mailer.sendMail({
emailRecipients,
subject: `${sharer.firstName} has shared an n8n credential with you`,
body: populateTemplate({ credentialsName, credentialsListUrl: `${baseUrl}/credentials` }),
});
if (!result)
return { emailSent: false };
this.logger.info('Sent credentials shared email successfully', { sharerId: sharer.id });
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onUserTransactionalEmail({
user_id: sharer.id,
message_type: 'Credentials shared',
public_api: false,
});
return result;
}
catch (e) {
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onEmailFailed({
user: sharer,
message_type: 'Credentials shared',
public_api: false,
});
const error = (0, utils_1.toError)(e);
throw new internal_server_error_1.InternalServerError(`Please contact your administrator: ${error.message}`);
}
}
};
exports.UserManagementMailer = UserManagementMailer;
exports.UserManagementMailer = UserManagementMailer = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [user_repository_1.UserRepository,
Logger_1.Logger,
url_service_1.UrlService])
], UserManagementMailer);
//# sourceMappingURL=UserManagementMailer.js.map
;