2017-05-30 06:06:14 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = new express.Router();
|
2017-11-27 14:08:18 +00:00
|
|
|
var config = require('../config.js');
|
2017-06-12 11:41:02 +00:00
|
|
|
var mail = require('../mail.js');
|
2017-11-27 14:08:18 +00:00
|
|
|
var template = require('../template.js');
|
|
|
|
var httpRequest = require('request');
|
2017-12-26 09:02:47 +00:00
|
|
|
var auth = require('../auth.js');
|
2017-11-27 14:08:18 +00:00
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
// Auth middleware
|
|
|
|
var requestToken = function(request, response, next) {
|
|
|
|
auth.init(request, response, next);
|
|
|
|
};
|
2017-11-27 14:08:18 +00:00
|
|
|
|
|
|
|
// Printer setup
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/printer-setup/:clientId', requestToken, function(request, response) {
|
2017-11-27 14:08:18 +00:00
|
|
|
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
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/printer-setup/:clientId/preview', requestToken, function(request, response) {
|
2017-11-27 14:08:18 +00:00
|
|
|
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
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/client-welcome/:clientId', requestToken, function(request, response) {
|
2017-11-27 14:08:18 +00:00
|
|
|
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
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/client-welcome/:clientId/preview', requestToken, function(request, response) {
|
2017-11-27 14:08:18 +00:00
|
|
|
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
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/sepa-core/:companyId/:clientId', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
clientId: request.params.clientId,
|
|
|
|
companyId: request.params.companyId
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = `${request.proxyHost}/print/manuscript/sepa-core/${params.companyId}/${params.clientId}`;
|
2017-11-27 14:08:18 +00:00
|
|
|
let options = {
|
|
|
|
url: path,
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Authorization': request.headers.authorization
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let httpStream = httpRequest(options, function(error, httpResponse, body) {
|
|
|
|
if (error || httpResponse.statusCode != 200)
|
2017-12-26 09:02:47 +00:00
|
|
|
return response.status(400).json({message: error});
|
2017-11-27 14:08:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (httpStream)
|
2017-12-26 09:02:47 +00:00
|
|
|
params.attachments = [{filename: 'sepa-core.pdf', content: httpStream}];
|
|
|
|
|
|
|
|
mail.sendWithTemplate('sepa-core', params, error => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
return response.json();
|
2017-11-27 14:08:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Client SEPA CORE preview
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/sepa-core/:companyId/:clientId/preview', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
clientId: request.params.clientId,
|
|
|
|
companyId: request.params.companyId,
|
|
|
|
token: request.user.token,
|
|
|
|
isPreview: true
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('sepa-core', params, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
response.send(result.body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// First debtor letter
|
|
|
|
router.get('/letter-debtor-st/:companyId/:clientId', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
clientId: request.params.clientId,
|
|
|
|
companyId: request.params.companyId,
|
|
|
|
token: request.user.token
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = `${request.proxyHost}/print/manuscript/letter-debtor/${params.companyId}/${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});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (httpStream)
|
|
|
|
params.attachments = [{filename: 'extracto.pdf', content: httpStream}];
|
|
|
|
|
|
|
|
mail.sendWithTemplate('letter-debtor-st', params, error => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// First debtor letter preview
|
|
|
|
router.get('/letter-debtor-st/:companyId/:clientId/preview', requestToken, function(request, response) {
|
|
|
|
template.get('letter-debtor-st', {
|
2017-11-27 14:08:18 +00:00
|
|
|
clientId: request.params.clientId,
|
2017-12-26 09:02:47 +00:00
|
|
|
companyId: request.params.companyId,
|
2017-11-27 14:08:18 +00:00
|
|
|
token: request.user.token,
|
|
|
|
isPreview: true
|
|
|
|
}, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
response.send(result.body);
|
|
|
|
});
|
|
|
|
});
|
2017-10-02 09:19:55 +00:00
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
// Second debtor letter
|
|
|
|
router.get('/letter-debtor-nd/:companyId/:clientId', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
clientId: request.params.clientId,
|
|
|
|
companyId: request.params.companyId,
|
|
|
|
token: request.user.token
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = `${request.proxyHost}/print/manuscript/letter-debtor/${params.companyId}/${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});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (httpStream)
|
|
|
|
params.attachments = [{filename: 'extracto.pdf', content: httpStream}];
|
|
|
|
|
|
|
|
mail.sendWithTemplate('letter-debtor-nd', params, error => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Second debtor letter preview
|
|
|
|
router.get('/letter-debtor-nd/:companyId/:clientId/preview', requestToken, function(request, response) {
|
|
|
|
template.get('letter-debtor-nd', {
|
|
|
|
clientId: request.params.clientId,
|
|
|
|
companyId: request.params.companyId,
|
|
|
|
token: request.user.token,
|
|
|
|
isPreview: true
|
|
|
|
}, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
response.send(result.body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Payment method changes
|
|
|
|
router.get('/payment-update/:clientId', requestToken, 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.get('/client-deactivate/:clientId', requestToken, 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;
|
|
|
|
|
|
|
|
|
2017-08-31 14:11:37 +00:00
|
|
|
// Single user notification
|
2017-09-27 15:44:51 +00:00
|
|
|
/* router.post('/:recipient/noticeUserSend', function(request, response) {
|
2017-08-31 14:11:37 +00:00
|
|
|
var params = {
|
|
|
|
recipient: request.params.recipient,
|
|
|
|
sender: request.body.sender,
|
|
|
|
category: request.body.category,
|
|
|
|
message: request.body.message
|
|
|
|
};
|
2017-06-12 11:41:02 +00:00
|
|
|
|
2017-08-31 14:11:37 +00:00
|
|
|
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) => {
|
2017-09-27 15:44:51 +00:00
|
|
|
mail.sendWithTemplate('notification-notice', params, result => {
|
2017-08-31 14:11:37 +00:00
|
|
|
return response.json(result);
|
|
|
|
});
|
|
|
|
});
|
2017-09-27 15:44:51 +00:00
|
|
|
}); */
|
2017-08-31 14:11:37 +00:00
|
|
|
|
|
|
|
// Send notification to all user subscribed to category
|
2017-09-27 15:44:51 +00:00
|
|
|
/* router.post('/:category/noticeCategorySend', function(request, response) {
|
2017-08-31 14:11:37 +00:00
|
|
|
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;
|
|
|
|
|
2017-09-27 15:44:51 +00:00
|
|
|
mail.sendWithTemplate('notification-notice', params, result => {
|
2017-08-31 14:11:37 +00:00
|
|
|
return response.json(result);
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
});
|
2017-09-27 15:44:51 +00:00
|
|
|
}); */
|
2017-06-12 11:41:02 +00:00
|
|
|
|
2017-08-31 14:11:37 +00:00
|
|
|
// Send system notification
|
2017-09-27 15:44:51 +00:00
|
|
|
/* router.post('/:recipient/noticeSystemSend', function(request, response) {
|
2017-08-31 14:11:37 +00:00
|
|
|
var params = {
|
|
|
|
recipient: request.params.recipient,
|
2017-09-27 15:44:51 +00:00
|
|
|
sender: settings.smtp().auth.id,
|
2017-08-31 14:11:37 +00:00
|
|
|
category: request.body.category,
|
|
|
|
message: request.body.message
|
|
|
|
};
|
|
|
|
|
2017-09-27 15:44:51 +00:00
|
|
|
mail.sendWithTemplate('notification-notice', params, result => {
|
|
|
|
return response.json(result);
|
|
|
|
});
|
2017-12-26 09:02:47 +00:00
|
|
|
}); */
|