@@ -22,7 +22,7 @@ const {
2222 PRINTING = { }
2323} = require ( '../../config/config.json' ) ;
2424const AuditLogActions = require ( '../util/auditLogActions.js' ) ;
25- const { createAuditLog } = require ( '../util/auditLogHelpers .js' ) ;
25+ const AuditLog = require ( '../models/AuditLog .js' ) ;
2626
2727// see https://github.com/SCE-Development/Quasar/tree/dev/docker-compose.dev.yml#L11
2828let PRINTER_URL = process . env . PRINTER_URL || 'http://localhost:14000' ;
@@ -62,7 +62,6 @@ router.get('/healthCheck', async (req, res) => {
6262 logger . warn (
6363 'Printing is disabled, returning 200 to mock the printing server'
6464 ) ;
65-
6665 return res . sendStatus ( OK ) ;
6766 }
6867 await axios
@@ -79,8 +78,6 @@ router.get('/healthCheck', async (req, res) => {
7978
8079router . post ( '/sendPrintRequest' , upload . single ( 'chunk' ) , async ( req , res ) => {
8180 let totalFileSize = 0 ;
82- const { copies, sides, id } = req . body ;
83- const action = AuditLogActions . PRINT_PAGE ;
8481
8582 if ( ! checkIfTokenSent ( req ) ) {
8683 logger . warn ( '/sendPrintRequest was requested without a token' ) ;
@@ -91,29 +88,13 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
9188 logger . warn ( '/sendPrintRequest was requested with an invalid token' ) ;
9289 return res . sendStatus ( UNAUTHORIZED ) ;
9390 }
94-
95- const details = {
96- copies : parseInt ( copies ) ,
97- sides,
98- fileSize : totalFileSize ,
99- userEmail : user . email ,
100- printedAt : new Date ( ) ,
101- printJobId : id ,
102- status : 'success' || 'fail'
103- } ;
104-
10591 if ( ! PRINTING . ENABLED ) {
106- details . status = 'mocked' ;
107- // create audit log on print
108- await createAuditLog ( {
109- user,
110- action,
111- details
112- } ) ;
11392 logger . warn ( 'Printing is disabled, returning 200 to mock the printing server' ) ;
11493 return res . sendStatus ( OK ) ;
11594 }
11695
96+ const { copies, sides, id } = req . body ;
97+
11798 const chunks = await fs . promises . readdir ( dir ) ;
11899 const assembledPdfFromChunks = path . join ( dir , id + '.pdf' ) ;
119100
@@ -145,13 +126,20 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
145126 }
146127 } )
147128 . then ( async ( ) => {
148- details . status = 'success' ;
129+
149130 // create audit log on print
150- await createAuditLog ( {
151- user,
152- action,
153- details
154- } ) ;
131+ await AuditLog . create ( {
132+ userId : user . _id ,
133+ action : AuditLogActions . PRINT_PAGE ,
134+ details : {
135+ copies : parseInt ( copies ) ,
136+ sides,
137+ fileSize : totalFileSize ,
138+ userEmail : user . email ,
139+ printedAt : new Date ( ) ,
140+ printJobId : id
141+ }
142+ } ) . catch ( logger . error ) ;
155143
156144 // delete file from temp folder after printing
157145 fs . unlink ( file . path , ( err ) => {
@@ -160,10 +148,8 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
160148 }
161149 } ) ;
162150 res . sendStatus ( OK ) ;
163- } ) . catch ( async ( err ) => {
151+ } ) . catch ( ( err ) => {
164152 logger . error ( '/sendPrintRequest had an error: ' , err ) ;
165- details . status = 'fail' ;
166- await createAuditLog ( { user, action, details } ) ;
167153 res . sendStatus ( SERVER_ERROR ) ;
168154 } ) ;
169155} ) ;
0 commit comments