Skip to content

Commit

Permalink
Cdog Integration
Browse files Browse the repository at this point in the history
PDF changes/improvement
  • Loading branch information
Brijesh committed Oct 5, 2023
1 parent 3733e7a commit 616405e
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 485 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ schemaspy
import
public/doc
sonar-runner/build
sonar-runner/.gradle

# Autogenerated document location. See npm scripts.
apidoc
Expand Down
Binary file added planTemplate.docx
Binary file not shown.
86 changes: 86 additions & 0 deletions src/libs/cdogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { errorWithCode, logger } from '@bcgov/nodejs-common-utils';
import axios from "axios";
import fs from 'fs';

export class Cdogs {

constructor(authenticaitonURL = process.env.CDOGS_AUTHENTICATION_URL,
serviceURL = process.env.CDOGS_SERVICE_URL,
clientId = process.env.CDOGS_CLIENT_ID,
clientSecret = process.env.CDOGS_CLIENT_SECRET,
enabled = process.env.CDOGS_ENABLED) {
this.authenticationURL = authenticaitonURL;
this.serviceURL = serviceURL;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.enabled = enabled;
}

async init() {
this.template = {
encodingType: 'base64',
fileType: 'docx',
content: await this.readTemplate()
}
this.options = {
cacheReport: false,
convertTo: "pdf",
overwrite: true,
}
}

async getBearerToken() {
const tokenEndpoint = `${this.authenticationURL}/auth/realms/comsvcauth/protocol/openid-connect/token`
const credentials = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString('base64')
try {
const response = await axios.post(tokenEndpoint, 'grant_type=client_credentials', {
headers: {
'Authorization': `Basic ${credentials}`,
'Content-Type': 'application/x-www-form-urlencoded',
}
});
logger.debug("Bearer token retrieved")
return response.data.access_token
}
catch (error) {
logger.error(`Failed to retrieve bearer token: ${JSON.stringify(error)}`)
throw errorWithCode('Failed to retrieve bearer token' + JSON.stringify(error), 500)
}
}

async generatePDF(planData) {
if (!eval(this.enabled))
return;
const serviceURL = `${this.serviceURL}/api/v2/template/render`
try {
const token = await this.getBearerToken()
const payload = {
data: planData,
options: { ...this.options, 'reportName': planData.agreementId + '.pdf' },
template: this.template
}
const response = await axios.post(serviceURL, JSON.stringify(payload), {
timeout: 20000,
responseType: 'arraybuffer',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
return response.data
} catch (error) {
logger.error(`Error generating PDF file: ${JSON.stringify(error)}`)
throw errorWithCode(`Error generating PDF file: ${JSON.stringify(error)} ${JSON.stringify(error)}`, 500)
}
}

async readTemplate(template = './planTemplate.docx') {
try {
const data = fs.readFileSync(template);
return Buffer.from(data).toString('base64');
} catch (error) {
logger.error(`Error reading template file ${template}: ${JSON.stringify(error)}`)
throw errorWithCode(`Error reading template file ${template}: ${JSON.stringify(error)}`, 500)
}
}
}
2 changes: 1 addition & 1 deletion src/libs/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Mailer {
try {
const token = await this.getBearerToken()
const emailPayload = { to, from, subject, body, bodyType, }
logger.debug("email payload: " + JSON.stringify(emailPayload))
logger.debug("email payload: " + JSON.stringify(emailPayload))
await axios.post(emailEndpoint, JSON.stringify(emailPayload), {
headers: {
'Content-Type': 'application/json',
Expand Down
Loading

0 comments on commit 616405e

Please sign in to comment.