From 23436286d5a1e092ef5ac94965e61006fa2862d6 Mon Sep 17 00:00:00 2001 From: Mikhail Chechnev Date: Sun, 14 Mar 2021 18:52:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D1=80=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=20SMTP-=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B5=D1=80=D1=83=20(sHost,=20nPort,=20sUser,=20sP?= =?UTF-8?q?ass,=20sFrom)=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BE=D0=B1=D1=8F=D0=B7=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/notifier.js | 32 ++++++++++++++++++++------------ models/obj_config.js | 10 +++++----- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/core/notifier.js b/core/notifier.js index a7dca7a..7f1e4ed 100644 --- a/core/notifier.js +++ b/core/notifier.js @@ -10,7 +10,7 @@ const _ = require("lodash"); //Работа с массивами и коллекциями const EventEmitter = require("events"); //Обработчик пользовательских событий const { ServerError } = require("./server_errors"); //Типовая ошибка -const { SERR_OBJECT_BAD_INTERFACE } = require("./constants"); //Общесистемные константы +const { SERR_OBJECT_BAD_INTERFACE, SERR_MAIL_FAILED } = require("./constants"); //Общесистемные константы const { makeErrorText, validateObject, sendMail } = require("./utils"); //Вспомогательные функции const prmsNotifierSchema = require("../models/prms_notifier"); //Схемы валидации параметров функций класса @@ -112,17 +112,25 @@ class Notifier extends EventEmitter { //Работаем только по неотправленным уведомлениям if (!message.bSent) { try { - //Отправляем - await sendMail({ - mail: this.mail, - sTo: message.sTo, - sSubject: message.sSubject, - sMessage: message.sMessage - }); - //Протоколируем отправку - await this.logger.info(`Сообщение с темой "${message.sSubject}" отпрвлено ${message.sTo}`); - //Говорим, что отправлено - message.bSent = true; + //Если всё в порядке с настройками + if (this.mail.sHost && this.mail.nPort && this.mail.sUser && this.mail.sPass && this.mail.sFrom) { + //Отправляем + await sendMail({ + mail: this.mail, + sTo: message.sTo, + sSubject: message.sSubject, + sMessage: message.sMessage + }); + //Протоколируем отправку + await this.logger.info(`Сообщение с темой "${message.sSubject}" отпрвлено ${message.sTo}`); + //Говорим, что отправлено + message.bSent = true; + } else { + throw new ServerError( + SERR_MAIL_FAILED, + 'Не указаны параметры подключения к SMTP-сервереру (проверьте секцию "mail" в файле конфигурации)' + ); + } } catch (e) { await this.logger.error( `Ошибка отправки сообщения с темой "${message.sSubject}" для ${message.sTo}: ${makeErrorText( diff --git a/models/obj_config.js b/models/obj_config.js index 6dbcd9d..e3a2170 100644 --- a/models/obj_config.js +++ b/models/obj_config.js @@ -208,7 +208,7 @@ const mail = new Schema({ //Адреc сервера SMTP sHost: { type: String, - required: true, + required: false, message: { type: path => `Адреc сервера SMTP (${path}) имеет некорректный тип данных (ожидалось - String)`, required: path => `Не указан aдреc сервера SMTP (${path})` @@ -217,7 +217,7 @@ const mail = new Schema({ //Порт сервера SMTP nPort: { type: Number, - required: true, + required: false, message: { type: path => `Порт сервера SMTP (${path}) имеет некорректный тип данных (ожидалось - Number)`, required: path => `Не указан порт сервера SMTP (${path})` @@ -226,7 +226,7 @@ const mail = new Schema({ //Имя пользователя SMTP-сервера sUser: { type: String, - required: true, + required: false, message: { type: path => `Имя пользователя SMTP-сервера (${path}) имеет некорректный тип данных (ожидалось - String)`, required: path => `Не указано имя пользователя SMTP-сервера (${path})` @@ -235,7 +235,7 @@ const mail = new Schema({ //Пароль пользователя SMTP-сервера sPass: { type: String, - required: true, + required: false, message: { type: path => `Пароль пользователя SMTP-сервера (${path}) имеет некорректный тип данных (ожидалось - String)`, @@ -245,7 +245,7 @@ const mail = new Schema({ //Наименование отправителя для исходящих сообщений sFrom: { type: String, - required: true, + required: false, message: { type: path => `Наименование отправителя для исходящих сообщений (${path}) имеет некорректный тип данных (ожидалось - String)`,