/var/www/greso.tech/server/nsm/services
NameSizeModeActions
auth.service.js9740644editdlrm
company.service.js15340644editdlrm
index.js9350644editdlrm
inventory.service.js21180644editdlrm
lead.service.js15180644editdlrm
mail.service.js99910644editdlrm
order.service.js53520644editdlrm
rateArea.service.js20220644editdlrm
system.service.js8720644editdlrm
webSocket.service.js46110644editdlrm
Edit: /var/www/greso.tech/server/nsm/services/mail.service.js (9991B)
const logger = require('../logger')({ service: 'AuthService' }) const nodemailer = require('nodemailer') const { NotFound, BadRequest } = require('../responseModels/errors') module.exports = class MailService { constructor({ db, config }) { this.db = db this.config = config this.companyConfig = config this.mail logger.child({ service: 'MailService', method: 'Constructor' }).info('Success') } async Run() { logger.child({ service: 'MailService', method: 'Run' }).info('Success') } async SendMail(email, html, companyId) { var company; if (companyId != null) { company = await this.db.company.findOne({ where: { id: companyId } }) } if (!company) { throw new BadRequest({ code: 2003, lang: 'en'//req.user?.languageCode }) } if (company != null) { await this.setTransport(company) logger.child({ service: 'MailService', method: 'createTransport' }).info('Success') } const html_message = { from: this.companyConfig['smtp_user_name'] ??this.config.mail.userName, // адрес отправителя to: email, // список получателей "mailto1, mailto2" subject: 'New message', // заголовок //text: 'text', html: html } const result = this.mail.sendMail(html_message) // logger.child({ service: 'MailService', method: 'SendMail' }).info(result) logger.child({ service: 'MailService', method: 'SendMail' }).info('Success') return result } async SendResetPassword(email, linkId) { await this.setTransport(null) const html_message = { from: this.companyConfig['smtp_user_name'] ??this.config.mail.userName, // адрес отправителя to: email, // список получателей "mailto1, mailto2" subject: 'Reset password', // заголовок //text: 'text', html: 'For reset password click '+this.config.mail.resetPasswordCallbackUrl + '?l=' + linkId } const result = this.mail.sendMail(html_message) // logger.child({ service: 'MailService', method: 'SendMail' }).info(result) logger.child({ service: 'MailService', method: 'SendResetPassword' }).info('Success') return result } async SendDocument(orderDocument) { const document = await this.db.documents.findOne({ where: { id: orderDocument.documentId } }) if (!document) { throw new BadRequest({ code: 2045, lang: 'en'//req.user?.languageCode }) } const order = await this.db.order.findOne({ where: { id: orderDocument.orderId } }) if (!order) { throw new BadRequest({ code: 2032, lang: 'en'//req.user?.languageCode }) } const company = await this.db.company.findOne({ where: { id: order.companyId } }) if (!company) { throw new BadRequest({ code: 2003, lang: 'en'//req.user?.languageCode }) } if (company != null) { await this.setTransport(company) logger.child({ service: 'MailService', method: 'createTransport' }).info('Success') } const user = await this.db.user.findOne({ include: [ this.db.userEmail, this.db.userPhone ], where: { id: order.userId } }) if (!user) { throw new BadRequest({ code: 1003, lang: 'en'//req.user?.languageCode }) } const template = await this.db.emailTemplates.findOne({ where: { id: document.emailTemplateId } }) if (!template) { throw new BadRequest({ code: 2077, lang: 'en'//req.user?.languageCode }) } var body = this.getMacros(template.body, order, user, company, document, orderDocument) if (user?.userEmails[0]?.email != null) { await this.setTransport(company) logger.child({ service: 'MailService', method: 'createTransport' }).info('Success') const html_message = { from: this.companyConfig['smtp_user_name'] ?? this.config.mail.userName, // адрес отправителя to: user.userEmails[0].email, // список получателей "mailto1, mailto2" subject: template.subject, // заголовок //text: 'text', html: body } const result = await this.mail.sendMail(html_message) // logger.child({ service: 'MailService', method: 'SendMail' }).info(result) logger.child({ service: 'MailService', method: 'SendMail' }).info('Success') return result } return false; } async SendNewUser(userId, companyId) { const company = await this.db.company.findOne({ where: { id: companyId } }) if (!company) { throw new BadRequest({ code: 2003, lang: 'en'//req.user?.languageCode }) } var emailTemplateId = '' try{ this.companyConfig = JSON.parse(company.config); for (let index = 0; index < this.companyConfig.length; index++) { if(this.companyConfig[index].key == 'email_new_client'){ emailTemplateId = this.companyConfig[index].value } } }catch(e){ return false; } if(emailTemplateId=='') return false; const user = await this.db.user.findOne({ include: [ this.db.userEmail, this.db.userPhone ], where: { id: userId } }) if (!user) { throw new BadRequest({ code: 1003, lang: 'en'//req.user?.languageCode }) } const template = await this.db.emailTemplates.findOne({ where: { id: emailTemplateId } }) if (!template) { throw new BadRequest({ code: 2077, lang: 'en'//req.user?.languageCode }) } var body = this.getMacros(template.body, null, user, company, null, null) if(user?.userEmails[0]?.email!=null){ await this.setTransport(company) logger.child({ service: 'MailService', method: 'createTransport' }).info('Success') const html_message = { from: this.companyConfig['smtp_user_name'] ?? this.config.mail.userName, // адрес отправителя to: user.userEmails[0].email, // список получателей "mailto1, mailto2" subject: template.subject, // заголовок //text: 'text', html: body } const result = await this.mail.sendMail(html_message) logger.child({ service: 'MailService', method: 'SendMail' }).info('Success') return result } // logger.child({ service: 'MailService', method: 'SendMail' }).info(result) return false; } async setTransport(company) { if (company == null) { // SET DEFAULT this.mail = nodemailer.createTransport({ host: this.config.mail.smtpServer, port: this.config.mail.smtpPort, secure: true, auth: { user: this.config.mail.login, pass: this.config.mail.password }, }) return } let config = JSON.parse(company.config) this.companyConfig = {} for (let c in config) { this.companyConfig[config[c].key] = config[c].value; } // this.companyConfig = JSON.parse(company.config) this.mail = nodemailer.createTransport({ host: this.companyConfig['smtp_server'] ?? this.config.mail.smtpServer, port: this.companyConfig['smtp_port'] ?? this.config.mail.smtpPort, secure: true, auth: { user: this.companyConfig['smtp_login'] ?? this.config.mail.login, pass: this.companyConfig['smtp_password'] ?? this.config.mail.password }, }) } getMacros(body, order, user, company, document, orderDocument){ if(user!=null){ if(user.userEmails[0]!=null){ body = body.replace("_clientPickupClientName_",user.userEmails[0].name??'') body = body.replace("_clientPickupClientEmail_",user.userEmails[0].email??'') } } if(order!=null){ body = body.replace("_orderNumber_",order.number??'') } if(company!=null){ body = body.replace("_companyName_",company.name) body = body.replace("_companySite_",company.site) body = body.replace("_companyEmail_",company.email) body = body.replace("_companyPhone_",company.phone) body = body.replace("_companyMc_",company.mc) body = body.replace("_companyDotNr_",company.dotNr) } if(document!=null){ body = body.replace("_documentName_",document.name) } if(orderDocument!=null){ body = body.replace("_documentLink_",this.config.mail.documentCallbackUrl + "?document_id="+orderDocument.id) } return body; } }