Skip to content

Commit d37448a

Browse files
author
Brijesh
committed
Cdog Integration
PDF changes/improvement
1 parent 3733e7a commit d37448a

File tree

11 files changed

+330
-485
lines changed

11 files changed

+330
-485
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ schemaspy
1818
import
1919
public/doc
2020
sonar-runner/build
21+
sonar-runner/.gradle
2122

2223
# Autogenerated document location. See npm scripts.
2324
apidoc

planTemplate.docx

82.5 KB
Binary file not shown.

src/libs/cdogs.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/libs/mailer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Mailer {
3737
try {
3838
const token = await this.getBearerToken()
3939
const emailPayload = { to, from, subject, body, bodyType, }
40-
logger.debug("email payload: " + JSON.stringify(emailPayload))
40+
logger.debug("email payload: " + JSON.stringify(emailPayload))
4141
await axios.post(emailEndpoint, JSON.stringify(emailPayload), {
4242
headers: {
4343
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)