n8n
Version:
n8n Workflow Automation Tool
162 lines • 7.9 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 config_1 = require("@n8n/config");
const user_repository_1 = require("../../databases/repositories/user.repository");
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 event_service_1 = require("../../events/event.service");
let UserManagementMailer = class UserManagementMailer {
constructor(globalConfig, logger, userRepository, urlService) {
this.logger = logger;
this.userRepository = userRepository;
this.urlService = urlService;
this.templatesCache = {};
const emailsConfig = globalConfig.userManagement.emails;
this.isEmailSetUp = emailsConfig.mode === 'smtp' && emailsConfig.smtp.host !== '';
this.templateOverrides = emailsConfig.template;
if (this.isEmailSetUp) {
this.mailer = typedi_1.Container.get(NodeMailer_1.NodeMailer);
}
}
async invite(inviteEmailData) {
if (!this.mailer)
return { emailSent: false };
const template = await this.getTemplate('invite');
const result = await this.mailer.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) {
if (!this.mailer)
return { emailSent: false };
const template = await this.getTemplate('passwordReset', 'passwordReset.html');
const result = await this.mailer.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 { emailSent: false };
const recipients = await this.userRepository.getEmailsByIds(newShareeIds);
if (recipients.length === 0)
return { emailSent: false };
const emailRecipients = recipients.map(({ email }) => email);
const populateTemplate = await this.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 });
typedi_1.Container.get(event_service_1.EventService).emit('user-transactional-email-sent', {
userId: sharer.id,
messageType: 'Workflow shared',
publicApi: false,
});
return result;
}
catch (e) {
typedi_1.Container.get(event_service_1.EventService).emit('email-failed', {
user: sharer,
messageType: 'Workflow shared',
publicApi: 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 { emailSent: false };
const recipients = await this.userRepository.getEmailsByIds(newShareeIds);
if (recipients.length === 0)
return { emailSent: false };
const emailRecipients = recipients.map(({ email }) => email);
const populateTemplate = await this.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}/home/credentials`,
}),
});
if (!result)
return { emailSent: false };
this.logger.info('Sent credentials shared email successfully', { sharerId: sharer.id });
typedi_1.Container.get(event_service_1.EventService).emit('user-transactional-email-sent', {
userId: sharer.id,
messageType: 'Credentials shared',
publicApi: false,
});
return result;
}
catch (e) {
typedi_1.Container.get(event_service_1.EventService).emit('email-failed', {
user: sharer,
messageType: 'Credentials shared',
publicApi: false,
});
const error = (0, utils_1.toError)(e);
throw new internal_server_error_1.InternalServerError(`Please contact your administrator: ${error.message}`);
}
}
async getTemplate(templateName, defaultFilename = `${templateName}.html`) {
let template = this.templatesCache[templateName];
if (!template) {
const templateOverride = this.templateOverrides[templateName];
const templatePath = templateOverride && (0, fs_1.existsSync)(templateOverride)
? templateOverride
: (0, path_1.join)(__dirname, `templates/${defaultFilename}`);
const markup = await (0, promises_1.readFile)(templatePath, 'utf-8');
template = handlebars_1.default.compile(markup);
this.templatesCache[templateName] = template;
}
return template;
}
};
exports.UserManagementMailer = UserManagementMailer;
exports.UserManagementMailer = UserManagementMailer = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [config_1.GlobalConfig,
Logger_1.Logger,
user_repository_1.UserRepository,
url_service_1.UrlService])
], UserManagementMailer);
//# sourceMappingURL=UserManagementMailer.js.map
;