-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from tidepool-org/pdf_generation_jhb
PDF Generation
- Loading branch information
Showing
18 changed files
with
36,504 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"ignorePatterns": ["pdfkit.js"], | ||
"extends": "airbnb", | ||
"parser": "babel-eslint", | ||
"plugins": ["lodash"], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16.20.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const fs = require('fs'); | ||
const { | ||
getSessionHeader, | ||
createCounter, | ||
exportTimeout, | ||
logMaker, | ||
} = require('../utils'); | ||
|
||
const { generateReport } = require('../reportUtils'); | ||
|
||
const reportStatusCount = createCounter( | ||
'tidepool_get_report_status_count', | ||
'The number of errors for each status code.', | ||
['status_code', 'report_params'], | ||
); | ||
|
||
const log = logMaker('getUserReportsHandler.js', { | ||
level: process.env.DEBUG_LEVEL || 'info', | ||
}); | ||
|
||
module.exports = function getUserReportsHandler() { | ||
return async (req, res) => { | ||
// Set the timeout for the request. Make it 10 seconds longer than | ||
// our configured timeout to give the service time to cancel the API data | ||
// request, and close the outgoing data stream cleanly. | ||
req.setTimeout(exportTimeout + 10000); | ||
|
||
try { | ||
const userId = req.params.userid; | ||
const { | ||
dob, | ||
fullName, | ||
mrn, | ||
bgUnits, | ||
tzName, | ||
restricted_token: restrictedToken, | ||
reports, | ||
} = req.query; | ||
|
||
const timer = setTimeout(() => { | ||
res.emit('timeout', exportTimeout); | ||
}, exportTimeout); | ||
|
||
const pdfReport = await generateReport( | ||
log, | ||
{ | ||
userId, | ||
fullName, | ||
dob, | ||
mrn, | ||
}, | ||
{ tzName, bgUnits, reports }, | ||
{ token: restrictedToken, sessionHeader: getSessionHeader(req) }, | ||
); | ||
|
||
res.setHeader('Content-Disposition', 'attachment: filename="report.pdf"'); | ||
res.setHeader('Content-Type', 'application/octet-stream'); | ||
const blobArrayBuffer = await pdfReport.blob.arrayBuffer(); | ||
const pdfBuffer = Buffer.from(blobArrayBuffer); | ||
fs.writeFileSync('test.pdf', pdfBuffer); | ||
res.send(pdfBuffer); | ||
clearTimeout(timer); | ||
} catch (error) { | ||
if (error.response && error.response.status === 403) { | ||
reportStatusCount.inc({ status_code: 403, report_params: req.query }); | ||
res.status(403); | ||
log.error(`403: ${error}`); | ||
} else { | ||
reportStatusCount.inc({ status_code: 500, report_params: req.query }); | ||
res.status(500); | ||
log.error(`500: ${error}`); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const getUserReportsHandler = require('./getUserReportsHandler'); | ||
const postUserReportsHandler = require('./postUserReportsHandler'); | ||
const userDataHandler = require('./userDataHandler'); | ||
|
||
module.exports = { | ||
postUserReport: postUserReportsHandler, | ||
getUserReport: getUserReportsHandler, | ||
getUserData: userDataHandler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const fs = require('fs'); | ||
const { | ||
getSessionHeader, | ||
createCounter, | ||
exportTimeout, | ||
logMaker, | ||
} = require('../utils'); | ||
|
||
const { generateReport } = require('../reportUtils'); | ||
|
||
const reportStatusCount = createCounter( | ||
'tidepool_post_report_status_count', | ||
'The number of errors for each status code.', | ||
['status_code', 'report_params'], | ||
); | ||
|
||
const log = logMaker('postUserReportsHandler.js', { | ||
level: process.env.DEBUG_LEVEL || 'info', | ||
}); | ||
|
||
module.exports = function postUserReportsHandler() { | ||
return async (req, res) => { | ||
// Set the timeout for the request. Make it 10 seconds longer than | ||
// our configured timeout to give the service time to cancel the API data | ||
// request, and close the outgoing data stream cleanly. | ||
req.setTimeout(exportTimeout + 10000); | ||
|
||
try { | ||
const userId = req.params.userid; | ||
// TODO: which token will we use here? | ||
const restrictedToken = ''; | ||
const { userDetail, reportDetail } = req.body; | ||
|
||
userDetail.userId = userId; | ||
const timer = setTimeout(() => { | ||
res.emit('timeout', exportTimeout); | ||
}, exportTimeout); | ||
|
||
const pdfReport = await generateReport(log, userDetail, reportDetail, { | ||
token: restrictedToken, | ||
sessionHeader: getSessionHeader(req), | ||
}); | ||
|
||
res.setHeader('Content-Disposition', 'attachment: filename="report.pdf"'); | ||
res.setHeader('Content-Type', 'application/octet-stream'); | ||
const blobArrayBuffer = await pdfReport.blob.arrayBuffer(); | ||
const pdfBuffer = Buffer.from(blobArrayBuffer); | ||
fs.writeFileSync('test.pdf', pdfBuffer); | ||
res.send(pdfBuffer); | ||
clearTimeout(timer); | ||
} catch (error) { | ||
if (error.response && error.response.status === 403) { | ||
reportStatusCount.inc({ status_code: 403, report_params: req.query }); | ||
res.status(403); | ||
log.error(`403: ${error}`); | ||
} else { | ||
reportStatusCount.inc({ status_code: 500, report_params: req.query }); | ||
res.status(500); | ||
log.error(`500: ${error}`); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.