@@ -10,43 +10,42 @@ const CONSOLE_MODE = process.env.PRINT_CONSOLE_MODE === 'true';
1010 * @param {Object } req - The request object.
1111 */
1212const populateQueueValueHelp = async function ( _ , req ) {
13-
14- if ( ! CONSOLE_MODE ) {
15- const vcap = await readVcapServices ( ) ;
16- if ( ! vcap || vcap ?. status === 500 ) {
17- // For Production, return the error if print service is not found
18- logger . error ( 'Print service not found' ) ;
19- return req . error ( 500 , 'Print service not found' ) ;
20- }
21- const jwt = await getJwt ( req , vcap ) ;
22- if ( jwt ?. code === 500 ) {
23- logger . error ( 'Failed to retrieve token' ) ;
24- return req . error ( 500 , 'Failed to retrieve token' ) ;
25- }
26-
27- const api = axios . create ( {
28- baseURL : vcap . service_url ,
29- headers : {
30- 'Authorization' : `Bearer ${ jwt } ` ,
31- "Accept" : "*/*" ,
32- "Content-Type" : "application/json"
13+ if ( ! CONSOLE_MODE ) {
14+ const vcap = await readVcapServices ( ) ;
15+ if ( ! vcap || vcap ?. status === 500 ) {
16+ // For Production, return the error if print service is not found
17+ logger . error ( 'Print service not found' ) ;
18+ return req . error ( 500 , 'Print service not found' ) ;
19+ }
20+ const jwt = await getJwt ( req , vcap ) ;
21+ if ( jwt ?. code === 500 ) {
22+ logger . error ( 'Failed to retrieve token' ) ;
23+ return req . error ( 500 , 'Failed to retrieve token' ) ;
3324 }
34- } ) ;
35- const resp = await api . get ( `/qm/api/v1/rest/queues` ) ;
3625
37- resp . data . forEach ( ( item , index ) => {
38- req . results [ index ] = { ID : item . qname } ;
39- } ) ;
40- req . results . $count = resp . data . length ;
41- } else {
42- const offlineQueues = [
43- { ID : 'DEFAULT_PRINTER' } ,
44- { ID : 'HP_LASERJET_PRO' } ,
45- { ID : 'CANON_IMAGECLASS' } ,
46- { ID : 'XEROX_WORKCENTRE' } ,
47- { ID : 'OFFICE_PRINTER_01' } ,
48- { ID : 'OFFICE_PRINTER_02' }
49- ] ;
26+ const api = axios . create ( {
27+ baseURL : vcap . service_url ,
28+ headers : {
29+ 'Authorization' : `Bearer ${ jwt } ` ,
30+ "Accept" : "*/*" ,
31+ "Content-Type" : "application/json"
32+ }
33+ } ) ;
34+ const resp = await api . get ( `/qm/api/v1/rest/queues` ) ;
35+
36+ resp . data . forEach ( ( item , index ) => {
37+ req . results [ index ] = { ID : item . qname } ;
38+ } ) ;
39+ req . results . $count = resp . data . length ;
40+ } else {
41+ const offlineQueues = [
42+ { ID : 'DEFAULT_PRINTER' } ,
43+ { ID : 'HP_LASERJET_PRO' } ,
44+ { ID : 'CANON_IMAGECLASS' } ,
45+ { ID : 'XEROX_WORKCENTRE' } ,
46+ { ID : 'OFFICE_PRINTER_01' } ,
47+ { ID : 'OFFICE_PRINTER_02' }
48+ ] ;
5049
5150 offlineQueues . forEach ( ( item , index ) => {
5251 req . results [ index ] = { ID : item . ID } ;
@@ -55,91 +54,91 @@ if (!CONSOLE_MODE) {
5554 return ;
5655 }
5756}
57+
5858/**
5959 * Handles the print request.
6060 * @param {Object } _ - Unused parameter.
6161 * @param {Object } req - The request object.
6262 */
6363const print = async function ( _ , req ) {
6464 let { qname, numberOfCopies, docsToPrint } = await getPrintConfigFromActionOrEntity ( req ) ;
65-
6665 if ( ! CONSOLE_MODE ) {
67- const vcap = await readVcapServices ( req ) ;
68- if ( ! vcap || vcap ?. status === 500 ) {
69- // For Production, return the error if print service is not found
70- logger . error ( 'Print service not found' ) ;
71- return req . error ( 500 , 'Print service not found' ) ;
72- }
73- const jwt = await getJwt ( req , vcap ) ;
74- if ( jwt ?. code === 500 ) {
75- logger . error ( 'Failed to retrieve token' ) ;
76- return req . error ( 500 , 'Failed to retrieve token' ) ;
77- }
78-
79- const api = axios . create ( {
80- baseURL : vcap . service_url ,
81- headers : {
82- 'Authorization' : `Bearer ${ jwt } ` ,
83- "DataServiceVersion" : "2.0" ,
84- "Accept" : "*/*" ,
85- "Content-Type" : "application/json" ,
86- 'If-None-Match' : '*' ,
87- 'scan' : true
88- }
89- } ) ;
90-
91- // Upload documents to be printed
92- for ( const doc of docsToPrint ) {
93- if ( ! doc . content ) {
94- logger . error ( 'No content provided for printing' ) ;
95- return req . error ( 'No content provided for printing' ) ;
66+ const vcap = await readVcapServices ( req ) ;
67+ if ( ! vcap || vcap ?. status === 500 ) {
68+ // For Production, return the error if print service is not found
69+ logger . error ( 'Print service not found' ) ;
70+ return req . error ( 500 , 'Print service not found' ) ;
9671 }
97- let documentResp ;
98- try {
99- documentResp = await api . post ( '/dm/api/v1/rest/print-documents' , doc . content ) ;
100- doc . objectKey = documentResp . data ;
101- } catch ( e ) {
102- logger . error ( `Error in uploading document ${ doc . fileName } : ` , e . response ?. data ?. error ?. message ) ;
103- return req . error ( e . response ?. data ?. error ?. message ) ;
72+ const jwt = await getJwt ( req , vcap ) ;
73+ if ( jwt ?. code === 500 ) {
74+ logger . error ( 'Failed to retrieve token' ) ;
75+ return req . error ( 500 , 'Failed to retrieve token' ) ;
10476 }
105- }
10677
107- let printTask = {
108- numberOfCopies : numberOfCopies ,
109- username : cds . context ?. user ?. id ,
110- qname : qname ,
111- printContents : [ ]
112- }
113- let itemId = "" ;
114- // Create Print Content
115- docsToPrint . forEach ( async ( doc ) => {
116- printTask . printContents . push ( {
117- objectKey : doc . objectKey ,
118- documentName : doc . fileName
119- } )
78+ const api = axios . create ( {
79+ baseURL : vcap . service_url ,
80+ headers : {
81+ 'Authorization' : `Bearer ${ jwt } ` ,
82+ "DataServiceVersion" : "2.0" ,
83+ "Accept" : "*/*" ,
84+ "Content-Type" : "application/json" ,
85+ 'If-None-Match' : '*' ,
86+ 'scan' : true
87+ }
88+ } ) ;
12089
121- if ( doc . isMainDocument ) {
122- itemId = doc . objectKey ;
90+ // Upload documents to be printed
91+ for ( const doc of docsToPrint ) {
92+ if ( ! doc . content ) {
93+ logger . error ( 'No content provided for printing' ) ;
94+ return req . error ( 'No content provided for printing' ) ;
95+ }
96+ let documentResp ;
97+ try {
98+ documentResp = await api . post ( '/dm/api/v1/rest/print-documents' , doc . content ) ;
99+ doc . objectKey = documentResp . data ;
100+ } catch ( e ) {
101+ logger . error ( `Error in uploading document ${ doc . fileName } : ` , e . response ?. data ?. error ?. message ) ;
102+ return req . error ( e . response ?. data ?. error ?. message ) ;
103+ }
123104 }
124- } ) ;
125105
126- // Print Task
127- let printTaskResp ;
128- try {
129- printTaskResp = await api . put ( `/qm/api/v1/rest/print-tasks/${ itemId } ` , printTask ) ;
130- } catch ( e ) {
131- logger . error ( 'Error in sending to print queue: ' , e . response ?. data ?. error ?. message ) ;
132- return req . error ( 'Print task failed' ) ;
106+ let printTask = {
107+ numberOfCopies : numberOfCopies ,
108+ username : cds . context ?. user ?. id ,
109+ qname : qname ,
110+ printContents : [ ]
133111 }
134- logger . info ( `Document sent to print queue ${ qname } ` ) ;
135- return req . info ( 200 , `Document sent to print queue ${ qname } \n
136- No. of copies requested: ${ numberOfCopies } ` ) ;
137- } else {
138- // Offline / Console Mode
139- docsToPrint . forEach ( ( doc ) => {
140- logger . info ( `Document ${ doc . fileName } sent to print queue ${ qname } ` ) ;
112+ let itemId = "" ;
113+ // Create Print Content
114+ docsToPrint . forEach ( async ( doc ) => {
115+ printTask . printContents . push ( {
116+ objectKey : doc . objectKey ,
117+ documentName : doc . fileName
118+ } )
119+
120+ if ( doc . isMainDocument ) {
121+ itemId = doc . objectKey ;
122+ }
141123 } ) ;
142- }
124+
125+ // Print Task
126+ let printTaskResp ;
127+ try {
128+ printTaskResp = await api . put ( `/qm/api/v1/rest/print-tasks/${ itemId } ` , printTask ) ;
129+ } catch ( e ) {
130+ logger . error ( 'Error in sending to print queue: ' , e . response ?. data ?. error ?. message ) ;
131+ return req . error ( 'Print task failed' ) ;
132+ }
133+ logger . info ( `Document sent to print queue ${ qname } ` ) ;
134+ return req . info ( 200 , `Document sent to print queue ${ qname } \n
135+ No. of copies requested: ${ numberOfCopies } ` ) ;
136+ } else {
137+ // Offline / Console Mode
138+ docsToPrint . forEach ( ( doc ) => {
139+ logger . info ( `Document ${ doc . fileName } with object key ${ doc . objectKey } and content length ${ doc . content . length } has been sent to print queue ${ qname } ` ) ;
140+ } ) ;
141+ }
143142}
144143
145144module . exports = { print, populateQueueValueHelp } ;
0 commit comments