salix/services/print/application/route/manuscript.js

172 lines
5.0 KiB
JavaScript
Raw Normal View History

2017-12-04 07:17:29 +00:00
var express = require('express');
var router = new express.Router();
var template = require('../template.js');
var config = require('../config.js');
var pdf = require('html-pdf');
var path = require('path');
var auth = require('../auth.js');
// Auth middleware
var requestToken = function(request, response, next) {
auth.init(request, response, next);
};
2017-12-04 07:17:29 +00:00
// Sepa core
router.get('/sepa-core/:companyId/:clientId', requestToken, function(request, response, next) {
let params = {
2018-10-01 08:58:44 +00:00
clientId: request.params.clientId,
companyId: request.params.companyId
};
template.get('sepa-core', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json({message: error.message});
2017-12-04 07:17:29 +00:00
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
2017-12-04 07:17:29 +00:00
if (error)
throw Error(error);
response.setHeader('Content-Disposition', 'attachment; filename="sepa-core.pdf"');
response.setHeader('Content-type', 'application/pdf');
2017-12-04 07:17:29 +00:00
stream.pipe(response);
});
2018-10-01 08:58:44 +00:00
});
});
2017-12-04 07:17:29 +00:00
// Sepa core html preview
router.get('/sepa-core/:companyId/:clientId/preview', requestToken, function(request, response, next) {
let params = {
2018-10-01 08:58:44 +00:00
clientId: request.params.clientId,
companyId: request.params.companyId,
isPreview: true
};
template.get('sepa-core', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json({message: error.message});
response.send(result.body);
2018-10-01 08:58:44 +00:00
});
});
// Debtor letter
router.get('/letter-debtor/:companyId/:clientId', requestToken, function(request, response, next) {
let params = {
2018-10-01 08:58:44 +00:00
clientId: request.params.clientId,
companyId: request.params.companyId
};
template.get('letter-debtor', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json({message: error.message});
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
if (error)
throw Error(error);
response.setHeader('Content-Disposition', 'attachment; filename="extracto.pdf"');
2017-12-04 07:17:29 +00:00
response.setHeader('Content-type', 'application/pdf');
stream.pipe(response);
2018-10-01 08:58:44 +00:00
});
2017-12-04 07:17:29 +00:00
});
2018-10-01 08:58:44 +00:00
});
// Debtor letter html preview
router.get('/letter-debtor/:companyId/:clientId/preview', requestToken, function(request, response, next) {
let params = {
2018-10-01 08:58:44 +00:00
clientId: request.params.clientId,
companyId: request.params.companyId,
isPreview: true
};
2017-12-04 07:17:29 +00:00
template.get('letter-debtor', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json({message: error.message});
2017-12-04 07:17:29 +00:00
response.send(result.body);
2018-10-01 08:58:44 +00:00
});
});
2017-12-04 07:17:29 +00:00
// Delivery note
router.get('/delivery-note/:ticketId/preview', requestToken, function(request, response) {
let params = {
ticketId: request.params.ticketId,
isPreview: true
};
2018-10-01 08:58:44 +00:00
template.get('delivery-note', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json(error);
response.send(result.body);
2018-10-01 08:58:44 +00:00
});
});
2018-10-01 08:58:44 +00:00
router.get('/delivery-note/:ticketId', requestToken, function(request, response, next) {
let params = {
ticketId: request.params.ticketId
};
template.get('delivery-note', params, (error, result) => {
2018-10-01 08:58:44 +00:00
if (error)
return response.status(400).json({message: error.message});
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
if (error)
throw Error(error);
stream.pipe(response);
2018-10-01 08:58:44 +00:00
});
});
2018-10-01 08:58:44 +00:00
});
2018-10-01 08:58:44 +00:00
router.get('/claim-pickup/:claimId/preview', requestToken, function(request, response) {
let params = {
claimId: request.params.claimId,
isPreview: true
};
template.get('claim-pickup', params, (error, result) => {
if (error)
return response.status(400).json(error);
response.send(result.body);
});
});
router.get('/claim-pickup/:claimId', requestToken, function(request, response) {
let params = {
claimId: request.params.claimId
};
template.get('claim-pickup', params, (error, result) => {
if (error)
return response.status(400).json({message: error.message});
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
if (error)
throw Error(error);
stream.pipe(response);
});
});
});
module.exports = router;
// store pdf
/* router.post('/sepa-core/:clientId', function(request, response, next) {
template.get('sepa-core', {recipient: request.params.clientId}, (error, result) => {
if (error)
return response.status(400).json({message: error.message});
pdf.create(result.body).toFile('./tmp/test.pdf', function(error, result) {
if (error)
return response.status(400).json({message: error});
console.log(result);
return response.json();
});
});
});
2018-10-01 08:58:44 +00:00
*/