Skip to content

Commit

Permalink
Merge pull request #3 from nhsengland/logging
Browse files Browse the repository at this point in the history
adds app insights logging to pdf generator
  • Loading branch information
assimoes-bjss authored Sep 2, 2021
2 parents cabf8a8 + 4d5bb7a commit b81da2e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/server/routes/pdf-generator.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { IProfile } from 'passport-azure-ad';
import { BASE_PATH } from '../config/constants.config';
import { generatePDF } from '../utils/pdf/parser';
import { getAccessTokenByOid } from './authentication.routes';
import { getAppInsightsClient } from '../../globals';

const pdfRouter: Router = express.Router();

// Generate PDF endpoint
pdfRouter.get(`${BASE_PATH}/exports/:innovationId/pdf`, (req, res) => {
try {

const innovationId = req.params.innovationId;
const user: IProfile = req.user || {};
const oid: string = user.oid || '';
Expand All @@ -16,20 +19,44 @@ pdfRouter.get(`${BASE_PATH}/exports/:innovationId/pdf`, (req, res) => {

generatePDF(req.params.innovationId, oid, config)
.then((response: any) => {

const client = getAppInsightsClient(req);

client.trackTrace({
message: 'PDFGenerator Success',
severity: 0,
});

res
.writeHead(200, {
'Content-Length': Buffer.byteLength(response),
'Content-Type': 'application/pdf',
'Content-disposition': `attachment;filename=innovation_record_${innovationId}.pdf`
})
.end(response);

})
.catch((error: any) => {
const client = getAppInsightsClient(req);
client.trackTrace({
message: 'PDFGenerator Error',
severity: 3,
properties: error,
});
console.log(error);
console.log(`Error when attempting to generate the PDF from innovation ${innovationId}`);
res.status(500).send();
});

} catch (error) {
const client = getAppInsightsClient(req);
client.trackTrace({
message: 'PDFGenerator Unhandled Error',
severity: 3,
properties: error,
});
}

});

export default pdfRouter;

0 comments on commit b81da2e

Please sign in to comment.