1
+ import { errorWithCode , logger } from '@bcgov/nodejs-common-utils' ;
2
+ import axios from "axios" ;
3
+ import fs from 'fs' ;
4
+
5
+ export class Cdogs {
6
+
7
+ constructor ( authenticaitonURL = process . env . CDOGS_AUTHENTICATION_URL ,
8
+ serviceURL = process . env . CDOGS_SERVICE_URL ,
9
+ clientId = process . env . CDOGS_CLIENT_ID ,
10
+ clientSecret = process . env . CDOGS_CLIENT_SECRET ,
11
+ enabled = process . env . CDOGS_ENABLED ) {
12
+ this . authenticationURL = authenticaitonURL ;
13
+ this . serviceURL = serviceURL ;
14
+ this . clientId = clientId ;
15
+ this . clientSecret = clientSecret ;
16
+ this . enabled = enabled ;
17
+ }
18
+
19
+ async init ( ) {
20
+ this . template = {
21
+ encodingType : 'base64' ,
22
+ fileType : 'docx' ,
23
+ content : await this . readTemplate ( )
24
+ }
25
+ this . options = {
26
+ cacheReport : false ,
27
+ convertTo : "pdf" ,
28
+ overwrite : true ,
29
+ }
30
+ }
31
+
32
+ async getBearerToken ( ) {
33
+ const tokenEndpoint = `${ this . authenticationURL } /auth/realms/comsvcauth/protocol/openid-connect/token`
34
+ const credentials = Buffer . from ( `${ this . clientId } :${ this . clientSecret } ` ) . toString ( 'base64' )
35
+ try {
36
+ const response = await axios . post ( tokenEndpoint , 'grant_type=client_credentials' , {
37
+ headers : {
38
+ 'Authorization' : `Basic ${ credentials } ` ,
39
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
40
+ }
41
+ } ) ;
42
+ logger . debug ( "Bearer token retrieved" )
43
+ return response . data . access_token
44
+ }
45
+ catch ( error ) {
46
+ logger . error ( `Failed to retrieve bearer token: ${ JSON . stringify ( error ) } ` )
47
+ throw errorWithCode ( 'Failed to retrieve bearer token' + JSON . stringify ( error ) , 500 )
48
+ }
49
+ }
50
+
51
+ async generatePDF ( planData ) {
52
+ if ( ! eval ( this . enabled ) )
53
+ return ;
54
+ const serviceURL = `${ this . serviceURL } /api/v2/template/render`
55
+ try {
56
+ const token = await this . getBearerToken ( )
57
+ const payload = {
58
+ data : planData ,
59
+ options : { ...this . options , 'reportName' : planData . agreementId + '.pdf' } ,
60
+ template : this . template
61
+ }
62
+ const response = await axios . post ( serviceURL , JSON . stringify ( payload ) , {
63
+ timeout : 20000 ,
64
+ responseType : 'arraybuffer' ,
65
+ headers : {
66
+ Authorization : `Bearer ${ token } ` ,
67
+ 'Content-Type' : 'application/json' ,
68
+ } ,
69
+ } ) ;
70
+ return response . data
71
+ } catch ( error ) {
72
+ logger . error ( `Error generating PDF file: ${ JSON . stringify ( error ) } ` )
73
+ throw errorWithCode ( `Error generating PDF file: ${ JSON . stringify ( error ) } ${ JSON . stringify ( error ) } ` , 500 )
74
+ }
75
+ }
76
+
77
+ async readTemplate ( template = './planTemplate.docx' ) {
78
+ try {
79
+ const data = fs . readFileSync ( template ) ;
80
+ return Buffer . from ( data ) . toString ( 'base64' ) ;
81
+ } catch ( error ) {
82
+ logger . error ( `Error reading template file ${ template } : ${ JSON . stringify ( error ) } ` )
83
+ throw errorWithCode ( `Error reading template file ${ template } : ${ JSON . stringify ( error ) } ` , 500 )
84
+ }
85
+ }
86
+ }
0 commit comments