salix/services/mailer/application/route/notification.js

180 lines
5.8 KiB
JavaScript

var express = require('express');
var router = new express.Router();
var config = require('../config.js');
var mail = require('../mail.js');
var template = require('../template.js');
var httpRequest = require('request');
// Printer setup
router.post('/printer-setup/:clientId', function(request, response) {
mail.sendWithTemplate('printer-setup', {clientId: request.params.clientId}, error => {
if (error)
return response.status(400).json({message: error.message});
return response.json();
});
});
// Printer setup preview
router.get('/printer-setup/:clientId', function(request, response) {
template.get('printer-setup', {clientId: request.params.clientId, isPreview: true}, (error, result) => {
if (error)
return response.status(400).json({message: error.message});
response.send(result.body);
});
});
// Client welcome
router.post('/client-welcome/:clientId', function(request, response) {
mail.sendWithTemplate('client-welcome', {clientId: request.params.clientId}, error => {
if (error)
return response.status(400).json({message: error.message});
return response.json();
});
});
// Client welcome preview
router.get('/client-welcome/:clientId', function(request, response) {
template.get('client-welcome', {clientId: request.params.clientId, isPreview: true}, (error, result) => {
if (error)
return response.status(400).json({message: error.message});
response.send(result.body);
});
});
// Client SEPA CORE
router.post('/sepa-core/:clientId', function(request, response) {
let path = `${request.proxyHost}/print/manuscript/sepa-core/${request.params.clientId}`;
let options = {
url: path,
method: 'GET',
headers: {
'Authorization': request.headers.authorization
}
}
let httpStream = httpRequest(options, function(error, httpResponse, body) {
if (error || httpResponse.statusCode != 200)
return response.status(400).json({message: error.message});
});
if (httpStream)
mail.sendWithTemplate('sepa-core', {
clientId: request.params.clientId,
attachments: [{filename: 'sepa-core.pdf', content: httpStream}]
}, error => {
if (error)
return response.status(400).json({message: error.message});
return response.json();
});
});
// Client SEPA CORE preview
router.get('/sepa-core/:clientId', function(request, response) {
template.get('sepa-core', {
clientId: request.params.clientId,
token: request.user.token,
isPreview: true
}, (error, result) => {
if (error)
return response.status(400).json({message: error.message});
response.send(result.body);
});
});
// Single user notification
/* router.post('/:recipient/noticeUserSend', function(request, response) {
var params = {
recipient: request.params.recipient,
sender: request.body.sender,
category: request.body.category,
message: request.body.message
};
if (params.sender == params.recipient)
return;
let query = `SELECT COUNT(*) isEnabled
FROM noticeSubscription s
JOIN noticeCategory c ON c.id = s.noticeCategoryFk AND c.isEnabled
WHERE c.keyName = ? AND s.userFk = ?`;
database.pool.query(query, [params.category, params.recipient, params.sender], (error, result) => {
mail.sendWithTemplate('notification-notice', params, result => {
return response.json(result);
});
});
}); */
// Send notification to all user subscribed to category
/* router.post('/:category/noticeCategorySend', function(request, response) {
var params = {
sender: request.body.sender,
category: request.params.category,
message: request.body.message
};
let query = `SELECT s.userFk id FROM noticeSubscription s JOIN noticeCategory
c ON c.id = s.noticeCategoryFk AND c.isEnabled WHERE c.keyName = ? AND s.userFk != ?`;
database.pool.query(query, [params.category, params.sender], (error, result) => {
result.forEach(function(user) {
params.recipient = user.id;
mail.sendWithTemplate('notification-notice', params, result => {
return response.json(result);
});
}, this);
});
}); */
// Send system notification
/* router.post('/:recipient/noticeSystemSend', function(request, response) {
var params = {
recipient: request.params.recipient,
sender: settings.smtp().auth.id,
category: request.body.category,
message: request.body.message
};
mail.sendWithTemplate('notification-notice', params, result => {
return response.json(result);
});
}); */
// Payment method changes
router.post('/payment-update/:clientId', function(request, response) {
mail.sendWithTemplate('payment-update', {clientId: request.params.clientId}, error => {
if (error)
return response.status(400).json({message: error.message});
return response.json();
});
});
// Send notification to alias creditInsurance on client deactivate
router.post('/client-deactivate/:clientId', function(request, response) {
var params = {
alias: 'creditInsurance',
code: 'clientDeactivate',
bodyParams: {
clientId: request.params.clientId
}
};
mail.sendWithTemplate('notification-alias', params, error => {
if (error)
response.status(400).json({message: error.message});
return response.json();
});
});
module.exports = router;